Class: FactoryGirl::Evaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/factory_girl/evaluator.rb

Defined Under Namespace

Classes: CallbackRunner

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(build_strategy, overrides = {}) ⇒ Evaluator

Returns a new instance of Evaluator.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/factory_girl/evaluator.rb', line 20

def initialize(build_strategy, overrides = {})
  @build_strategy    = build_strategy
  @overrides         = overrides
  @cached_attributes = overrides

  @overrides.each do |name, value|
    singleton_class.send :define_method, name, lambda { value }
  end

  @build_strategy.add_observer(CallbackRunner.new(self.class.callbacks, self))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/factory_girl/evaluator.rb', line 48

def method_missing(method_name, *args, &block)
  if @cached_attributes.key?(method_name)
    @cached_attributes[method_name]
  else
    @instance.send(method_name, *args, &block)
  end
end

Class Method Details

.attribute_listObject



8
9
10
11
12
13
14
# File 'lib/factory_girl/evaluator.rb', line 8

def self.attribute_list
  AttributeList.new.tap do |list|
    attribute_lists.each do |attribute_list|
      list.apply_attributes attribute_list.to_a
    end
  end
end

Instance Method Details

#__overridesObject



56
57
58
# File 'lib/factory_girl/evaluator.rb', line 56

def __overrides
  @overrides
end

#association(factory_name, overrides = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/factory_girl/evaluator.rb', line 32

def association(factory_name, overrides = {})
  build_strategy = if overrides.has_key?(:strategy)
                     overrides[:strategy]
                   else
                     Strategy::Create
                   end

  build_strategy = StrategyCalculator.new(build_strategy).strategy
  runner = FactoryRunner.new(factory_name, build_strategy, [overrides.except(:strategy)])
  @build_strategy.association(runner)
end

#instance=(object_instance) ⇒ Object



44
45
46
# File 'lib/factory_girl/evaluator.rb', line 44

def instance=(object_instance)
  @instance = object_instance
end