Class: ActiveRecordCompose::ComposedCollection

Inherits:
Object
  • Object
show all
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

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.

Parameters:

  • model (Object)

    model instance

Returns:

  • (self)

    returns itself.



35
36
37
38
# File 'lib/active_record_compose/composed_collection.rb', line 35

def <<(model)
  models << wrap(model, destroy: false)
  self
end

#clearself

Set to empty.

Returns:

  • (self)

    returns itself.



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.

Parameters:

  • model (Object)

    model instance

Returns:

  • (self)

    Successful deletion

  • (nil)

    If deletion fails



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.

Yield Parameters:

  • model (Object)

    model instance

Returns:

  • (Enumerator)

    when not block given.

  • (self)

    when block given, returns itself.



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.

Returns:

  • (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.

Parameters:

  • model (Object)

    model instance

  • destroy (Boolean, Proc, Symbol) (defaults to: false)

    Controls whether the model should be destroyed.

    • Boolean: if true, the model will be destroyed.
    • Proc: the model will be destroyed if the proc returns true.
    • Symbol: sends the symbol as a method to owner; if the result is truthy, the model will be destroyed.
  • if (Proc, Symbol) (defaults to: nil)

    Controls conditional inclusion in renewal.

    • Proc: the proc is called, and if it returns false, the model is excluded.
    • Symbol: sends the symbol as a method to owner; if the result is falsy, the model is excluded.

Returns:

  • (self)

    returns itself.



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