Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor NullClassBuilder#generate_class #32

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions lib/naught/base_class_initializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Naught
class BaseClassInitializer
attr_reader :builder, :base_class

def initialize(builder)
@builder = builder
@base_class = builder.base_class
end

def execute(generation_mod, customization_mod)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When there is a class that only does one thing, I like to name that method call. This makes it easy to substitute with a proc, lambda, or Method object. There’s actually a RubyTapas episode about this, which describes why this can be useful. 😄

builder = @builder
Class.new(base_class) do
const_set :GeneratedMethods, generation_mod
const_set :Customizations, customization_mod
const_set :NULL_EQUIVS, builder.null_equivalents
include Conversions
remove_const :NULL_EQUIVS
Conversions.instance_methods.each do |instance_method|
undef_method(instance_method)
end
const_set :Conversions, Conversions

include NullObjectTag
include generation_mod
include customization_mod
end
end
end
end
24 changes: 6 additions & 18 deletions lib/naught/null_class_builder.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'naught/basic_object'
require 'naught/conversions'
require 'naught/base_class_initializer'

module Naught
class NullClassBuilder
Expand Down Expand Up @@ -38,31 +39,18 @@ def generate_class
respond_to_any_message unless interface_defined?
generation_mod = Module.new
customization_mod = customization_module # get a local binding
builder = self

apply_operations(operations, generation_mod)

null_class = Class.new(@base_class) do
const_set :GeneratedMethods, generation_mod
const_set :Customizations, customization_mod
const_set :NULL_EQUIVS, builder.null_equivalents
include Conversions
remove_const :NULL_EQUIVS
Conversions.instance_methods.each do |instance_method|
undef_method(instance_method)
end
const_set :Conversions, Conversions

include NullObjectTag
include generation_mod
include customization_mod
end

null_class = initialize_base_class(generation_mod, customization_mod)
apply_operations(class_operations, null_class)

null_class
end

def initialize_base_class(generation_mod, customization_mod)
BaseClassInitializer.new(self).execute(generation_mod, customization_mod)
end

def method_missing(method_name, *args, &block)
command_name = command_name_for_method(method_name)
if Commands.const_defined?(command_name)
Expand Down