Class: JsonAPIObjectMapper::Deserialize::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Parser::Errors
Defined in:
lib/jsonapi-object-mapper/deserialize/collection.rb

Instance Attribute Summary collapse

Attributes included from Parser::Errors

#errors

Instance Method Summary collapse

Methods included from Parser::Errors

#invalid?, #valid?

Constructor Details

#initialize(parser, klass:) ⇒ Collection

Returns a new instance of Collection.

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/jsonapi-object-mapper/deserialize/collection.rb', line 13

def initialize(parser, klass:)
  raise InvalidResource unless klass.is_a?(Class)
  raise InvalidParser   unless parser.is_a?(JsonAPIObjectMapper::Parser::Document)
  @errors            = parser.errors
  @collection_data   =
    if document_invalid?
      []
    else
      Array(parser.document["data"]).map do |doc|
        klass.new(parser, document: doc)
      end
    end.freeze

  freeze
end

Instance Attribute Details

#collection_dataObject

Returns the value of attribute collection_data.



11
12
13
# File 'lib/jsonapi-object-mapper/deserialize/collection.rb', line 11

def collection_data
  @collection_data
end

Instance Method Details

#[](index) ⇒ Object



33
34
35
# File 'lib/jsonapi-object-mapper/deserialize/collection.rb', line 33

def [](index)
  @collection_data[index]
end

#eachObject



37
38
39
40
41
# File 'lib/jsonapi-object-mapper/deserialize/collection.rb', line 37

def each
  @collection_data.each do |data|
    yield data
  end
end

#to_hashObject



29
30
31
# File 'lib/jsonapi-object-mapper/deserialize/collection.rb', line 29

def to_hash
  @collection_data.map(&:to_hash)
end