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

Inherits:
Object
  • Object
show all
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.



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

def initialize(owner, association)
  @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)



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

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.



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

def association
  @association
end

#ownerObject (readonly) Also known as: proxy_owner

Returns the value of attribute owner.



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

def owner
  @owner
end

#targetObject (readonly) Also known as: proxy_target

Returns the value of attribute target.



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

def target
  @target
end

Instance Method Details

#===(other) ⇒ Object



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

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

#blank?Boolean

Returns:



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

def blank?
  load_target
  target.blank?
end

#inspectObject



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

def inspect
  load_target
  target.inspect
end

#loadedObject



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

def loaded
  @loaded = true
end

#loaded?Boolean

Returns:



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

def loaded?
  @loaded
end

#nil?Boolean

Returns:



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

def nil?
  load_target
  target.nil?
end

#present?Boolean

Returns:



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

def present?
  load_target
  target.present?
end

#proxy_respond_to?Object



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

alias :proxy_respond_to? :respond_to?

#reloadObject



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

def reload
  reset
  load_target
  self unless target.nil?
end

#replace(v) ⇒ Object

Raises:

  • (NotImplementedError)


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

def replace(v)
  raise NotImplementedError
end

#resetObject



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

def reset
  @loaded = false
  @target = nil
end

#respond_to?(*args) ⇒ Boolean

Returns:



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

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

#send(method, *args) ⇒ Object



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

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