-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathresource.rb
More file actions
148 lines (123 loc) · 4.18 KB
/
resource.rb
File metadata and controls
148 lines (123 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
require 'jsonapi/deserializable/resource/dsl'
module JSONAPI
module Deserializable
class Resource
extend DSL
class << self
attr_accessor :type_block, :id_block, :attr_blocks,
:has_one_rel_blocks, :has_many_rel_blocks,
:default_attr_block, :default_has_one_rel_block,
:default_has_many_rel_block,
:key_formatter
end
self.attr_blocks = {}
self.has_one_rel_blocks = {}
self.has_many_rel_blocks = {}
self.key_formatter = proc { |k| k }
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
def self.call(payload)
new(payload).to_h
end
def initialize(payload, root: '/data')
@data = payload || {}
@root = root
@type = @data['type']
@id = @data['id']
@attributes = @data['attributes'] || {}
@relationships = @data['relationships'] || {}
deserialize!
freeze
end
def to_hash
@hash
end
alias to_h to_hash
attr_reader :reverse_mapping
private
def register_mappings(keys, path)
keys.each do |k|
@reverse_mapping[k] = @root + path
end
end
def deserialize!
@reverse_mapping = {}
hashes = [deserialize_type, deserialize_id,
deserialize_attrs, deserialize_rels]
@hash = hashes.reduce({}, :merge)
end
def deserialize_type
block = self.class.type_block
return {} unless block
hash = block.call(@type)
register_mappings(hash.keys, '/type')
hash
end
def deserialize_id
block = self.class.id_block
return {} unless @id && block
hash = block.call(@id)
register_mappings(hash.keys, '/id')
hash
end
def deserialize_attrs
obj = {}
@attributes.each { |key, val| obj.merge!(deserialize_attr(key, val)) }
obj
end
def deserialize_attr(key, val)
block = self.class.attr_blocks[key] || self.class.default_attr_block
return {} unless block
hash = block.call(val, self.class.key_formatter.call(key))
register_mappings(hash.keys, "/attributes/#{key}")
hash
end
def deserialize_rels
obj = {}
@relationships.each { |key, val| obj.merge!(deserialize_rel(key, val)) }
obj
end
def deserialize_rel(key, val)
if val['data'].is_a?(Array)
deserialize_has_many_rel(key, val)
else
deserialize_has_one_rel(key, val)
end
end
# rubocop: disable Metrics/AbcSize
def deserialize_has_one_rel(key, val)
block = self.class.has_one_rel_blocks[key] ||
self.class.default_has_one_rel_block
return {} unless block
id = val['data'] && val['data']['id']
type = val['data'] && val['data']['type']
hash = block.call(val, id, type, self.class.key_formatter.call(key))
register_mappings(hash.keys, "/relationships/#{key}")
hash
end
# rubocop: enable Metrics/AbcSize
# rubocop: disable Metrics/AbcSize
def deserialize_has_many_rel(key, val)
block = self.class.has_many_rel_blocks[key] ||
self.class.default_has_many_rel_block
return {} unless block && val['data'].is_a?(Array)
ids = val['data'].map { |ri| ri['id'] }
types = val['data'].map { |ri| ri['type'] }
hash = block.call(val, ids, types, self.class.key_formatter.call(key))
register_mappings(hash.keys, "/relationships/#{key}")
hash
end
# rubocop: enable Metrics/AbcSize
end
end
end