Class: Factory::Proxy::Stub

Inherits:
Factory::Proxy show all
Defined in:
lib/factory_girl/proxy/stub.rb

Overview

:nodoc:

Constant Summary collapse

@@next_id =
1000

Instance Attribute Summary

Attributes inherited from Factory::Proxy

#callbacks

Instance Method Summary collapse

Methods inherited from Factory::Proxy

#add_callback, #method_missing, #run_callbacks, #set_param

Constructor Details

#initialize(klass) ⇒ Stub

Returns a new instance of Stub.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/factory_girl/proxy/stub.rb', line 6

def initialize(klass)
  super
  @instance = klass.new
  @instance.id = next_id
  @instance.instance_eval do
    def new_record?
      id.nil?
    end

    def connection
      raise "stubbed models are not allowed to access the database"
    end

    def reload
      raise "stubbed models are not allowed to access the database"
    end
  end
end

Dynamic Method Handling

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

Instance Method Details

#associate(name, factory, attributes) ⇒ Object



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

def associate(name, factory, attributes)
  set(name, Factory.stub(factory, attributes))
end

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



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

def association(factory, overrides = {})
  Factory.stub(factory, overrides)
end

#get(attribute) ⇒ Object



29
30
31
# File 'lib/factory_girl/proxy/stub.rb', line 29

def get(attribute)
  @instance.send(attribute)
end

#next_idObject



25
26
27
# File 'lib/factory_girl/proxy/stub.rb', line 25

def next_id
  @@next_id += 1
end

#resultObject



45
46
47
48
# File 'lib/factory_girl/proxy/stub.rb', line 45

def result
  run_callbacks(:after_stub)
  @instance
end

#set(attribute, value) ⇒ Object



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

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