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

This includes #callbacks, #constructor, and #to_create. The reasoning behind this is that we were mimicing an inheritance chain via methods; now, we actually generate classes, which Factory maintains, who inherit from their parent's hierarchy. We build the hierarchy during compilation to conditionally define methods based on whether what we're dealing with from the definition is actually meaningful. The base class (DefinitionHierarchy) uses the defaults (an empty array for #callbacks and the global #to_create and #constructor) so once we hit the top level, if the definition doesn't set any overrides, we have a list of sensible values.
39 lines
641 B
Ruby
39 lines
641 B
Ruby
module FactoryGirl
|
|
class DefinitionHierarchy
|
|
def callbacks
|
|
[]
|
|
end
|
|
|
|
def constructor
|
|
FactoryGirl.constructor
|
|
end
|
|
|
|
def to_create
|
|
FactoryGirl.to_create
|
|
end
|
|
|
|
def self.add_callbacks(callbacks)
|
|
if callbacks.any?
|
|
define_method :callbacks do
|
|
super() + callbacks
|
|
end
|
|
end
|
|
end
|
|
|
|
def self.build_constructor(&block)
|
|
if block
|
|
define_method(:constructor) do
|
|
block
|
|
end
|
|
end
|
|
end
|
|
|
|
def self.build_to_create(&block)
|
|
if block
|
|
define_method(:to_create) do
|
|
block
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|