class NinjaModel::Associations::AssociationProxy

Public Class Methods

new(owner, reflection) click to toggle source
# 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

Public Instance Methods

===(other) click to toggle source
# File lib/ninja_model/associations/association_proxy.rb, line 34
def ===(other)
  load_target
  other === @target
end
inspect() click to toggle source
# File lib/ninja_model/associations/association_proxy.rb, line 67
def inspect
  load_target
  @target.inspect
end
loaded() click to toggle source
# File lib/ninja_model/associations/association_proxy.rb, line 54
def loaded
  @loaded = true
end
loaded?() click to toggle source
# File lib/ninja_model/associations/association_proxy.rb, line 50
def loaded?
  @loaded
end
proxy_owner() click to toggle source
# File lib/ninja_model/associations/association_proxy.rb, line 18
def proxy_owner
  @owner
end
proxy_reflection() click to toggle source
# File lib/ninja_model/associations/association_proxy.rb, line 22
def proxy_reflection
  @reflection
end
proxy_respond_to?(*args)
Alias for: respond_to?
proxy_target() click to toggle source
# File lib/ninja_model/associations/association_proxy.rb, line 26
def proxy_target
  @target
end
reload() click to toggle source
# File lib/ninja_model/associations/association_proxy.rb, line 39
def reload
  reset
  load_target
  self unless @target.nil?
end
reset() click to toggle source
# File lib/ninja_model/associations/association_proxy.rb, line 45
def reset
  @loaded = false
  @target = nil
end
respond_to?(*args) click to toggle source
# 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
Also aliased as: proxy_respond_to?
send(method, *args) click to toggle source
Calls superclass method
# 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
target() click to toggle source
# File lib/ninja_model/associations/association_proxy.rb, line 58
def target
  @target
end
target=(target) click to toggle source
# File lib/ninja_model/associations/association_proxy.rb, line 62
def target=(target)
  @target = target
  loaded
end

Protected Instance Methods

dependent?() click to toggle source
# File lib/ninja_model/associations/association_proxy.rb, line 83
def dependent?
  @reflection.options[:dependent]
end
with_scope(*args, &block) click to toggle source
# File lib/ninja_model/associations/association_proxy.rb, line 87
def with_scope(*args, &block)
  @reflection.klass.send :with_scope, *args, &block
end

Private Instance Methods

foreign_key_present() click to toggle source
# File lib/ninja_model/associations/association_proxy.rb, line 121
def foreign_key_present
  false
end
load_target() click to toggle source
# File lib/ninja_model/associations/association_proxy.rb, line 108
def load_target
  return nil unless defined?(@loaded)

  if !loaded? and (@owner.persisted? || foreign_key_present)
    @target = find_target
  end

  @loaded = true
  @target
rescue NinjaModel::RecordNotFound
  reset
end
method_missing(method, *args) { |*block_args| ... } click to toggle source
# 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