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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner, association) ⇒ Proxy

Returns a new instance of Proxy.



22
23
24
25
26
# File 'lib/mongo_mapper/plugins/associations/proxy.rb', line 22

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 (protected)



107
108
109
110
111
# File 'lib/mongo_mapper/plugins/associations/proxy.rb', line 107

def method_missing(method, *args, &block)
  if load_target
    target.send(method, *args, &block)
  end
end

Instance Attribute Details

#associationObject (readonly) Also known as: proxy_association

Returns the value of attribute association.



14
15
16
# File 'lib/mongo_mapper/plugins/associations/proxy.rb', line 14

def association
  @association
end

#proxy_ownerObject (readonly)

Returns the value of attribute proxy_owner.



14
15
16
# File 'lib/mongo_mapper/plugins/associations/proxy.rb', line 14

def proxy_owner
  @proxy_owner
end

#targetObject (readonly) Also known as: proxy_target

Returns the value of attribute target.



14
15
16
# File 'lib/mongo_mapper/plugins/associations/proxy.rb', line 14

def target
  @target
end

Instance Method Details

#===(other) ⇒ Object



101
102
103
104
# File 'lib/mongo_mapper/plugins/associations/proxy.rb', line 101

def ===(other)
  load_target
  other === target
end

#as_json(*options) ⇒ Object

see comments to to_json



40
41
42
43
# File 'lib/mongo_mapper/plugins/associations/proxy.rb', line 40

def as_json(*options)
  load_target
  target.as_json(*options)
end

#blank?Boolean

Returns:



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

def blank?
  load_target
  target.blank?
end

#inspectObject



45
46
47
48
# File 'lib/mongo_mapper/plugins/associations/proxy.rb', line 45

def inspect
  load_target
  target.inspect
end

#loadedObject



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

def loaded
  @loaded = true
end

#loaded?Boolean

Returns:



50
51
52
# File 'lib/mongo_mapper/plugins/associations/proxy.rb', line 50

def loaded?
  @loaded
end

#nil?Boolean

Returns:



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

def nil?
  load_target
  target.nil?
end

#present?Boolean

Returns:



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

def present?
  load_target
  target.present?
end

#proxy_respond_to?Object



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

alias :proxy_respond_to? :respond_to?

#reloadObject



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

def reload
  reset
  load_target
  self unless target.nil?
end

#replace(v) ⇒ Object

Raises:

  • (NotImplementedError)


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

def replace(v)
  raise NotImplementedError
end

#resetObject



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

def reset
  @loaded = false
  @target = nil
end

#respond_to?(*args) ⇒ Boolean

Returns:



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

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

#send(method, *args) ⇒ Object



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

def send(method, *args)
  if proxy_respond_to?(method)
    super
  else
    load_target
    target.send(method, *args)
  end
end

#to_json(*options) ⇒ Object

Active support in rails 3 beta 4 can override to_json after this is loaded, at least when run in mongomapper tests. The implementation was changed in master some time after this, so not sure whether this is still a problem.

In rails 2, this isn’t a problem however it also solves an issue where to_json isn’t forwarded because it supports to_json itself



34
35
36
37
# File 'lib/mongo_mapper/plugins/associations/proxy.rb', line 34

def to_json(*options)
  load_target
  target.to_json(*options)
end