Class: ActiveRecordCompose::InnerModelCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/active_record_compose/inner_model_collection.rb

Defined Under Namespace

Modules: PackagePrivate

Instance Method Summary collapse

Constructor Details

#initialize(owner) ⇒ InnerModelCollection

Returns a new instance of InnerModelCollection.



11
12
13
14
# File 'lib/active_record_compose/inner_model_collection.rb', line 11

def initialize(owner)
  @owner = owner
  @models = []
end

Instance Method Details

#<<(model) ⇒ self

Appends model to collection.

Parameters:

  • model (Object)

    the model instance

Returns:

  • (self)

    returns itself.



32
33
34
35
# File 'lib/active_record_compose/inner_model_collection.rb', line 32

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

#clearself

Set to empty.

Returns:

  • (self)

    returns itself.



59
60
61
62
# File 'lib/active_record_compose/inner_model_collection.rb', line 59

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)

    the model instance

Returns:

  • (self)

    Successful deletion

  • (nil)

    If deletion fails



70
71
72
73
74
75
# File 'lib/active_record_compose/inner_model_collection.rb', line 70

def delete(model)
  wrapped = wrap(model)
  return nil unless models.delete(wrapped)

  self
end

#each {|the| ... } ⇒ Enumerator, self

Enumerates model objects.

Yield Parameters:

  • the (Object)

    model instance

Returns:

  • (Enumerator)

    when not block given.

  • (self)

    when block given, returns itself.



21
22
23
24
25
26
# File 'lib/active_record_compose/inner_model_collection.rb', line 21

def each
  return enum_for(:each) unless block_given?

  models.each { yield _1.__raw_model } # steep:ignore
  self
end

#empty?Boolean

Returns true if the element exists.

Returns:

  • (Boolean)

    Returns true if the element exists



54
# File 'lib/active_record_compose/inner_model_collection.rb', line 54

def empty? = models.empty?

#push(model, destroy: false, if: nil) ⇒ self

Appends model to collection.

Parameters:

  • model (Object)

    the model instance

  • destroy (Boolean) (defaults to: false)

    given true, destroy model.

  • destroy (Proc) (defaults to: false)

    when proc returning true, destroy model.

  • destroy (Symbol) (defaults to: false)

    applies boolean value of result of sending a message to ‘owner` to evaluation.

  • if (Proc) (defaults to: nil)

    evaluation result is false, it will not be included in the renewal.

  • if (Symbol) (defaults to: nil)

    applies boolean value of result of sending a message to ‘owner` to evaluation.

Returns:

  • (self)

    returns itself.



46
47
48
49
# File 'lib/active_record_compose/inner_model_collection.rb', line 46

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