class Opto::Model::Association::HasOne::Proxy

Attributes

as[R]
options[R]
parent[R]
target[R]
target_class[R]

Public Class Methods

new(parent, target_class, as, target = nil, options = {}) click to toggle source
# File lib/opto/model/association/has_one/proxy.rb, line 10
def initialize(parent, target_class, as, target = nil, options = {})
  @parent, @target_class, @as, @target = parent, target_class, as, target
  @options = { key_is: :name }.merge(options)
end

Public Instance Methods

association_errors() click to toggle source
# File lib/opto/model/association/has_one/proxy.rb, line 15
def association_errors
  errors = { }
  if target.nil? && options[:required]
    errors[:presence] = "Missing child '#{as}'"
  end
  errors
end
association_valid?() click to toggle source
# File lib/opto/model/association/has_one/proxy.rb, line 23
def association_valid?
  association_errors.empty?
end
errors() click to toggle source
# File lib/opto/model/association/has_one/proxy.rb, line 47
def errors
  result = {}
  if target
    target_errors = target.errors
  else
    target_errors = {}
  end
  assoc_errors = association_errors

  if target_errors.empty? && assoc_errors.empty?
    {}
  else
    { as => target_errors.merge(assoc_errors) }
  end
end
method_missing(meth, *args) click to toggle source
# File lib/opto/model/association/has_one/proxy.rb, line 27
def method_missing(meth, *args)
  target.send(meth, *args)
end
new(*args) click to toggle source
# File lib/opto/model/association/has_one/proxy.rb, line 35
def new(*args)
  if args.first.kind_of?(Hash) && args.size == 1 && args.first[args.first.keys.first].kind_of?(Hash)
    key = args.first.keys.first
    args = [args.first[key].merge(options[:key_is] => key.kind_of?(Symbol) ? key.to_s : key)]
  end
  @target = target_class.new(*args)
end
respond_to_missing?(meth, include_private = false) click to toggle source
# File lib/opto/model/association/has_one/proxy.rb, line 31
def respond_to_missing?(meth, include_private = false)
  target.respond_to?(meth, include_private)
end
to_h() click to toggle source
# File lib/opto/model/association/has_one/proxy.rb, line 43
def to_h
  target.nil? ? {} : { as => target.to_h }
end
valid?() click to toggle source
# File lib/opto/model/association/has_one/proxy.rb, line 63
def valid?
  association_valid? && (target && target.valid?)
end