Class: MassiveRecord::ORM::Relations::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/massive_record/orm/relations/proxy.rb,
lib/massive_record/orm/relations/proxy/embedded_in.rb,
lib/massive_record/orm/relations/proxy/embeds_many.rb,
lib/massive_record/orm/relations/proxy/references_one.rb,
lib/massive_record/orm/relations/proxy/references_many.rb,
lib/massive_record/orm/relations/proxy/embedded_in_polymorphic.rb,
lib/massive_record/orm/relations/proxy/references_one_polymorphic.rb

Overview

Parent class for all proxies sitting between records. It’s responsibility is to transparently load and forward method calls to it’s proxy_target. Iy may also do some small maintenance work like setting foreign key in proxy_owner object etc. That kind of functionality is likely to be implemented in one of it’s more specific sub class proxies.

Defined Under Namespace

Classes: EmbeddedIn, EmbeddedInPolymorphic, EmbedsMany, ReferencesMany, ReferencesOne, ReferencesOnePolymorphic

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Proxy

Returns a new instance of Proxy.



20
21
22
23
24
25
26
27
# File 'lib/massive_record/orm/relations/proxy.rb', line 20

def initialize(options = {})
  options.to_options!
  self. = options[:metadata]
  self.proxy_owner = options[:proxy_owner]
  self.proxy_target = options[:proxy_target]

  reset if proxy_target.nil?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



95
96
97
98
99
100
# File 'lib/massive_record/orm/relations/proxy.rb', line 95

def method_missing(method, *args, &block)
  return proxy_target.send(method, *args, &block) if load_proxy_target && proxy_target.respond_to?(method)
  super
rescue NoMethodError => e
  raise e, e.message.sub(/ for #<.*$/, " via proxy for #{proxy_target}")
end

Instance Attribute Details

#metadataObject

Returns the value of attribute metadata.



16
17
18
# File 'lib/massive_record/orm/relations/proxy.rb', line 16

def 
  @metadata
end

#proxy_ownerObject

Returns the value of attribute proxy_owner.



16
17
18
# File 'lib/massive_record/orm/relations/proxy.rb', line 16

def proxy_owner
  @proxy_owner
end

#proxy_targetObject

Returns the value of attribute proxy_target.



15
16
17
# File 'lib/massive_record/orm/relations/proxy.rb', line 15

def proxy_target
  @proxy_target
end

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/massive_record/orm/relations/proxy.rb', line 103

def blank?
  load_proxy_target.blank?
end

#inspectObject



74
75
76
# File 'lib/massive_record/orm/relations/proxy.rb', line 74

def inspect
  load_proxy_target.inspect
end

#is_a?(type) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/massive_record/orm/relations/proxy.rb', line 107

def is_a?(type)
  load_proxy_target.is_a?(type)
end

#load_proxy_target(options = {}) ⇒ Object

Returns the proxy_target. Loads it, if it’s not there. Returns nil if for some reason proxy_target could not be found.



49
50
51
52
53
54
# File 'lib/massive_record/orm/relations/proxy.rb', line 49

def load_proxy_target(options = {})
  self.proxy_target = find_proxy_target_or_find_with_proc(options) if find_proxy_target?
  proxy_target
rescue RecordNotFound
  reset
end

#loaded!Object



87
88
89
# File 'lib/massive_record/orm/relations/proxy.rb', line 87

def loaded!
  @loaded = true
end

#loaded?Boolean

If the proxy is loaded it has a proxy_target

Returns:

  • (Boolean)


83
84
85
# File 'lib/massive_record/orm/relations/proxy.rb', line 83

def loaded?
  !!@loaded
end

#reloadObject



56
57
58
59
# File 'lib/massive_record/orm/relations/proxy.rb', line 56

def reload
  reset
  load_proxy_target
end

#replace(proxy_target) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/massive_record/orm/relations/proxy.rb', line 65

def replace(proxy_target)
  if proxy_target.nil?
    reset(true)
  else
    raise_if_type_mismatch(proxy_target)
    self.proxy_target = proxy_target
  end
end

#reset(force = true) ⇒ Object



61
62
63
# File 'lib/massive_record/orm/relations/proxy.rb', line 61

def reset(force = true)
  @loaded = @proxy_target = nil
end

#respond_to?(*args) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/massive_record/orm/relations/proxy.rb', line 91

def respond_to?(*args)
  super || (load_proxy_target && proxy_target.respond_to?(*args))
end

#to_paramObject

Strange.. Without Rails, to_param goes through method_missing,

With Rails it seems like the proxy answered to to_param, which
kinda was not what I wanted.


114
115
116
# File 'lib/massive_record/orm/relations/proxy.rb', line 114

def to_param # :nodoc:
  proxy_target.try :to_param
end