Skip to content

Commit

Permalink
Standardize on ability to define attributes on an evaluator
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaclayton committed May 5, 2012
1 parent d475033 commit 2d01b6f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
12 changes: 11 additions & 1 deletion lib/factory_girl/evaluator.rb
Expand Up @@ -13,6 +13,16 @@ def self.attribute_list
end
end

def self.define_cached_attribute(name, &block)
define_method(name) do
if @cached_attributes.key?(name)
@cached_attributes[name]
else
@cached_attributes[name] = instance_exec(&block)
end
end
end

private_instance_methods.each do |method|
undef_method(method) unless method =~ /^__|initialize/
end
Expand All @@ -24,7 +34,7 @@ def initialize(build_class, build_strategy, overrides = {})
@cached_attributes = overrides

@overrides.each do |name, value|
singleton_class.send :define_method, name, -> { value }
singleton_class.define_cached_attribute(name) { value }
end
end

Expand Down
14 changes: 1 addition & 13 deletions lib/factory_girl/evaluator_class_definer.rb
Expand Up @@ -5,7 +5,7 @@ def initialize(attributes, parent_class)
@attributes = attributes

attributes.each do |attribute|
define_attribute(attribute.name, attribute.to_proc)
evaluator_class.define_cached_attribute(attribute.name, &attribute.to_proc)
end
end

Expand All @@ -15,17 +15,5 @@ def evaluator_class
klass.attribute_lists += [@attributes]
end
end

private

def define_attribute(attribute_name, attribute_proc)
evaluator_class.send(:define_method, attribute_name) do
if @cached_attributes.key?(attribute_name)
@cached_attributes[attribute_name]
else
@cached_attributes[attribute_name] = instance_exec(&attribute_proc)
end
end
end
end
end

0 comments on commit 2d01b6f

Please sign in to comment.