Class: Opto::Model::Association::HasMany::Proxy
- Inherits:
-
Object
- Object
- Opto::Model::Association::HasMany::Proxy
- Extended by:
- Forwardable
- Defined in:
- lib/opto/model/association/has_many/proxy.rb
Instance Attribute Summary collapse
-
#as ⇒ Object
readonly
Returns the value of attribute as.
-
#members ⇒ Object
readonly
Returns the value of attribute members.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#target_class ⇒ Object
readonly
Returns the value of attribute target_class.
Instance Method Summary collapse
- #association_errors ⇒ Object
- #association_valid? ⇒ Boolean
- #errors ⇒ Object
-
#initialize(parent, target_class, as, options = {}) ⇒ Proxy
constructor
A new instance of Proxy.
- #method_missing(meth, *args) ⇒ Object
- #new(*args) ⇒ Object
- #respond_to_missing?(meth, include_private = false) ⇒ Boolean
- #to_h ⇒ Object
Constructor Details
#initialize(parent, target_class, as, options = {}) ⇒ Proxy
Returns a new instance of Proxy.
10 11 12 13 14 |
# File 'lib/opto/model/association/has_many/proxy.rb', line 10 def initialize(parent, target_class, as, = {}) @parent, @target_class, @as = parent, target_class, as @options = { key_is: :name }.merge() @members = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args) ⇒ Object
28 29 30 |
# File 'lib/opto/model/association/has_many/proxy.rb', line 28 def method_missing(meth, *args) members.send(meth, *args) end |
Instance Attribute Details
#as ⇒ Object (readonly)
Returns the value of attribute as.
6 7 8 |
# File 'lib/opto/model/association/has_many/proxy.rb', line 6 def as @as end |
#members ⇒ Object (readonly)
Returns the value of attribute members.
6 7 8 |
# File 'lib/opto/model/association/has_many/proxy.rb', line 6 def members @members end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/opto/model/association/has_many/proxy.rb', line 6 def @options end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
6 7 8 |
# File 'lib/opto/model/association/has_many/proxy.rb', line 6 def parent @parent end |
#target_class ⇒ Object (readonly)
Returns the value of attribute target_class.
6 7 8 |
# File 'lib/opto/model/association/has_many/proxy.rb', line 6 def target_class @target_class end |
Instance Method Details
#association_errors ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/opto/model/association/has_many/proxy.rb', line 16 def association_errors errors = { } if members.empty? && [:required] errors[:presence] = "Collection '#{as}' is empty" end errors end |
#association_valid? ⇒ Boolean
24 25 26 |
# File 'lib/opto/model/association/has_many/proxy.rb', line 24 def association_valid? association_errors.empty? end |
#errors ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/opto/model/association/has_many/proxy.rb', line 50 def errors result = { } each_with_index do |member, idx| errors = member.errors result[idx] = errors unless errors.empty? end result.merge!(association_errors || {}) result.empty? ? {} : { as => result } end |
#new(*args) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/opto/model/association/has_many/proxy.rb', line 36 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([:key_is] => key.kind_of?(Symbol) ? key.to_s : key)] end target = target_class.new(*args) members << target target end |
#respond_to_missing?(meth, include_private = false) ⇒ Boolean
32 33 34 |
# File 'lib/opto/model/association/has_many/proxy.rb', line 32 def respond_to_missing?(meth, include_private = false) members.respond_to?(meth, include_private) end |
#to_h ⇒ Object
46 47 48 |
# File 'lib/opto/model/association/has_many/proxy.rb', line 46 def to_h empty? ? {} : { as => map(&:to_h) } end |