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 = Set.new
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.

The specified model instance will be deleted regardless of the options used when it was added.

Examples:

model_a = Model.new
model_b = Model.new

collection.push(model_a, destroy: true)
collection.push(model_b)
collection.push(model_a, destroy: false)
collection.count  #=> 3

collection.delete(model_a)
collection.count  #=> 1

Parameters:

  • model (Object)

    model instance

Returns:

  • (self)

    Successful deletion

  • (nil)

    If deletion fails



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.

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?

#instance_variables_to_inspectvoid (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.

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