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)



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

def method_missing(method, *args, &block)
  if load_target
    if block_given?
      target.send(method, *args)  { |*block_args| block.call(*block_args) }
    else
      target.send(method, *args)
    end
  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



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

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

#blank?Boolean

Returns:



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

def blank?
  load_target
  target.blank?
end

#inspectObject



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

def inspect
  load_target
  target.inspect
end

#loadedObject



37
38
39
# File 'lib/mongo_mapper/plugins/associations/proxy.rb', line 37

def loaded
  @loaded = true
end

#loaded?Boolean

Returns:



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

def loaded?
  @loaded
end

#nil?Boolean

Returns:



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

def nil?
  load_target
  target.nil?
end

#present?Boolean

Returns:



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

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



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

def reload
  reset
  load_target
  self unless target.nil?
end

#replace(v) ⇒ Object

Raises:

  • (NotImplementedError)


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

def replace(v)
  raise NotImplementedError
end

#resetObject



66
67
68
69
# File 'lib/mongo_mapper/plugins/associations/proxy.rb', line 66

def reset
  @loaded = false
  @target = nil
end

#respond_to?(*args) ⇒ Boolean

Returns:



71
72
73
# File 'lib/mongo_mapper/plugins/associations/proxy.rb', line 71

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

#send(method, *args) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/mongo_mapper/plugins/associations/proxy.rb', line 75

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