Skip to content

Commit

Permalink
Move constructor to definition
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaclayton committed May 6, 2012
1 parent 8e0d492 commit 8ff5fc2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
10 changes: 9 additions & 1 deletion lib/factory_girl/definition.rb
Expand Up @@ -10,7 +10,7 @@ def initialize(name = nil, base_traits = [])
@to_create = ->(instance) { instance.save! }
@base_traits = base_traits
@additional_traits = []
@constructor = nil
@constructor = default_constructor
end

delegate :declare_attribute, to: :declarations
Expand Down Expand Up @@ -56,8 +56,16 @@ def define_constructor(&block)
@constructor = block
end

def custom_constructor?
@constructor != default_constructor
end

private

def default_constructor
@default_constructor ||= -> { new }
end

def base_traits
@base_traits.map { |name| trait_by_name(name) }
end
Expand Down
14 changes: 7 additions & 7 deletions lib/factory_girl/factory.rb
Expand Up @@ -34,7 +34,7 @@ def run(build_strategy, overrides, &block)
strategy = StrategyCalculator.new(build_strategy).strategy.new

evaluator = evaluator_class.new(build_class, strategy, overrides.symbolize_keys)
attribute_assigner = AttributeAssigner.new(evaluator, build_class, &instance_builder)
attribute_assigner = AttributeAssigner.new(evaluator, build_class, &constructor)

evaluation = Evaluation.new(attribute_assigner, to_create)
evaluation.add_observer(CallbacksObserver.new(callbacks, evaluator))
Expand Down Expand Up @@ -118,7 +118,12 @@ def callbacks
end

def constructor
@constructor ||= @definition.constructor || parent.constructor
@constructor ||=
if @definition.custom_constructor?
@definition.constructor
else
parent.constructor
end
end

private
Expand All @@ -135,11 +140,6 @@ def parent
end
end

def instance_builder
build_class = self.build_class
constructor || -> { build_class.new }
end

def initialize_copy(source)
super
@definition = @definition.clone
Expand Down

0 comments on commit 8ff5fc2

Please sign in to comment.