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.
15 16 17 18 |
# File 'lib/active_record_compose/composed_collection.rb', line 15 def initialize(owner) @owner = owner @models = Set.new end |
Instance Method Details
#<<(model) ⇒ self
Appends model to collection.
36 37 38 39 |
# File 'lib/active_record_compose/composed_collection.rb', line 36 def <<(model) models << wrap(model, destroy: false) self end |
#clear ⇒ self
Set to empty.
65 66 67 68 |
# File 'lib/active_record_compose/composed_collection.rb', line 65 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.
90 91 92 93 94 95 96 |
# File 'lib/active_record_compose/composed_collection.rb', line 90 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.
25 26 27 28 29 30 |
# File 'lib/active_record_compose/composed_collection.rb', line 25 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.
60 |
# File 'lib/active_record_compose/composed_collection.rb', line 60 def empty? = models.empty? |
#instance_variables_to_inspect ⇒ void (private)
125 |
# File 'lib/active_record_compose/composed_collection.rb', line 125 def instance_variables_to_inspect = %i[@owner @models] |
#push(model, destroy: false, if: nil) ⇒ self
Appends model to collection.
52 53 54 55 |
# File 'lib/active_record_compose/composed_collection.rb', line 52 def push(model, destroy: false, if: nil) models << wrap(model, destroy:, if:) self end |