Class: ActiveRecordCompose::InnerModelCollection

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

Instance Method Summary collapse

Instance Method Details

#<<(model) ⇒ InnerModelCollection

Appends model to collection.

Parameters:

  • model (Object)

    the model instance

Returns:



25
26
27
28
# File 'lib/active_record_compose/inner_model_collection.rb', line 25

def <<(model)
  models << wrap(model, context: :save)
  self
end

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

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

Yield Parameters:

Returns:



73
74
75
76
77
78
# File 'lib/active_record_compose/inner_model_collection.rb', line 73

def __each_by_wrapped
  return enum_for(:__each_by_wrapped) unless block_given?

  models.each { yield _1 }
  self
end

#clearInnerModelCollection

Set to empty.

Returns:



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

def clear
  models.clear
  self
end

#delete(model, context: :save) ⇒ InnerModelCollection?

Removes the specified model from the collection. Returns nil if the deletion fails, self if it succeeds.

Parameters:

  • model (Object)

    the model instance

  • context (Symbol) (defaults to: :save)

    :save or :destroy

Returns:



60
61
62
63
64
65
# File 'lib/active_record_compose/inner_model_collection.rb', line 60

def delete(model, context: :save)
  wrapped = wrap(model, context:)
  return nil unless models.delete(wrapped)

  self
end

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

Enumerates model objects.

Yield Parameters:

  • the (Object)

    model instance

Returns:



14
15
16
17
18
19
# File 'lib/active_record_compose/inner_model_collection.rb', line 14

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



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

def empty? = models.empty?

#push(model, context: :save) ⇒ InnerModelCollection

Appends model to collection.

Parameters:

  • model (Object)

    the model instance

  • context (Symbol) (defaults to: :save)

    :save or :destroy

Returns:



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

def push(model, context: :save)
  models << wrap(model, context:)
  self
end