Class: ActiveRecordCompose::InnerModelCollection

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

Instance Method Summary collapse

Constructor Details

#initialize(owner) ⇒ InnerModelCollection

Returns a new instance of InnerModelCollection.



9
10
11
12
# File 'lib/active_record_compose/inner_model_collection.rb', line 9

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.



30
31
32
33
# File 'lib/active_record_compose/inner_model_collection.rb', line 30

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

#__each_by_wrapped {|rawpped| ... } ⇒ Enumerator, self

Enumerates model objects, but it should be noted that application developers are not expected to use this interface.

Yield Parameters:

Returns:

  • (Enumerator)

    when not block given.

  • (self)

    when block given, returns itself.



80
81
82
83
84
85
# File 'lib/active_record_compose/inner_model_collection.rb', line 80

def __each_by_wrapped
  return enum_for(:__each_by_wrapped) unless block_given?

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

#clearself

Set to empty.

Returns:

  • (self)

    returns itself.



55
56
57
58
# File 'lib/active_record_compose/inner_model_collection.rb', line 55

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



66
67
68
69
70
71
# File 'lib/active_record_compose/inner_model_collection.rb', line 66

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.



19
20
21
22
23
24
# File 'lib/active_record_compose/inner_model_collection.rb', line 19

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



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

def empty? = models.empty?

#push(model, destroy: false) ⇒ 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.

Returns:

  • (self)

    returns itself.



42
43
44
45
# File 'lib/active_record_compose/inner_model_collection.rb', line 42

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