dry_serialization 0.4.3 → 0.4.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/dry_serialization.gemspec +1 -1
- data/lib/dry_serialization.rb +1 -1
- data/lib/dry_serialization/concerns/deserializable.rb +69 -0
- data/lib/dry_serialization/concerns/serialization_helper.rb +2 -2
- data/lib/dry_serialization/{fast_jsonapi.rb → jsonapi_serializer.rb} +1 -1
- data/lib/dry_serialization/version.rb +1 -1
- data/lib/generators/base_generator.rb +2 -2
- data/lib/generators/dry_serialization/blueprinter/install_generator.rb +1 -1
- data/lib/generators/dry_serialization/{fast_jsonapi → jsonapi_serializer}/install_generator.rb +6 -6
- metadata +6 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a362cbf5323852dddb9104b617f76efd6288bbecb351a56b614cf0c6fd084250
|
4
|
+
data.tar.gz: a5e62c311423e5a8265448f9caa20685cf7b2a0d93d6395dd82eef9913fb4b44
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d342355ee030f28689dc0fc496020f69e3be986b782c154cc101f9bb3b628e44ca22ade883d64a6a58be1f47aa763d9bdfafc1609dac4f7e6fb6d6d49613d82f
|
7
|
+
data.tar.gz: 8c31a0f9f14265bb47d688e6eae8b9aac459c467252384ba5eb609d0c5efe830c20e107abe23aa04e746b5bd4a8b3a019e12c01c594b848cc2b5562ce617770f
|
data/README.md
CHANGED
@@ -27,7 +27,7 @@ Or install it yourself as:
|
|
27
27
|
## Usage
|
28
28
|
|
29
29
|
- Install with rails generator
|
30
|
-
* currently supported serializers are
|
30
|
+
* currently supported serializers are [jsonapi_serializer](https://github.com/jsonapi-serializer/jsonapi-serializer) and [blueprinter](https://github.com/procore/blueprinter)
|
31
31
|
|
32
32
|
|
33
33
|
`rails g dry_serialization:<serializer_name>:install`
|
data/dry_serialization.gemspec
CHANGED
@@ -30,5 +30,5 @@ Gem::Specification.new do |spec|
|
|
30
30
|
spec.add_development_dependency 'activerecord'
|
31
31
|
spec.add_development_dependency 'sqlite3', '~> 1.4'
|
32
32
|
spec.add_development_dependency 'yard', '~> 0.9.11'
|
33
|
-
spec.add_development_dependency '
|
33
|
+
spec.add_development_dependency 'jsonapi_serializer'
|
34
34
|
end
|
data/lib/dry_serialization.rb
CHANGED
@@ -0,0 +1,69 @@
|
|
1
|
+
module DrySerialization
|
2
|
+
module Deserializable
|
3
|
+
|
4
|
+
def deserialized_payload(base_params, object = nil)
|
5
|
+
attrs = attributes(base_params)
|
6
|
+
id = dig_id(base_params, object)
|
7
|
+
attrs.merge!(id: id) if id
|
8
|
+
|
9
|
+
build_nested_relationships(attrs, base_params)
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def build_nested_relationships(attrs, base_params)
|
15
|
+
relations = relationships(base_params)
|
16
|
+
attrs.merge!(transform_object_values(relations))
|
17
|
+
end
|
18
|
+
|
19
|
+
def transform_object_values(relations)
|
20
|
+
relations.to_h.each_with_object({}) do |(key, data), acc|
|
21
|
+
key = "#{key}_attributes" unless key.end_with?('_attributes')
|
22
|
+
acc[key] ||= transform_relationships_to_get_attributes(data)
|
23
|
+
transform_nested_relationships_to_get_attributes(acc[key], data)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def transform_nested_relationships_to_get_attributes(parent_relation_hash, data)
|
28
|
+
nested_relationships = data[:data].respond_to?(:key?) ? relationships(data) : nil
|
29
|
+
return unless nested_relationships
|
30
|
+
|
31
|
+
nested_relationships.each do |(relation, rel_data)|
|
32
|
+
hash = { "#{relation}_attributes": transform_relationships_to_get_attributes(rel_data) }
|
33
|
+
parent_relation_hash.merge!(hash)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def transform_relationships_to_get_attributes(object)
|
38
|
+
case object[:data]
|
39
|
+
when Hash
|
40
|
+
object[:attributes] || object.dig(:data, :attributes)
|
41
|
+
when Array
|
42
|
+
object[:data].map do |e|
|
43
|
+
attrs = transform_relationships_to_get_attributes({ data: e })
|
44
|
+
attrs.merge!(transform_object_values(e[:relationships]))
|
45
|
+
end
|
46
|
+
else
|
47
|
+
return
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def dig_id(base_params, object)
|
52
|
+
return unless object
|
53
|
+
|
54
|
+
base_params.dig(:data, :id)
|
55
|
+
send("#{object}_id")
|
56
|
+
rescue
|
57
|
+
nil
|
58
|
+
end
|
59
|
+
|
60
|
+
def attributes(base_params)
|
61
|
+
base_params.dig(:data, :attributes)
|
62
|
+
end
|
63
|
+
|
64
|
+
def relationships(base_params)
|
65
|
+
base_params.dig(:data, :relationships)
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
end
|
@@ -9,12 +9,12 @@ module DrySerialization
|
|
9
9
|
errors = [errors] unless errors.is_a?(Array)
|
10
10
|
errors = handle_errors(errors)
|
11
11
|
|
12
|
-
render json: errors, status: status
|
12
|
+
render json: { errors: errors }, status: status
|
13
13
|
end
|
14
14
|
|
15
15
|
private
|
16
16
|
|
17
|
-
def handle_errors(
|
17
|
+
def handle_errors(errors)
|
18
18
|
errors.each_with_object([]) { |error, acc| acc << { detail: error } }
|
19
19
|
end
|
20
20
|
|
@@ -5,7 +5,7 @@ class BaseGenerator < Rails::Generators::Base
|
|
5
5
|
SERIALIZERS = {
|
6
6
|
ams: 'ActiveModelSerializers',
|
7
7
|
blueprinter: 'Blueprinter',
|
8
|
-
|
8
|
+
jsonapi_serializer: 'JsonapiSerializer'
|
9
9
|
}
|
10
10
|
|
11
11
|
def self.exit_on_failure?
|
@@ -14,7 +14,7 @@ class BaseGenerator < Rails::Generators::Base
|
|
14
14
|
|
15
15
|
private
|
16
16
|
|
17
|
-
# Removes other serialization gems, currently just AMS and
|
17
|
+
# Removes other serialization gems, currently just AMS and JsonapiSerializer
|
18
18
|
def remove_other_supported_gems(*gems)
|
19
19
|
gems = [gems] unless gems.is_a?(Array)
|
20
20
|
return if gems.empty?
|
@@ -7,7 +7,7 @@ module DrySerialization
|
|
7
7
|
|
8
8
|
# Add blueprinter gem to gemfile after dry_serialization declaration and bundles the newly declared gem
|
9
9
|
def install_blueprinter
|
10
|
-
remove_other_supported_gems(SERIALIZERS[:ams], SERIALIZERS[:
|
10
|
+
remove_other_supported_gems(SERIALIZERS[:ams], SERIALIZERS[:jsonapi_serializer])
|
11
11
|
|
12
12
|
puts "Installing #{SERIALIZERS[:blueprinter]}..."
|
13
13
|
insert_into_file('Gemfile',
|
data/lib/generators/dry_serialization/{fast_jsonapi → jsonapi_serializer}/install_generator.rb
RENAMED
@@ -1,21 +1,21 @@
|
|
1
1
|
require 'generators/base_generator'
|
2
2
|
|
3
3
|
module DrySerialization
|
4
|
-
module
|
4
|
+
module JsonapiSerializer
|
5
5
|
class InstallGenerator < BaseGenerator
|
6
6
|
source_root File.expand_path("../../../templates", __FILE__)
|
7
7
|
|
8
8
|
# Add blueprinter gem to gemfile after dry_serialization declaration and bundles the newly declared gem
|
9
|
-
def
|
9
|
+
def install_jsonapi_serializer
|
10
10
|
remove_other_supported_gems(SERIALIZERS[:blueprinter], SERIALIZERS[:ams])
|
11
11
|
|
12
|
-
puts "Installing #{SERIALIZERS[:
|
12
|
+
puts "Installing #{SERIALIZERS[:jsonapi_serializer]}..."
|
13
13
|
insert_into_file('Gemfile',
|
14
|
-
"\ngem '
|
15
|
-
after: "gem 'dry_serialization'
|
14
|
+
"\ngem 'jsonapi_serializer'",
|
15
|
+
after: "gem 'dry_serialization'")
|
16
16
|
run 'bundle install'
|
17
17
|
|
18
|
-
helper_include(SERIALIZERS[:
|
18
|
+
helper_include(SERIALIZERS[:jsonapi_serializer])
|
19
19
|
end
|
20
20
|
|
21
21
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dry_serialization
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Heft
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-10-
|
11
|
+
date: 2020-10-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -137,7 +137,7 @@ dependencies:
|
|
137
137
|
- !ruby/object:Gem::Version
|
138
138
|
version: 0.9.11
|
139
139
|
- !ruby/object:Gem::Dependency
|
140
|
-
name:
|
140
|
+
name: jsonapi_serializer
|
141
141
|
requirement: !ruby/object:Gem::Requirement
|
142
142
|
requirements:
|
143
143
|
- - ">="
|
@@ -162,12 +162,13 @@ files:
|
|
162
162
|
- dry_serialization.gemspec
|
163
163
|
- lib/dry_serialization.rb
|
164
164
|
- lib/dry_serialization/blueprinter.rb
|
165
|
+
- lib/dry_serialization/concerns/deserializable.rb
|
165
166
|
- lib/dry_serialization/concerns/serialization_helper.rb
|
166
|
-
- lib/dry_serialization/
|
167
|
+
- lib/dry_serialization/jsonapi_serializer.rb
|
167
168
|
- lib/dry_serialization/version.rb
|
168
169
|
- lib/generators/base_generator.rb
|
169
170
|
- lib/generators/dry_serialization/blueprinter/install_generator.rb
|
170
|
-
- lib/generators/dry_serialization/
|
171
|
+
- lib/generators/dry_serialization/jsonapi_serializer/install_generator.rb
|
171
172
|
- lib/generators/templates/api_controller.rb
|
172
173
|
homepage:
|
173
174
|
licenses:
|