Class: FactoryGirl::Proxy::Stub

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

Overview

:nodoc:

Constant Summary collapse

@@next_id =
1000

Instance Method Summary collapse

Methods inherited from FactoryGirl::Proxy

ensure_strategy_exists!, #method_missing, #run_callbacks, #set_ignored

Constructor Details

#initialize(klass, callbacks = []) ⇒ 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
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/factory_girl/proxy/stub.rb', line 6

def initialize(klass, callbacks = [])
  super
  @instance = klass.new
  @instance.id = next_id
  @instance.instance_eval do
    def persisted?
      !new_record?
    end

    def created_at
      @created_at ||= Time.now
    end

    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

    def update_attribute(*args)
      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 FactoryGirl::Proxy

Instance Method Details

#associate(name, factory_name, overrides) ⇒ Object



61
62
63
# File 'lib/factory_girl/proxy/stub.rb', line 61

def associate(name, factory_name, overrides)
  set(name, association(factory_name, overrides))
end

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



65
66
67
68
# File 'lib/factory_girl/proxy/stub.rb', line 65

def association(factory_name, overrides = {})
  factory = FactoryGirl.factory_by_name(factory_name)
  factory.run(Proxy::Stub, overrides.except(:method))
end

#get(attribute) ⇒ Object



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

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

#next_idObject



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

def next_id
  @@next_id += 1
end

#result(to_create) ⇒ Object



70
71
72
73
# File 'lib/factory_girl/proxy/stub.rb', line 70

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

#set(attribute, value) ⇒ Object



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

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