Class: FactoryGirl::Proxy::Build

Inherits:
FactoryGirl::Proxy show all
Defined in:
lib/factory_girl/proxy/build.rb

Overview

:nodoc:

Direct Known Subclasses

Create

Instance Attribute Summary

Attributes inherited from FactoryGirl::Proxy

#callbacks

Instance Method Summary collapse

Methods inherited from FactoryGirl::Proxy

#add_callback, #method_missing, #run_callbacks, #set_ignored

Constructor Details

#initialize(klass) ⇒ Build

Returns a new instance of Build.



4
5
6
7
# File 'lib/factory_girl/proxy/build.rb', line 4

def initialize(klass)
  super(klass)
  @instance = klass.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class FactoryGirl::Proxy

Instance Method Details

#associate(name, factory_name, overrides) ⇒ Object



21
22
23
24
25
# File 'lib/factory_girl/proxy/build.rb', line 21

def associate(name, factory_name, overrides)
  method = get_method(overrides[:method])
  factory = FactoryGirl.factory_by_name(factory_name)
  set(name, factory.run(method, remove_method(overrides)))
end

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



27
28
29
30
31
# File 'lib/factory_girl/proxy/build.rb', line 27

def association(factory_name, overrides = {})
  method = get_method(overrides[:method])
  factory = FactoryGirl.factory_by_name(factory_name)
  factory.run(method, remove_method(overrides))
end

#get(attribute) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/factory_girl/proxy/build.rb', line 9

def get(attribute)
  if @ignored_attributes.has_key?(attribute)
    @ignored_attributes[attribute]
  else
    @instance.send(attribute)
  end
end

#parse_method(method) ⇒ Object Also known as: get_method



42
43
44
45
46
47
48
49
50
51
# File 'lib/factory_girl/proxy/build.rb', line 42

def parse_method(method)
  method ||= :create
  if :build == method
    return Proxy::Build
  elsif :create == method
    return Proxy::Create
  else
    raise "unrecognized method #{method}"
  end
end

#remove_method(overrides) ⇒ Object



33
34
35
# File 'lib/factory_girl/proxy/build.rb', line 33

def remove_method(overrides)
  overrides.dup.delete_if {|key, value| key == :method}
end

#result(to_create) ⇒ Object



37
38
39
40
# File 'lib/factory_girl/proxy/build.rb', line 37

def result(to_create)
  run_callbacks(:after_build)
  @instance
end

#set(attribute, value) ⇒ Object



17
18
19
# File 'lib/factory_girl/proxy/build.rb', line 17

def set(attribute, value)
  @instance.send(:"#{attribute}=", value)
end