jsonapi-deserializable 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: '00948aee890ce50979d9e79091d3d773c718c81d'
4
- data.tar.gz: be1b26548249485868b080a05d232119ac5c7f71
3
+ metadata.gz: ac770df506999a1b03b79a97722d3697c74e31cb
4
+ data.tar.gz: 31d3323da426ffc8e9c73bd5241d5335a233a13a
5
5
  SHA512:
6
- metadata.gz: 1f74445bc44fef0931df10075cf8cdd359c91c8c178c4ba2a59318168094f18b9ca5ca30f131f3a6d401497d41c33cd3c628f79069294dae41cc168f5de2bfa6
7
- data.tar.gz: a2bff5fd5dd4442935088c08208f400d0c79fc08363c2b8742df0d70f33f979883b051bacc460d1b3ba2cf9c5d31e622c399cd2bf34051f8dd04bfa1fee129d4
6
+ metadata.gz: b09ba8b3d06401c0ddaa52546a22ae729bb198713231944bb8a4b71b97179de36c5f929ceaaaf2d16780e2be0e910b6df918423ead4b75ae3fdc6c465bb52522
7
+ data.tar.gz: 7371a9283581c85d3c155436f622ee24372dfe7019adcc5df30c98fa171c1e52c4f28ad98a4380b064c5817cb7f37fefc08b12cf463b7968e2678f31b44a1e20
data/README.md CHANGED
@@ -9,6 +9,11 @@ hashes.
9
9
  [![codecov](https://codecov.io/gh/jsonapi-rb/jsonapi-deserializable/branch/master/graph/badge.svg)](https://codecov.io/gh/jsonapi-rb/deserializable)
10
10
  [![Gitter chat](https://badges.gitter.im/gitterHQ/gitter.png)](https://gitter.im/jsonapi-rb/Lobby)
11
11
 
12
+ ## Resources
13
+
14
+ * Chat: [gitter](http://gitter.im/jsonapi-rb)
15
+ * Twitter: [@jsonapirb](http://twitter.com/jsonapirb)
16
+ * Docs: [jsonapi-rb.org](http://jsonapi-rb.org)
12
17
 
13
18
  ## Usage and documentation
14
19
 
@@ -1,5 +1,4 @@
1
1
  require 'jsonapi/deserializable/relationship/dsl'
2
- require 'jsonapi/parser/relationship'
3
2
 
4
3
  module JSONAPI
5
4
  module Deserializable
@@ -21,10 +20,10 @@ module JSONAPI
21
20
  end
22
21
 
23
22
  def initialize(payload)
24
- Parser::Relationship.parse!(payload)
25
23
  @document = payload
26
24
  @data = payload['data']
27
25
  deserialize!
26
+
28
27
  freeze
29
28
  end
30
29
 
@@ -45,23 +44,19 @@ module JSONAPI
45
44
  end
46
45
 
47
46
  def deserialize_has_one
47
+ block = self.class.has_one_block
48
+ return {} unless block
48
49
  id = @data && @data['id']
49
50
  type = @data && @data['type']
50
- if self.class.has_one_block
51
- self.class.has_one_block.call(@document, id, type)
52
- else
53
- { id: id, type: type }
54
- end
51
+ block.call(@document, id, type)
55
52
  end
56
53
 
57
54
  def deserialize_has_many
55
+ block = self.class.has_many_block
56
+ return {} unless block && @data.is_a?(Array)
58
57
  ids = @data.map { |ri| ri['id'] }
59
58
  types = @data.map { |ri| ri['type'] }
60
- if self.class.has_many_block
61
- self.class.has_many_block.call(@document, ids, types)
62
- else
63
- { ids: ids, types: types }
64
- end
59
+ block.call(@document, ids, types)
65
60
  end
66
61
  end
67
62
  end
@@ -2,12 +2,19 @@ module JSONAPI
2
2
  module Deserializable
3
3
  class Relationship
4
4
  module DSL
5
+ DEFAULT_HAS_ONE_REL_BLOCK = proc do |_val, id, type|
6
+ { type: type, id: id }
7
+ end
8
+ DEFAULT_HAS_MANY_REL_BLOCK = proc do |_val, ids, types|
9
+ { types: types, ids: ids }
10
+ end
11
+
5
12
  def has_one(&block)
6
- self.has_one_block = block
13
+ self.has_one_block = block || DEFAULT_HAS_ONE_REL_BLOCK
7
14
  end
8
15
 
9
16
  def has_many(&block)
10
- self.has_many_block = block
17
+ self.has_many_block = block || DEFAULT_HAS_MANY_REL_BLOCK
11
18
  end
12
19
  end
13
20
  end
@@ -1,6 +1,4 @@
1
- require 'jsonapi/deserializable/resource/configuration'
2
1
  require 'jsonapi/deserializable/resource/dsl'
3
- require 'jsonapi/parser/resource'
4
2
 
5
3
  module JSONAPI
6
4
  module Deserializable
@@ -10,47 +8,42 @@ module JSONAPI
10
8
  class << self
11
9
  attr_accessor :type_block, :id_block, :attr_blocks,
12
10
  :has_one_rel_blocks, :has_many_rel_blocks,
13
- :configuration
11
+ :default_attr_block, :default_has_one_rel_block,
12
+ :default_has_many_rel_block,
13
+ :key_formatter
14
14
  end
15
15
 
16
- @class_cache = {}
17
-
18
- self.configuration = Configuration.new
19
16
  self.attr_blocks = {}
20
17
  self.has_one_rel_blocks = {}
21
18
  self.has_many_rel_blocks = {}
19
+ self.key_formatter = proc { |k| k }
22
20
 
23
21
  def self.inherited(klass)
24
22
  super
25
- klass.configuration = configuration.dup
26
23
  klass.type_block = type_block
27
24
  klass.id_block = id_block
28
25
  klass.attr_blocks = attr_blocks.dup
29
26
  klass.has_one_rel_blocks = has_one_rel_blocks.dup
30
27
  klass.has_many_rel_blocks = has_many_rel_blocks.dup
31
- end
32
-
33
- def self.configure
34
- yield(configuration)
35
- end
36
-
37
- def self.[](name)
38
- @class_cache[name] ||= Class.new(self)
28
+ klass.default_attr_block = default_attr_block
29
+ klass.default_has_one_rel_block = default_has_one_rel_block
30
+ klass.default_has_many_rel_block = default_has_many_rel_block
31
+ klass.key_formatter = key_formatter
39
32
  end
40
33
 
41
34
  def self.call(payload)
42
35
  new(payload).to_h
43
36
  end
44
37
 
45
- def initialize(payload)
46
- Parser::Resource.parse!(payload)
47
- @document = payload
48
- @data = @document['data']
38
+ def initialize(payload, root: '/data')
39
+ @data = payload || {}
40
+ @root = root
49
41
  @type = @data['type']
50
42
  @id = @data['id']
51
43
  @attributes = @data['attributes'] || {}
52
44
  @relationships = @data['relationships'] || {}
53
45
  deserialize!
46
+
54
47
  freeze
55
48
  end
56
49
 
@@ -63,13 +56,9 @@ module JSONAPI
63
56
 
64
57
  private
65
58
 
66
- def configuration
67
- self.class.configuration
68
- end
69
-
70
59
  def register_mappings(keys, path)
71
60
  keys.each do |k|
72
- @reverse_mapping[k] = path
61
+ @reverse_mapping[k] = @root + path
73
62
  end
74
63
  end
75
64
 
@@ -81,17 +70,20 @@ module JSONAPI
81
70
  end
82
71
 
83
72
  def deserialize_type
84
- block = self.class.type_block || configuration.default_type
73
+ block = self.class.type_block
74
+ return {} unless block
75
+
85
76
  hash = block.call(@type)
86
- register_mappings(hash.keys, '/data/type')
77
+ register_mappings(hash.keys, '/type')
87
78
  hash
88
79
  end
89
80
 
90
81
  def deserialize_id
91
- return {} unless @id
92
- block = self.class.id_block || configuration.default_id
93
- hash = block.call(@id)
94
- register_mappings(hash.keys, '/data/id')
82
+ block = self.class.id_block
83
+ return {} unless @id && block
84
+
85
+ hash = block.call(@id)
86
+ register_mappings(hash.keys, '/id')
95
87
  hash
96
88
  end
97
89
 
@@ -102,12 +94,11 @@ module JSONAPI
102
94
  end
103
95
 
104
96
  def deserialize_attr(key, val)
105
- hash = if self.class.attr_blocks.key?(key)
106
- self.class.attr_blocks[key].call(val)
107
- else
108
- configuration.default_attribute.call(key, val)
109
- end
110
- register_mappings(hash.keys, "/data/attributes/#{key}")
97
+ block = self.class.attr_blocks[key] || self.class.default_attr_block
98
+ return {} unless block
99
+
100
+ hash = block.call(val, self.class.key_formatter.call(key))
101
+ register_mappings(hash.keys, "/attributes/#{key}")
111
102
  hash
112
103
  end
113
104
 
@@ -118,36 +109,38 @@ module JSONAPI
118
109
  end
119
110
 
120
111
  def deserialize_rel(key, val)
121
- hash = if val['data'].is_a?(Array)
122
- deserialize_has_many_rel(key, val)
123
- else
124
- deserialize_has_one_rel(key, val)
125
- end
126
- register_mappings(hash.keys, "/data/relationships/#{key}")
127
- hash
112
+ if val['data'].is_a?(Array)
113
+ deserialize_has_many_rel(key, val)
114
+ else
115
+ deserialize_has_one_rel(key, val)
116
+ end
128
117
  end
129
118
 
130
119
  # rubocop: disable Metrics/AbcSize
131
120
  def deserialize_has_one_rel(key, val)
121
+ block = self.class.has_one_rel_blocks[key] ||
122
+ self.class.default_has_one_rel_block
123
+ return {} unless block
124
+
132
125
  id = val['data'] && val['data']['id']
133
126
  type = val['data'] && val['data']['type']
134
- if self.class.has_one_rel_blocks.key?(key)
135
- self.class.has_one_rel_blocks[key].call(val, id, type)
136
- else
137
- configuration.default_has_one.call(key, val, id, type)
138
- end
127
+ hash = block.call(val, id, type, self.class.key_formatter.call(key))
128
+ register_mappings(hash.keys, "/relationships/#{key}")
129
+ hash
139
130
  end
140
131
  # rubocop: enable Metrics/AbcSize
141
132
 
142
133
  # rubocop: disable Metrics/AbcSize
143
134
  def deserialize_has_many_rel(key, val)
135
+ block = self.class.has_many_rel_blocks[key] ||
136
+ self.class.default_has_many_rel_block
137
+ return {} unless block && val['data'].is_a?(Array)
138
+
144
139
  ids = val['data'].map { |ri| ri['id'] }
145
140
  types = val['data'].map { |ri| ri['type'] }
146
- if self.class.has_many_rel_blocks.key?(key)
147
- self.class.has_many_rel_blocks[key].call(val, ids, types)
148
- else
149
- configuration.default_has_many.call(key, val, ids, types)
150
- end
141
+ hash = block.call(val, ids, types, self.class.key_formatter.call(key))
142
+ register_mappings(hash.keys, "/relationships/#{key}")
143
+ hash
151
144
  end
152
145
  # rubocop: enable Metrics/AbcSize
153
146
  end
@@ -2,24 +2,54 @@ module JSONAPI
2
2
  module Deserializable
3
3
  class Resource
4
4
  module DSL
5
+ DEFAULT_TYPE_BLOCK = proc { |t| { type: t } }
6
+ DEFAULT_ID_BLOCK = proc { |i| { id: i } }
7
+ DEFAULT_ATTR_BLOCK = proc { |v, k| { k.to_sym => v } }
8
+ DEFAULT_HAS_ONE_BLOCK = proc do |_, i, t, k|
9
+ { "#{k}_id".to_sym => i, "#{k}_type".to_sym => t }
10
+ end
11
+ DEFAULT_HAS_MANY_BLOCK = proc do |_, i, t, k|
12
+ { "#{k}_ids".to_sym => i, "#{k}_types".to_sym => t }
13
+ end
14
+
5
15
  def type(&block)
6
- self.type_block = block
16
+ self.type_block = block || DEFAULT_TYPE_BLOCK
7
17
  end
8
18
 
9
19
  def id(&block)
10
- self.id_block = block
20
+ self.id_block = block || DEFAULT_ID_BLOCK
11
21
  end
12
22
 
13
23
  def attribute(key, &block)
14
- attr_blocks[key.to_s] = block
24
+ attr_blocks[key.to_s] = block || DEFAULT_ATTR_BLOCK
25
+ end
26
+
27
+ def attributes(*keys, &block)
28
+ if keys.empty?
29
+ self.default_attr_block = block || DEFAULT_ATTR_BLOCK
30
+ else
31
+ keys.each { |k| attribute(k, &block) }
32
+ end
33
+ end
34
+
35
+ def has_one(key = nil, &block)
36
+ if key
37
+ has_one_rel_blocks[key.to_s] = block || DEFAULT_HAS_ONE_BLOCK
38
+ else
39
+ self.default_has_one_rel_block = block || DEFAULT_HAS_ONE_BLOCK
40
+ end
15
41
  end
16
42
 
17
- def has_one(key, &block)
18
- has_one_rel_blocks[key.to_s] = block
43
+ def has_many(key = nil, &block)
44
+ if key
45
+ has_many_rel_blocks[key.to_s] = block || DEFAULT_HAS_MANY_BLOCK
46
+ else
47
+ self.default_has_many_rel_block = block || DEFAULT_HAS_MANY_BLOCK
48
+ end
19
49
  end
20
50
 
21
- def has_many(key, &block)
22
- has_many_rel_blocks[key.to_s] = block
51
+ def key_format(callable = nil, &block)
52
+ self.key_formatter = callable || block
23
53
  end
24
54
  end
25
55
  end
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-deserializable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lucas Hosseini
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-02 00:00:00.000000000 Z
11
+ date: 2017-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: jsonapi-parser
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - '='
18
- - !ruby/object:Gem::Version
19
- version: 0.1.1
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - '='
25
- - !ruby/object:Gem::Version
26
- version: 0.1.1
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: rake
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -77,8 +63,6 @@ files:
77
63
  - lib/jsonapi/deserializable/relationship.rb
78
64
  - lib/jsonapi/deserializable/relationship/dsl.rb
79
65
  - lib/jsonapi/deserializable/resource.rb
80
- - lib/jsonapi/deserializable/resource/configurable.rb
81
- - lib/jsonapi/deserializable/resource/configuration.rb
82
66
  - lib/jsonapi/deserializable/resource/dsl.rb
83
67
  homepage: https://github.com/jsonapi-rb/jsonapi-deserializable
84
68
  licenses:
@@ -100,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
100
84
  version: '0'
101
85
  requirements: []
102
86
  rubyforge_project:
103
- rubygems_version: 2.6.8
87
+ rubygems_version: 2.6.12
104
88
  signing_key:
105
89
  specification_version: 4
106
90
  summary: Deserialize JSON API payloads.
@@ -1,17 +0,0 @@
1
- require 'jsonapi/deserializable/resource/configuration'
2
-
3
- module JSONAPI
4
- module Deserializable
5
- module Configurable
6
- def self.included(klass)
7
- klass.class_eval do
8
- extend ClassMethods
9
- include InstanceMethods
10
- end
11
- end
12
- module ClassMethods
13
-
14
- end
15
- end
16
- end
17
- end
@@ -1,28 +0,0 @@
1
- module JSONAPI
2
- module Deserializable
3
- class Resource
4
- class Configuration
5
- DEFAULT_TYPE_BLOCK = proc { |t| { type: t } }
6
- DEFAULT_ID_BLOCK = proc { |i| { id: i } }
7
- DEFAULT_ATTR_BLOCK = proc { |k, v| { k.to_sym => v } }
8
- DEFAULT_HAS_ONE_BLOCK = proc do |k, _, i, t|
9
- { "#{k}_id".to_sym => i, "#{k}_type".to_sym => t }
10
- end
11
- DEFAULT_HAS_MANY_BLOCK = proc do |k, _, i, t|
12
- { "#{k}_ids".to_sym => i, "#{k}_types".to_sym => t }
13
- end
14
-
15
- attr_accessor :default_type, :default_id, :default_attribute,
16
- :default_has_one, :default_has_many
17
-
18
- def initialize
19
- self.default_type = DEFAULT_TYPE_BLOCK
20
- self.default_id = DEFAULT_ID_BLOCK
21
- self.default_attribute = DEFAULT_ATTR_BLOCK
22
- self.default_has_one = DEFAULT_HAS_ONE_BLOCK
23
- self.default_has_many = DEFAULT_HAS_MANY_BLOCK
24
- end
25
- end
26
- end
27
- end
28
- end