Class: NinjaModel::Associations::AssociationProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/ninja_model/associations/association_proxy.rb

Instance Method Summary collapse

Constructor Details

#initialize(owner, reflection) ⇒ AssociationProxy

Returns a new instance of AssociationProxy.



11
12
13
14
15
16
# File 'lib/ninja_model/associations/association_proxy.rb', line 11

def initialize(owner, reflection)
  @owner, @reflection = owner, reflection
  @updated = false
  Array.wrap(reflection.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) ⇒ Object (private)



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/ninja_model/associations/association_proxy.rb', line 93

def method_missing(method, *args)
  if load_target
    unless @target.respond_to?(method)
      message = "undefined method '#{method.to_s}' for \"#{@target}\":#{@target.class.to_s}"
      raise NoMethodError, message
    end

    if block_given?
      @target.send(method, *args) { |*block_args| yield(*block_args) }
    else
      @target.send(method, *args)
    end
  end
end

Instance Method Details

#===(other) ⇒ Object



34
35
36
37
# File 'lib/ninja_model/associations/association_proxy.rb', line 34

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

#inspectObject



67
68
69
70
# File 'lib/ninja_model/associations/association_proxy.rb', line 67

def inspect
  load_target
  @target.inspect
end

#loadedObject



54
55
56
# File 'lib/ninja_model/associations/association_proxy.rb', line 54

def loaded
  @loaded = true
end

#loaded?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/ninja_model/associations/association_proxy.rb', line 50

def loaded?
  @loaded
end

#proxy_ownerObject



18
19
20
# File 'lib/ninja_model/associations/association_proxy.rb', line 18

def proxy_owner
  @owner
end

#proxy_reflectionObject



22
23
24
# File 'lib/ninja_model/associations/association_proxy.rb', line 22

def proxy_reflection
  @reflection
end

#proxy_respond_to?Object



6
# File 'lib/ninja_model/associations/association_proxy.rb', line 6

alias_method :proxy_respond_to?, :respond_to?

#proxy_targetObject



26
27
28
# File 'lib/ninja_model/associations/association_proxy.rb', line 26

def proxy_target
  @target
end

#reloadObject



39
40
41
42
43
# File 'lib/ninja_model/associations/association_proxy.rb', line 39

def reload
  reset
  load_target
  self unless @target.nil?
end

#resetObject



45
46
47
48
# File 'lib/ninja_model/associations/association_proxy.rb', line 45

def reset
  @loaded = false
  @target = nil
end

#respond_to?(*args) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/ninja_model/associations/association_proxy.rb', line 30

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

#send(method, *args) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/ninja_model/associations/association_proxy.rb', line 72

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

#targetObject



58
59
60
# File 'lib/ninja_model/associations/association_proxy.rb', line 58

def target
  @target
end

#target=(target) ⇒ Object



62
63
64
65
# File 'lib/ninja_model/associations/association_proxy.rb', line 62

def target=(target)
  @target = target
  loaded
end