Class: Stannum::Associations::Many
- Inherits:
-
Stannum::Association
- Object
- Stannum::Association
- Stannum::Associations::Many
- Defined in:
- lib/stannum/associations/many.rb
Overview
Data object representing a plural association.
Defined Under Namespace
Classes: Proxy
Instance Attribute Summary
Attributes inherited from Stannum::Association
Instance Method Summary collapse
-
#add_value(entity, value, update_inverse: true) ⇒ void
private
Adds the given value to the association for the entity.
-
#clear_value(entity, update_inverse: true) ⇒ void
private
Removes the value of the association for the entity.
-
#get_value(entity) ⇒ Object
private
Retrieves the value of the association for the entity.
-
#many? ⇒ true
True if the association is a plural association; otherwise false.
-
#remove_value(entity, value, update_inverse: true) ⇒ void
private
Removes the given value from the association for the entity.
-
#resolved_inverse ⇒ Stannum::Association
The inverse association, if any.
-
#set_value(entity, value, update_inverse: true) ⇒ void
private
Replaces the association for the entity with the given value.
Methods inherited from Stannum::Association
#entity_class_name, #foreign_key?, #foreign_key_name, #foreign_key_type, #initialize, #inverse?, #inverse_name, #one?, #reader_name, #resolved_type, #writer_name
Constructor Details
This class inherits a constructor from Stannum::Association
Instance Method Details
#add_value(entity, value, update_inverse: true) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Adds the given value to the association for the entity.
127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/stannum/associations/many.rb', line 127 def add_value(entity, value, update_inverse: true) return unless value data = entity.read_association(name, safe: false) || [] data, changed = add_item(data:, value:) entity.write_association(name, data, safe: false) if changed update_item_inverse(entity:, value:) if inverse? && update_inverse nil end |
#clear_value(entity, update_inverse: true) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Removes the value of the association for the entity.
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/stannum/associations/many.rb', line 142 def clear_value(entity, update_inverse: true) data = entity.read_association(name, safe: false) || [] entity.write_association(name, [], safe: false) if inverse? && update_inverse data.each { |item| remove_item_inverse(value: item) } end nil end |
#get_value(entity) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Retrieves the value of the association for the entity.
155 156 157 |
# File 'lib/stannum/associations/many.rb', line 155 def get_value(entity) entity.association_proxy_for(self) end |
#many? ⇒ true
Returns true if the association is a plural association; otherwise false.
161 162 163 |
# File 'lib/stannum/associations/many.rb', line 161 def many? true end |
#remove_value(entity, value, update_inverse: true) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Removes the given value from the association for the entity.
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/stannum/associations/many.rb', line 166 def remove_value(entity, value, update_inverse: true) return unless value data = entity.read_association(name, safe: false) || [] data, changed = remove_item(data:, value:) return unless changed entity.write_association(name, data, safe: false) remove_item_inverse(value:) if inverse? && update_inverse nil end |
#resolved_inverse ⇒ Stannum::Association
Returns the inverse association, if any.
183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/stannum/associations/many.rb', line 183 def resolved_inverse return @resolved_inverse if @resolved_inverse inverse = super return inverse unless inverse&.many? raise InverseAssociationError, "invalid inverse association #{inverse_name.inspect} - :many to " \ ':many associations are not currently supported' end |
#set_value(entity, value, update_inverse: true) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Replaces the association for the entity with the given value.
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/stannum/associations/many.rb', line 196 def set_value(entity, value, update_inverse: true) data = entity.read_association(name, safe: false) || [] value&.each do |item| next unless item data, _ = add_item(data:, value: item) update_item_inverse(entity:, value: item) if inverse? && update_inverse end entity.write_association(name, data, safe: false) nil end |