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.



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

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)



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

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.



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

def association
  @association
end

#ownerObject (readonly) Also known as: proxy_owner

Returns the value of attribute owner.



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

def owner
  @owner
end

#targetObject (readonly) Also known as: proxy_target

Returns the value of attribute target.



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

def target
  @target
end

Instance Method Details

#===(other) ⇒ Object



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

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

#blank?Boolean

Returns:



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

def blank?
  load_target
  target.blank?
end

#inspectObject



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

def inspect
  load_target
  target.inspect
end

#loadedObject



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

def loaded
  @loaded = true
end

#loaded?Boolean

Returns:



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

def loaded?
  @loaded
end

#nil?Boolean

Returns:



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

def nil?
  load_target
  target.nil?
end

#present?Boolean

Returns:



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

def present?
  load_target
  target.present?
end

#proxy_respond_to?Object



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

alias :proxy_respond_to? :respond_to?

#reloadObject



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

def reload
  reset
  load_target
  self unless target.nil?
end

#replace(v) ⇒ Object

Raises:

  • (NotImplementedError)


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

def replace(v)
  raise NotImplementedError
end

#resetObject



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

def reset
  @loaded = false
  @target = nil
end

#respond_to?(*args) ⇒ Boolean

Returns:



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

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

#send(method, *args) ⇒ Object



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

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