Class: Stannum::Associations::Many::Proxy
- Inherits:
-
Object
- Object
- Stannum::Associations::Many::Proxy
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/stannum/associations/many.rb
Overview
Wrapper object for an entity’s plural association.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
True if the object has matching data; otherwise false.
-
#[](index) ⇒ Stannum::Entity
Retrieves the value of the association at the specified index.
-
#add(value) ⇒ self
(also: #<<, #push)
Appends the entity to the association.
- #each(&block) ⇒ Object
-
#empty? ⇒ true, false
True if the association has no items; otherwise false.
-
#initialize(association:, entity:) ⇒ Proxy
constructor
A new instance of Proxy.
-
#inspect ⇒ String
A human-readable string representation of the object.
-
#remove(value) ⇒ self
(also: #delete)
Removes the entity from the association.
Constructor Details
#initialize(association:, entity:) ⇒ Proxy
Returns a new instance of Proxy.
19 20 21 22 |
# File 'lib/stannum/associations/many.rb', line 19 def initialize(association:, entity:) @association = association @entity = entity end |
Instance Method Details
#==(other) ⇒ true, false
Returns true if the object has matching data; otherwise false.
56 57 58 59 60 61 62 |
# File 'lib/stannum/associations/many.rb', line 56 def ==(other) return false if other.nil? return false unless other.respond_to?(:to_a) to_a == other.to_a end |
#[](index) ⇒ Stannum::Entity
Retrieves the value of the association at the specified index.
30 |
# File 'lib/stannum/associations/many.rb', line 30 def_delegator :data, :[] |
#add(value) ⇒ self Also known as: <<, push
Appends the entity to the association.
If the entity is already in the association data, this method does nothing.
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/stannum/associations/many.rb', line 72 def add(value) unless value.is_a?(association.resolved_type) = 'invalid association item - must be an instance of ' \ "#{association.resolved_type.name}" raise ArgumentError, end association.add_value(entity, value) self end |
#each ⇒ Enumerator #each {|item| ... } ⇒ Object
40 |
# File 'lib/stannum/associations/many.rb', line 40 def_delegator :data, :each |
#empty? ⇒ true, false
Returns true if the association has no items; otherwise false.
45 |
# File 'lib/stannum/associations/many.rb', line 45 def_delegator :data, :empty? |
#inspect ⇒ String
Returns a human-readable string representation of the object.
89 90 91 |
# File 'lib/stannum/associations/many.rb', line 89 def inspect "#{super[...55]} data=[#{each.map(&:inspect).join(', ')}]>" end |
#remove(value) ⇒ self Also known as: delete
Removes the entity from the association.
If the entity is not in the association data, this method does nothing.
100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/stannum/associations/many.rb', line 100 def remove(value) unless value.is_a?(association.resolved_type) = 'invalid association item - must be an instance of ' \ "#{association.resolved_type.name}" raise ArgumentError, end association.remove_value(entity, value) self end |