Class: MongoMapper::Plugins::Associations::Proxy

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/mongo_mapper/plugins/associations/proxy/proxy.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner, association) ⇒ Proxy

Returns a new instance of Proxy.



27
28
29
30
31
# File 'lib/mongo_mapper/plugins/associations/proxy/proxy.rb', line 27

def initialize(owner, association)
  @proxy_owner, @association, @loaded = owner, association, false
  Array(association.options[:extend]).each { |ext| proxy_extend(ext) }
  reset
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/mongo_mapper/plugins/associations/proxy/proxy.rb', line 150

def method_missing(method, *args, &block)
  # load the target just in case it isn't loaded
  load_target

  # only define the method if the target has the method
  # NOTE: include private methods!
  if target.respond_to?(method, true)
    define_and_call_proxy_method(method, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#associationObject (readonly) Also known as: proxy_association

Returns the value of attribute association.



18
19
20
# File 'lib/mongo_mapper/plugins/associations/proxy/proxy.rb', line 18

def association
  @association
end

#proxy_ownerObject (readonly)

Returns the value of attribute proxy_owner.



18
19
20
# File 'lib/mongo_mapper/plugins/associations/proxy/proxy.rb', line 18

def proxy_owner
  @proxy_owner
end

#targetObject (readonly)

Returns the value of attribute target.



18
19
20
# File 'lib/mongo_mapper/plugins/associations/proxy/proxy.rb', line 18

def target
  @target
end

Class Method Details

.define_proxy_method(method) ⇒ Object



11
12
13
14
15
# File 'lib/mongo_mapper/plugins/associations/proxy/proxy.rb', line 11

def define_proxy_method(method)
  define_method(method) do |*args, &block|
    proxy_method(method, *args, &block)
  end
end

Instance Method Details

#inspectObject



55
56
57
58
# File 'lib/mongo_mapper/plugins/associations/proxy/proxy.rb', line 55

def inspect
  load_target
  "#<#{self.class.inspect}:#{object_id} #{@target.inspect}>"
end

#loadedObject



64
65
66
# File 'lib/mongo_mapper/plugins/associations/proxy/proxy.rb', line 64

def loaded
  @loaded = true
end

#loaded?Boolean

Returns:



60
61
62
# File 'lib/mongo_mapper/plugins/associations/proxy/proxy.rb', line 60

def loaded?
  @loaded
end

#proxy_method(method, *args, &block) ⇒ Object



99
100
101
102
# File 'lib/mongo_mapper/plugins/associations/proxy/proxy.rb', line 99

def proxy_method(method, *args, &block)
  load_target
  target.public_send(method, *args, &block)
end

#proxy_respond_to?Object



20
# File 'lib/mongo_mapper/plugins/associations/proxy/proxy.rb', line 20

alias_method :proxy_respond_to?, :respond_to?

#readObject



89
90
91
92
# File 'lib/mongo_mapper/plugins/associations/proxy/proxy.rb', line 89

def read
  load_target
  @target
end

#reloadObject



68
69
70
71
72
# File 'lib/mongo_mapper/plugins/associations/proxy/proxy.rb', line 68

def reload
  reset
  load_target
  self unless target.nil?
end

#replace(v) ⇒ Object

:nocov:

Raises:

  • (NotImplementedError)


75
76
77
# File 'lib/mongo_mapper/plugins/associations/proxy/proxy.rb', line 75

def replace(v)
  raise NotImplementedError
end

#resetObject

:nocov:



80
81
82
83
# File 'lib/mongo_mapper/plugins/associations/proxy/proxy.rb', line 80

def reset
  @loaded = false
  @target = nil
end

#respond_to?(*args) ⇒ Boolean

Returns:



85
86
87
# File 'lib/mongo_mapper/plugins/associations/proxy/proxy.rb', line 85

def respond_to?(*args)
  super || (load_target && target.respond_to?(*args))
end

#write(value) ⇒ Object



94
95
96
97
# File 'lib/mongo_mapper/plugins/associations/proxy/proxy.rb', line 94

def write(value)
  replace(value)
  read
end