Class: FactoryGirl::Evaluator

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Evaluator.



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

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

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

def method_missing(method_name, *args, &block)
  if @cached_attributes.key?(method_name)
    @cached_attributes[method_name]
  else
    if @instance.respond_to?(method_name)
      @instance.send(method_name, *args, &block)
    else
      SyntaxRunner.new.send(method_name, *args, &block)
    end
  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

#__override_names__Object



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

def __override_names__
  @overrides.keys
end

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



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

def association(factory_name, overrides = {})
  strategy_override = overrides.fetch(:strategy) { FactoryGirl.strategy_by_name(:create) }

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

#instance=(object_instance) ⇒ Object



41
42
43
# File 'lib/factory_girl/evaluator.rb', line 41

def instance=(object_instance)
  @instance = object_instance
end