Class: Opto::Model::Association::HasOne::Proxy

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/opto/model/association/has_one/proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, target_class, as, target = nil, options = {}) ⇒ Proxy

Returns a new instance of Proxy.



10
11
12
13
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



27
28
29
# File 'lib/opto/model/association/has_one/proxy.rb', line 27

def method_missing(meth, *args)
  target.send(meth, *args)
end

Instance Attribute Details

#asObject (readonly)

Returns the value of attribute as.



6
7
8
# File 'lib/opto/model/association/has_one/proxy.rb', line 6

def as
  @as
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/opto/model/association/has_one/proxy.rb', line 6

def options
  @options
end

#parentObject (readonly)

Returns the value of attribute parent.



6
7
8
# File 'lib/opto/model/association/has_one/proxy.rb', line 6

def parent
  @parent
end

#targetObject (readonly)

Returns the value of attribute target.



6
7
8
# File 'lib/opto/model/association/has_one/proxy.rb', line 6

def target
  @target
end

#target_classObject (readonly)

Returns the value of attribute target_class.



6
7
8
# File 'lib/opto/model/association/has_one/proxy.rb', line 6

def target_class
  @target_class
end

Instance Method Details

#association_errorsObject



15
16
17
18
19
20
21
# 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?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/opto/model/association/has_one/proxy.rb', line 23

def association_valid?
  association_errors.empty?
end

#errorsObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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

#new(*args) ⇒ Object



35
36
37
38
39
40
41
# 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) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# 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_hObject



43
44
45
# File 'lib/opto/model/association/has_one/proxy.rb', line 43

def to_h
  target.nil? ? {} : { as => target.to_h }
end

#valid?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/opto/model/association/has_one/proxy.rb', line 63

def valid?
  association_valid? && (target && target.valid?)
end