Class: ActiveRecordCompose::ComposedCollection
- Inherits:
-
Object
- Object
- ActiveRecordCompose::ComposedCollection
- Includes:
- Enumerable
- Defined in:
- lib/active_record_compose/composed_collection.rb
Overview
Object obtained by Model#models.
It functions as a collection that contains the object to be saved.
Instance Method Summary collapse
-
#<<(model) ⇒ self
Appends model to collection.
-
#clear ⇒ self
Set to empty.
-
#delete(model) ⇒ self?
Removes the specified model from the collection.
-
#each {|model| ... } ⇒ Enumerator, self
Enumerates model objects.
-
#empty? ⇒ Boolean
Returns true if the element exists.
-
#initialize(owner) ⇒ ComposedCollection
constructor
A new instance of ComposedCollection.
- #instance_variables_to_inspect ⇒ void private
-
#push(model, destroy: false, if: nil) ⇒ self
Appends model to collection.
Constructor Details
#initialize(owner) ⇒ ComposedCollection
Returns a new instance of ComposedCollection.
14 15 16 17 |
# File 'lib/active_record_compose/composed_collection.rb', line 14 def initialize(owner) @owner = owner @models = Set.new end |
Instance Method Details
#<<(model) ⇒ self
Appends model to collection.
35 36 37 38 |
# File 'lib/active_record_compose/composed_collection.rb', line 35 def <<(model) models << wrap(model, destroy: false) self end |
#clear ⇒ self
Set to empty.
64 65 66 67 |
# File 'lib/active_record_compose/composed_collection.rb', line 64 def clear models.clear self end |
#delete(model) ⇒ self?
Removes the specified model from the collection. Returns nil if the deletion fails, self if it succeeds.
The specified model instance will be deleted regardless of the options used when it was added.
89 90 91 92 93 94 95 |
# File 'lib/active_record_compose/composed_collection.rb', line 89 def delete(model) matched = models.select { _1.__raw_model == model } return nil if matched.blank? matched.each { models.delete(_1) } self end |
#each {|model| ... } ⇒ Enumerator, self
Enumerates model objects.
24 25 26 27 28 29 |
# File 'lib/active_record_compose/composed_collection.rb', line 24 def each return enum_for(:each) unless block_given? models.each { yield _1.__raw_model } self end |
#empty? ⇒ Boolean
Returns true if the element exists.
59 |
# File 'lib/active_record_compose/composed_collection.rb', line 59 def empty? = models.empty? |
#instance_variables_to_inspect ⇒ void (private)
124 |
# File 'lib/active_record_compose/composed_collection.rb', line 124 def instance_variables_to_inspect = %i[@owner @models] |
#push(model, destroy: false, if: nil) ⇒ self
Appends model to collection.
51 52 53 54 |
# File 'lib/active_record_compose/composed_collection.rb', line 51 def push(model, destroy: false, if: nil) models << wrap(model, destroy:, if:) self end |