class MarkMapper::Plugins::Associations::Proxy

Attributes

association[R]
proxy_association[R]
proxy_owner[R]
proxy_target[R]
target[R]

Public Class Methods

new(owner, association) click to toggle source
# File lib/mark_mapper/plugins/associations/proxy.rb, line 22
def initialize(owner, association)
  @proxy_owner, @association, @loaded = owner, association, false
  Array(association.options[:extend]).each { |ext| proxy_extend(ext) }
  reset
end

Public Instance Methods

as_json(*options) click to toggle source

see comments to to_json

# File lib/mark_mapper/plugins/associations/proxy.rb, line 40
def as_json(*options)
  load_target
  target.as_json(*options)
end
blank?() click to toggle source
# File lib/mark_mapper/plugins/associations/proxy.rb, line 63
def blank?
  load_target
  target.blank?
end
inspect() click to toggle source
# File lib/mark_mapper/plugins/associations/proxy.rb, line 45
def inspect
  load_target
  target.inspect
end
loaded() click to toggle source
# File lib/mark_mapper/plugins/associations/proxy.rb, line 54
def loaded
  @loaded = true
end
loaded?() click to toggle source
# File lib/mark_mapper/plugins/associations/proxy.rb, line 50
def loaded?
  @loaded
end
nil?() click to toggle source
# File lib/mark_mapper/plugins/associations/proxy.rb, line 58
def nil?
  load_target
  target.nil?
end
present?() click to toggle source
# File lib/mark_mapper/plugins/associations/proxy.rb, line 68
def present?
  load_target
  target.present?
end
proxy_respond_to?(*args)
Alias for: respond_to?
reload() click to toggle source
# File lib/mark_mapper/plugins/associations/proxy.rb, line 73
def reload
  reset
  load_target
  self unless target.nil?
end
replace(v) click to toggle source

:nocov:

# File lib/mark_mapper/plugins/associations/proxy.rb, line 80
def replace(v)
  raise NotImplementedError
end
reset() click to toggle source

:nocov:

# File lib/mark_mapper/plugins/associations/proxy.rb, line 85
def reset
  @loaded = false
  @target = nil
end
respond_to?(*args) click to toggle source
# File lib/mark_mapper/plugins/associations/proxy.rb, line 90
def respond_to?(*args)
  proxy_respond_to?(*args) || (load_target && target.respond_to?(*args))
end
Also aliased as: proxy_respond_to?
send(method, *args, &block) click to toggle source
Calls superclass method
# File lib/mark_mapper/plugins/associations/proxy.rb, line 94
def send(method, *args, &block)
  if proxy_respond_to?(method, true)
    super
  else
    load_target
    target.send(method, *args, &block)
  end
end
to_json(*options) click to toggle source

Active support in rails 3 beta 4 can override to_json after this is loaded, at least when run in markmapper tests. The implementation was changed in master some time after this, so not sure whether this is still a problem.

In rails 2, this isn’t a problem however it also solves an issue where to_json isn’t forwarded because it supports to_json itself

# File lib/mark_mapper/plugins/associations/proxy.rb, line 34
def to_json(*options)
  load_target
  target.to_json(*options)
end

Protected Instance Methods

find_target() click to toggle source

:nocov:

# File lib/mark_mapper/plugins/associations/proxy.rb, line 125
def find_target
  raise NotImplementedError
end
flatten_deeper(array) click to toggle source

:nocov:

# File lib/mark_mapper/plugins/associations/proxy.rb, line 130
def flatten_deeper(array)
  array.collect do |element|
    (element.respond_to?(:flatten) && !element.is_a?(Hash)) ? element.flatten : element
  end.flatten
end
load_target() click to toggle source
# File lib/mark_mapper/plugins/associations/proxy.rb, line 110
def load_target
  unless loaded?
    if @target.is_a?(Array) && @target.any?
      @target = find_target + @target.find_all { |record| !record.persisted? }
    else
      @target = find_target
    end
    loaded
  end
  @target
rescue MarkMapper::DocumentNotFound
  reset
end
method_missing(method, *args, &block) click to toggle source
# File lib/mark_mapper/plugins/associations/proxy.rb, line 104
def method_missing(method, *args, &block)
  if load_target
    target.send(method, *args, &block)
  end
end