Class: JsonAPIObjectMapper::Deserialize::Collection
- Inherits:
-
Object
- Object
- JsonAPIObjectMapper::Deserialize::Collection
- Extended by:
- Forwardable
- Includes:
- Enumerable, Parser::Errors
- Defined in:
- lib/jsonapi-object-mapper/deserialize/collection.rb
Instance Attribute Summary collapse
-
#collection_data ⇒ Object
readonly
Returns the value of attribute collection_data.
-
#links ⇒ Object
readonly
Returns the value of attribute links.
Attributes included from Parser::Errors
Instance Method Summary collapse
- #each ⇒ Object
-
#initialize(parser, klass:) ⇒ Collection
constructor
A new instance of Collection.
- #inspect ⇒ Object (also: #to_s)
- #to_hash ⇒ Object (also: #to_h)
Methods included from Parser::Errors
Constructor Details
#initialize(parser, klass:) ⇒ Collection
Returns a new instance of Collection.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/jsonapi-object-mapper/deserialize/collection.rb', line 16 def initialize(parser, klass:) raise InvalidResource unless klass.is_a?(Class) raise InvalidParser unless parser.is_a?(JsonAPIObjectMapper::Parser::Document) @errors = parser.errors @links = parser.links @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_data ⇒ Object (readonly)
Returns the value of attribute collection_data.
12 13 14 |
# File 'lib/jsonapi-object-mapper/deserialize/collection.rb', line 12 def collection_data @collection_data end |
#links ⇒ Object (readonly)
Returns the value of attribute links.
12 13 14 |
# File 'lib/jsonapi-object-mapper/deserialize/collection.rb', line 12 def links @links end |
Instance Method Details
#each ⇒ Object
33 34 35 36 37 |
# File 'lib/jsonapi-object-mapper/deserialize/collection.rb', line 33 def each @collection_data.each do |data| yield data end end |
#inspect ⇒ Object Also known as: to_s
39 40 41 42 43 44 |
# File 'lib/jsonapi-object-mapper/deserialize/collection.rb', line 39 def inspect "#<#{self.class}:0x#{object_id.to_s(16)}, "\ "@errors=#{@errors.inspect}, "\ "@links=#{@links.inspect}, "\ "@data=#{@collection_data.map(&:inspect)}>"\ end |
#to_hash ⇒ Object Also known as: to_h
47 48 49 50 51 52 53 |
# File 'lib/jsonapi-object-mapper/deserialize/collection.rb', line 47 def to_hash {}.tap do |hash| hash[:data] = @collection_data.map(&:to_hash) hash[:links] = @links.to_h unless @links.nil? hash[:errors] = @errors unless valid? end end |