Class: JsonAPIObjectMapper::Deserialize::Resource

Inherits:
Object
  • Object
show all
Extended by:
DSL
Includes:
Parser::Errors
Defined in:
lib/jsonapi-object-mapper/deserialize/resource.rb

Constant Summary

Constants included from DSL

DSL::DEFAULT_BLOCK, DSL::HAS_MANY_BLOCK

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from Parser::Errors

#errors

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSL

attribute, attributes, extended, has_many, has_one, id, kind_of_resource?, type

Methods included from Parser::Errors

#invalid?, #valid?

Constructor Details

#initialize(parser, document: nil) ⇒ Resource

Returns a new instance of Resource.

Raises:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/jsonapi-object-mapper/deserialize/resource.rb', line 46

def initialize(parser, document: nil)
  super()
  raise InvalidParser unless parser.is_a?(JsonAPIObjectMapper::Parser::Document)
  @errors = parser.errors

  if document_valid?
    @includes      = parser.includes
    @links         = parser_links(parser)
    @data          = document_data(parser, document)
    @id            = @data["id"]
    @type          = @data["type"]
    @attributes    = @data.fetch("attributes", {})
    @relationships = @data.fetch("relationships", {})
    deserialize!
  end

  freeze
end

Class Attribute Details

.attr_blocksObject

Returns the value of attribute attr_blocks.



16
17
18
# File 'lib/jsonapi-object-mapper/deserialize/resource.rb', line 16

def attr_blocks
  @attr_blocks
end

.id_blockObject

Returns the value of attribute id_block.



16
17
18
# File 'lib/jsonapi-object-mapper/deserialize/resource.rb', line 16

def id_block
  @id_block
end

.rel_has_many_blocksObject

Returns the value of attribute rel_has_many_blocks.



16
17
18
# File 'lib/jsonapi-object-mapper/deserialize/resource.rb', line 16

def rel_has_many_blocks
  @rel_has_many_blocks
end

.rel_has_one_blocksObject

Returns the value of attribute rel_has_one_blocks.



16
17
18
# File 'lib/jsonapi-object-mapper/deserialize/resource.rb', line 16

def rel_has_one_blocks
  @rel_has_one_blocks
end

.rel_optionsObject

Returns the value of attribute rel_options.



16
17
18
# File 'lib/jsonapi-object-mapper/deserialize/resource.rb', line 16

def rel_options
  @rel_options
end

.type_blockObject

Returns the value of attribute type_block.



16
17
18
# File 'lib/jsonapi-object-mapper/deserialize/resource.rb', line 16

def type_block
  @type_block
end

Instance Attribute Details

Returns the value of attribute links.



13
14
15
# File 'lib/jsonapi-object-mapper/deserialize/resource.rb', line 13

def links
  @links
end

Class Method Details

.call(document) ⇒ Object



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

def self.call(document)
  load(document)
end

.inherited(klass) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/jsonapi-object-mapper/deserialize/resource.rb', line 23

def self.inherited(klass)
  super
  klass.instance_variable_set("@attr_blocks", attr_blocks.dup)
  klass.instance_variable_set("@rel_has_one_blocks", rel_has_one_blocks.dup)
  klass.instance_variable_set("@rel_has_many_blocks", rel_has_many_blocks.dup)
  klass.instance_variable_set("@rel_options", rel_options.dup)
  klass.instance_variable_set("@id_block", id_block)
  klass.instance_variable_set("@type_block", type_block)
end

.load(document) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/jsonapi-object-mapper/deserialize/resource.rb', line 37

def self.load(document)
  parser = JsonAPIObjectMapper::Parser::Document.new(document)
  if parser.contains_data_array? || parser.invalid?
    Collection.new(parser, klass: self)
  else
    new(parser)
  end
end

Instance Method Details

#each {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



65
66
67
# File 'lib/jsonapi-object-mapper/deserialize/resource.rb', line 65

def each
  yield self
end