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

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
# File 'lib/factory_girl/proxy/stub.rb', line 6

def initialize(klass)
  @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



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

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

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



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

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

#get(attribute) ⇒ Object



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

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

#next_idObject



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

def next_id
  @@next_id += 1
end

#resultObject



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

def result
  run_callbacks(:after_stub)
  @instance
end

#set(attribute, value) ⇒ Object



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

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