Class: JSONAPI::Deserializable::Resource

Inherits:
Object
  • Object
show all
Extended by:
DSL
Defined in:
lib/jsonapi/deserializable/resource.rb,
lib/jsonapi/deserializable/resource/dsl.rb

Defined Under Namespace

Modules: DSL

Constant Summary

Constants included from DSL

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSL

attribute, attributes, has_many, has_one, id, key_format, type

Constructor Details

#initialize(payload, root: '/data') ⇒ Resource



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/jsonapi/deserializable/resource.rb', line 38

def initialize(payload, root: '/data')
  @data = payload || {}
  @root = root
  @type = @data['type']
  @id   = @data['id']
  @attributes    = @data['attributes'] || {}
  @relationships = @data['relationships'] || {}
  deserialize!

  freeze
end

Class Attribute Details

.attr_blocksObject

Returns the value of attribute attr_blocks.



9
10
11
# File 'lib/jsonapi/deserializable/resource.rb', line 9

def attr_blocks
  @attr_blocks
end

.default_attr_blockObject

Returns the value of attribute default_attr_block.



9
10
11
# File 'lib/jsonapi/deserializable/resource.rb', line 9

def default_attr_block
  @default_attr_block
end

.default_has_many_rel_blockObject

Returns the value of attribute default_has_many_rel_block.



9
10
11
# File 'lib/jsonapi/deserializable/resource.rb', line 9

def default_has_many_rel_block
  @default_has_many_rel_block
end

.default_has_one_rel_blockObject

Returns the value of attribute default_has_one_rel_block.



9
10
11
# File 'lib/jsonapi/deserializable/resource.rb', line 9

def default_has_one_rel_block
  @default_has_one_rel_block
end

.has_many_rel_blocksObject

Returns the value of attribute has_many_rel_blocks.



9
10
11
# File 'lib/jsonapi/deserializable/resource.rb', line 9

def has_many_rel_blocks
  @has_many_rel_blocks
end

.has_one_rel_blocksObject

Returns the value of attribute has_one_rel_blocks.



9
10
11
# File 'lib/jsonapi/deserializable/resource.rb', line 9

def has_one_rel_blocks
  @has_one_rel_blocks
end

.id_blockObject

Returns the value of attribute id_block.



9
10
11
# File 'lib/jsonapi/deserializable/resource.rb', line 9

def id_block
  @id_block
end

.key_formatterObject

Returns the value of attribute key_formatter.



9
10
11
# File 'lib/jsonapi/deserializable/resource.rb', line 9

def key_formatter
  @key_formatter
end

.type_blockObject

Returns the value of attribute type_block.



9
10
11
# File 'lib/jsonapi/deserializable/resource.rb', line 9

def type_block
  @type_block
end

Instance Attribute Details

#reverse_mappingObject (readonly)

Returns the value of attribute reverse_mapping.



55
56
57
# File 'lib/jsonapi/deserializable/resource.rb', line 55

def reverse_mapping
  @reverse_mapping
end

Class Method Details

.call(payload) ⇒ Object



34
35
36
# File 'lib/jsonapi/deserializable/resource.rb', line 34

def self.call(payload)
  new(payload).to_h
end

.inherited(klass) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/jsonapi/deserializable/resource.rb', line 21

def self.inherited(klass)
  super
  klass.type_block          = type_block
  klass.id_block            = id_block
  klass.attr_blocks         = attr_blocks.dup
  klass.has_one_rel_blocks  = has_one_rel_blocks.dup
  klass.has_many_rel_blocks = has_many_rel_blocks.dup
  klass.default_attr_block  = default_attr_block
  klass.default_has_one_rel_block   = default_has_one_rel_block
  klass.default_has_many_rel_block  = default_has_many_rel_block
  klass.key_formatter = key_formatter
end

Instance Method Details

#to_hashObject Also known as: to_h



50
51
52
# File 'lib/jsonapi/deserializable/resource.rb', line 50

def to_hash
  @hash
end