1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00
This commit is contained in:
Joshua Clayton 2014-11-04 13:44:15 -08:00
parent 8971b820d9
commit 71d1da0d28
7 changed files with 115 additions and 2 deletions

View file

@ -23,6 +23,14 @@ module FactoryGirl
map(&:name)
end
def hash
name.hash ^ attributes.hash
end
def ==(other)
name == other.name && attributes == other.attributes
end
def associations
AttributeList.new(@name, select(&:association?))
end
@ -39,6 +47,10 @@ module FactoryGirl
attributes_to_apply.each { |attribute| add_attribute(attribute) }
end
protected
attr_reader :name, :attributes
private
def add_attribute(attribute)

View file

@ -1,6 +1,27 @@
require 'active_support/core_ext/class/attribute'
module FactoryGirl
# @api private
class AttributesModuleGenerator
class_attribute :foo
self.foo = {}
def self.cleanup
self.foo = {}
end
def self.to_module(attributes)
if FactoryGirl.configuration.module_caching
if result = foo[attributes.hash]
result
else
foo[attributes.hash] = new(attributes).to_module
end
else
new(attributes).to_module
end
end
def initialize(attributes)
@attributes = attributes
end

View file

@ -3,6 +3,8 @@ module FactoryGirl
class Configuration
attr_reader :factories, :sequences, :traits, :strategies, :callback_names
attr_accessor :module_caching
def initialize
@factories = Decorator::DisallowsDuplicatesRegistry.new(Registry.new('Factory'))
@sequences = Decorator::DisallowsDuplicatesRegistry.new(Registry.new('Sequence'))
@ -10,6 +12,7 @@ module FactoryGirl
@strategies = Registry.new('Strategy')
@callback_names = Set.new
@definition = Definition.new
@module_caching = true
to_create { |instance| instance.save! }
initialize_with { new }

View file

@ -16,7 +16,7 @@ module FactoryGirl
@cached_attributes = overrides
@instance = nil
extend HashModuleGenerator.new(@overrides).to_module
extend HashModuleGenerator.to_module(@overrides)
end
def association(factory_name, *traits_and_overrides)

View file

@ -5,7 +5,7 @@ module FactoryGirl
@parent_class = parent_class
@attributes = attributes
evaluator_class.send :include, AttributesModuleGenerator.new(attributes).to_module
evaluator_class.send :include, AttributesModuleGenerator.to_module(attributes)
end
def evaluator_class

View file

@ -1,6 +1,27 @@
require 'active_support/core_ext/class/attribute'
module FactoryGirl
# @api private
class HashModuleGenerator
class_attribute :foo
self.foo = {}
def self.cleanup
self.foo = {}
end
def self.to_module(attributes)
if FactoryGirl.configuration.module_caching
if result = foo[attributes.hash]
result
else
foo[attributes.hash] = new(attributes).to_module
end
else
new(attributes).to_module
end
end
def initialize(hash)
@hash = hash
end

View file

@ -141,3 +141,59 @@ describe FactoryGirl::AttributeList, "generating names" do
expect(subject.associations.names).to eq [:avatar]
end
end
describe FactoryGirl::AttributeList, "#hash" do
let(:static_attribute) { FactoryGirl::Attribute::Static.new(:full_name, "value", false) }
let(:dynamic_attribute) { FactoryGirl::Attribute::Dynamic.new(:email, false, ->(u) { "#{u.full_name}@example.com" }) }
it "generates the value correctly" do
list = FactoryGirl::AttributeList.new(:foo)
list.define_attribute(static_attribute)
second_list = FactoryGirl::AttributeList.new(:foo)
second_list.define_attribute static_attribute
expect(list.hash).to eq second_list.hash
end
it "generates the value correctly again" do
list = FactoryGirl::AttributeList.new(:foo)
list.define_attribute(static_attribute)
second_list = FactoryGirl::AttributeList.new(:bar)
second_list.define_attribute static_attribute
expect(list.hash).not_to eq second_list.hash
end
it "generates the value correctly a third time" do
list = FactoryGirl::AttributeList.new(:foo)
list.define_attribute(static_attribute)
second_list = FactoryGirl::AttributeList.new(:foo)
second_list.define_attribute dynamic_attribute
expect(list.hash).not_to eq second_list.hash
end
it "generates the value correctly a fourth time" do
list = FactoryGirl::AttributeList.new(:foo)
list.define_attribute(dynamic_attribute)
second_list = FactoryGirl::AttributeList.new(:foo)
second_list.define_attribute dynamic_attribute
expect(list.hash).to eq second_list.hash
end
it "generates the value correctly a fifth time" do
list = FactoryGirl::AttributeList.new(:foo)
list.define_attribute(dynamic_attribute)
list.define_attribute(static_attribute)
second_list = FactoryGirl::AttributeList.new(:foo)
second_list.define_attribute dynamic_attribute
expect(list.hash).not_to eq second_list.hash
end
end