Class: Opto::Model::Association::HasOne::Proxy
- Inherits:
-
Object
- Object
- Opto::Model::Association::HasOne::Proxy
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
|
# 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, @options = parent, target_class, as, target, options
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args) ⇒ Object
26
27
28
|
# File 'lib/opto/model/association/has_one/proxy.rb', line 26
def method_missing(meth, *args)
target.send(meth, *args)
end
|
Instance Attribute Details
#as ⇒ Object
Returns the value of attribute as.
6
7
8
|
# File 'lib/opto/model/association/has_one/proxy.rb', line 6
def as
@as
end
|
#options ⇒ Object
Returns the value of attribute options.
6
7
8
|
# File 'lib/opto/model/association/has_one/proxy.rb', line 6
def options
@options
end
|
#parent ⇒ Object
Returns the value of attribute parent.
6
7
8
|
# File 'lib/opto/model/association/has_one/proxy.rb', line 6
def parent
@parent
end
|
#target ⇒ Object
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_class ⇒ Object
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_errors ⇒ Object
14
15
16
17
18
19
20
|
# File 'lib/opto/model/association/has_one/proxy.rb', line 14
def association_errors
errors = { }
if target.nil? && options[:required]
errors[:presence] = "Missing child '#{as}'"
end
errors
end
|
#association_valid? ⇒ Boolean
22
23
24
|
# File 'lib/opto/model/association/has_one/proxy.rb', line 22
def association_valid?
association_errors.empty?
end
|
#errors ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/opto/model/association/has_one/proxy.rb', line 42
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
34
35
36
|
# File 'lib/opto/model/association/has_one/proxy.rb', line 34
def new(*args)
@target = target_class.new(*args)
end
|
#respond_to_missing?(meth, include_private = false) ⇒ Boolean
30
31
32
|
# File 'lib/opto/model/association/has_one/proxy.rb', line 30
def respond_to_missing?(meth, include_private = false)
target.respond_to?(meth, include_private)
end
|
#to_h ⇒ Object
38
39
40
|
# File 'lib/opto/model/association/has_one/proxy.rb', line 38
def to_h
target.nil? ? {} : { as => target.to_h }
end
|
#valid? ⇒ Boolean
58
59
60
|
# File 'lib/opto/model/association/has_one/proxy.rb', line 58
def valid?
association_valid? && (target && target.valid?)
end
|