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
23
24
25
26
27
28
29
30
# 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 save(*args)
      raise "stubbed models are not allowed to access the database"
    end

    def destroy(*args)
      raise "stubbed models are not allowed to access the database"
    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



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

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

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



48
49
50
# File 'lib/factory_girl/proxy/stub.rb', line 48

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

#get(attribute) ⇒ Object



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

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

#next_idObject



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

def next_id
  @@next_id += 1
end

#resultObject



52
53
54
55
# File 'lib/factory_girl/proxy/stub.rb', line 52

def result
  run_callbacks(:after_stub)
  @instance
end

#set(attribute, value) ⇒ Object



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

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