Class: Sumaki::Model::Associations::Association::Repeated

Inherits:
Object
  • Object
show all
Defined in:
lib/sumaki/model/associations/association.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(owner, reflection) ⇒ Repeated

Returns a new instance of Repeated.



31
32
33
34
# File 'lib/sumaki/model/associations/association.rb', line 31

def initialize(owner, reflection)
  @owner = owner
  @reflection = reflection
end

Instance Method Details

#collectionObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sumaki/model/associations/association.rb', line 36

def collection
  @collection ||= begin
    objects_or_value = @owner.get(@reflection.name)
    models = if objects_or_value.is_a?(Array)
               model_class = @reflection.model_class
               objects_or_value.map { |object| model_class.new(object, parent: @owner) }
             else
               []
             end

    @reflection.collection_class.new(models, owner: @owner)
  end
end