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.
-
#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 = [] 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.
75 76 77 78 79 80 |
# File 'lib/active_record_compose/composed_collection.rb', line 75 def delete(model) wrapped = wrap(model) return nil unless models.delete(wrapped) 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? |
#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 |