1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00

Standardize on ability to define attributes on an evaluator

This commit is contained in:
Joshua Clayton 2012-05-05 00:45:54 -04:00
parent d475033a81
commit 2d01b6f793
2 changed files with 12 additions and 14 deletions

View file

@ -13,6 +13,16 @@ module FactoryGirl
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
@ -24,7 +34,7 @@ module FactoryGirl
@cached_attributes = overrides
@overrides.each do |name, value|
singleton_class.send :define_method, name, -> { value }
singleton_class.define_cached_attribute(name) { value }
end
end

View file

@ -5,7 +5,7 @@ module FactoryGirl
@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
@ -15,17 +15,5 @@ module FactoryGirl
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