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

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

Direct Known Subclasses

BelongsToPolymorphicProxy, BelongsToProxy, Collection, OneEmbeddedProxy, OneProxy

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Proxy) initialize(owner, association)



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

- (Object) method_missing(method, *args, &block) (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

- (Object) association (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

- (Object) proxy_owner (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

- (Object) target (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

- (Object) as_json(*options)

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

- (Boolean) blank?



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

def blank?
  load_target
  target.blank?
end

- (Object) inspect



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

def inspect
  load_target
  target.inspect
end

- (Object) loaded



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

def loaded
  @loaded = true
end

- (Boolean) loaded?



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

def loaded?
  @loaded
end

- (Boolean) nil?



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

def nil?
  load_target
  target.nil?
end

- (Boolean) present?



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

def present?
  load_target
  target.present?
end

- (Object) proxy_respond_to?



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

alias :proxy_respond_to? :respond_to?

- (Object) reload



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

- (Object) replace(v)

:nocov:

Raises:

  • (NotImplementedError)


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

def replace(v)
  raise NotImplementedError
end

- (Object) reset

:nocov:



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

def reset
  @loaded = false
  @target = nil
end

- (Boolean) respond_to?(*args)



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

- (Object) send(method, *args)



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

- (Object) to_json(*options)

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