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



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)



104
105
106
107
108
# File 'lib/mongo_mapper/plugins/associations/proxy.rb', line 104

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

#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



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



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

def loaded?
  @loaded
end

#nil?Boolean



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

def nil?
  load_target
  target.nil?
end

#present?Boolean



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

:nocov:

Raises:

  • (NotImplementedError)


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

def replace(v)
  raise NotImplementedError
end

#resetObject

:nocov:



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

def reset
  @loaded = false
  @target = nil
end

#respond_to?(*args) ⇒ Boolean



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

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

#send(method, *args) ⇒ Object



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

def send(method, *args)
  if proxy_respond_to?(method, true)
    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