Class: JsonAPIObjectMapper::Deserializer::Resource

Inherits:
Object
  • Object
show all
Extended by:
DSL
Defined in:
lib/jsonapi_object_mapper/deserializer/resource.rb

Constant Summary

Constants included from DSL

DSL::DEFAULT_ATTR_BLOCK, DSL::DEFAULT_HAS_ONE_BLOCK, DSL::DEFAULT_ID_BLOCK, DSL::DEFAULT_TYPE_BLOCK

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSL

attribute, attributes, extended, has_one, id, type

Constructor Details

#initialize(payload = nil, included = nil) ⇒ Resource

Returns a new instance of Resource.



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/jsonapi_object_mapper/deserializer/resource.rb', line 43

def initialize(payload = nil, included = nil)
  super()
  data           = payload || {}
  @id            = data["id"]
  @type          = data["type"]
  @attributes    = data["attributes"] || {}
  @relationships = data["relationships"] || {}
  @includes      = IncludedResources.load(included)
  deserialize!

  freeze
end

Class Attribute Details

.attr_blocksObject

Returns the value of attribute attr_blocks.



12
13
14
# File 'lib/jsonapi_object_mapper/deserializer/resource.rb', line 12

def attr_blocks
  @attr_blocks
end

.id_blockObject

Returns the value of attribute id_block.



12
13
14
# File 'lib/jsonapi_object_mapper/deserializer/resource.rb', line 12

def id_block
  @id_block
end

.rel_blocksObject

Returns the value of attribute rel_blocks.



12
13
14
# File 'lib/jsonapi_object_mapper/deserializer/resource.rb', line 12

def rel_blocks
  @rel_blocks
end

.rel_optionsObject

Returns the value of attribute rel_options.



12
13
14
# File 'lib/jsonapi_object_mapper/deserializer/resource.rb', line 12

def rel_options
  @rel_options
end

.type_blockObject

Returns the value of attribute type_block.



12
13
14
# File 'lib/jsonapi_object_mapper/deserializer/resource.rb', line 12

def type_block
  @type_block
end

Class Method Details

.call(document, parsed_includes = nil) ⇒ Object



33
34
35
36
37
# File 'lib/jsonapi_object_mapper/deserializer/resource.rb', line 33

def self.call(document, parsed_includes = nil)
  parsed_document = document.is_a?(Hash) ? document : ::Oj.load(document)
  parsed_includes ||= IncludedResources.load(parsed_document["included"])
  new(parsed_document["data"], parsed_includes)
end

.call_collection(document) ⇒ Object



27
28
29
30
31
# File 'lib/jsonapi_object_mapper/deserializer/resource.rb', line 27

def self.call_collection(document)
  parsed_document = document.is_a?(Hash) ? document : ::Oj.load(document)
  parsed_includes = IncludedResources.load(parsed_document["included"])
  Array(parsed_document["data"]).map { |doc| new(doc, parsed_includes) }
end

.embed!(attributes) ⇒ Object



39
40
41
# File 'lib/jsonapi_object_mapper/deserializer/resource.rb', line 39

def self.embed!(attributes)
  new("attributes" => attributes)
end

.inherited(klass) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/jsonapi_object_mapper/deserializer/resource.rb', line 18

def self.inherited(klass)
  super
  klass.instance_variable_set("@attr_blocks", attr_blocks.dup)
  klass.instance_variable_set("@rel_blocks", rel_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