light_serializer 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/light_serializer/hashed_object.rb +17 -16
- data/lib/light_serializer/helpers/with_custom_root.rb +9 -0
- data/lib/light_serializer/serialize_collection.rb +33 -0
- data/lib/light_serializer/version.rb +1 -1
- data/lib/light_serializer.rb +2 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dc42bdb60c1c303a13fa6cb066dceb052f762612019c932c7db38363f8b5df94
|
4
|
+
data.tar.gz: 2464fcf561ba306e7c8c6b6377d9c3d4ed6f3ad9f0249b9836de002505bbcc56
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '00884b745cedcac93b091fbdc3e58ce2c9d848112134580fa42c057736e680e26318fb4e3a09932fbd0781d504f51a871fc50a1a318e9ec8d6e7d56d4b6a4d4f'
|
7
|
+
data.tar.gz: 4a4b08cdc134007968182bcf6abf4512ccd720e8dc343f5367c75b0f7dcacb3b82d078f300a5f62086b1cba84d4c65cdfe2957764ef1fa5f1b0629ec5af10af4
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'light_serializer/helpers/with_custom_root'
|
4
|
+
|
3
5
|
module LightSerializer
|
4
6
|
class HashedObject
|
7
|
+
include Helpers::WithCustomRoot
|
8
|
+
|
5
9
|
attr_reader :raw_object, :serializer
|
6
10
|
|
7
11
|
def self.get(*args)
|
@@ -14,25 +18,11 @@ module LightSerializer
|
|
14
18
|
end
|
15
19
|
|
16
20
|
def get
|
17
|
-
with_custom_root
|
18
|
-
if raw_object.is_a?(Enumerable)
|
19
|
-
raw_object.map { |entity| transform_to_hash(entity) }
|
20
|
-
else
|
21
|
-
transform_to_hash(raw_object)
|
22
|
-
end
|
23
|
-
end
|
21
|
+
with_custom_root(serializer.root) { transform_to_hash(raw_object) }
|
24
22
|
end
|
25
23
|
|
26
24
|
private
|
27
25
|
|
28
|
-
def with_custom_root
|
29
|
-
custom_root ? { custom_root.to_sym => yield } : yield
|
30
|
-
end
|
31
|
-
|
32
|
-
def custom_root
|
33
|
-
@custom_root ||= serializer.root
|
34
|
-
end
|
35
|
-
|
36
26
|
def transform_to_hash(object)
|
37
27
|
serializer.class.attributes.each_with_object({}) do |attribute, result|
|
38
28
|
obtain_values(attribute, object, result)
|
@@ -50,8 +40,19 @@ module LightSerializer
|
|
50
40
|
def values_from_nested_resource(attribute, object, result)
|
51
41
|
attribute_name = attribute.keys.last
|
52
42
|
nested_serializer = attribute.values.last
|
43
|
+
|
53
44
|
value = obtain_value(object, attribute_name)
|
54
|
-
result[attribute_name] =
|
45
|
+
result[attribute_name] = hashed_nested_resource(value, nested_serializer)
|
46
|
+
end
|
47
|
+
|
48
|
+
def hashed_nested_resource(value, nested_serializer)
|
49
|
+
return hashed_entity(value, nested_serializer) unless value.is_a?(Enumerable)
|
50
|
+
|
51
|
+
value.each_with_object(nested_serializer).map(&method(:hashed_entity))
|
52
|
+
end
|
53
|
+
|
54
|
+
def hashed_entity(entity, nested_serializer)
|
55
|
+
nested_serializer.new(entity, context: serializer.context).to_hash
|
55
56
|
end
|
56
57
|
|
57
58
|
def values_from_current_resource(attribute, object, result)
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'light_serializer/helpers/with_custom_root'
|
4
|
+
require 'light_serializer/serializer'
|
5
|
+
|
6
|
+
module LightSerializer
|
7
|
+
class SerializeCollection
|
8
|
+
include Helpers::WithCustomRoot
|
9
|
+
|
10
|
+
attr_reader :collection, :serializer, :root, :context
|
11
|
+
|
12
|
+
def initialize(collection, serializer:, root: nil, context: nil)
|
13
|
+
@collection = collection
|
14
|
+
@serializer = serializer
|
15
|
+
@root = root
|
16
|
+
@context = context
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_json
|
20
|
+
with_custom_root(root) { Oj.dump(hashed_collection, mode: :compat) }
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_hash
|
24
|
+
hashed_collection
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def hashed_collection
|
30
|
+
collection.map { |entity| serializer.new(entity, context: context).to_hash }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/lib/light_serializer.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: light_serializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pavel Rodionov
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2018-11-
|
12
|
+
date: 2018-11-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: oj
|
@@ -157,6 +157,8 @@ files:
|
|
157
157
|
- bin/test
|
158
158
|
- lib/light_serializer.rb
|
159
159
|
- lib/light_serializer/hashed_object.rb
|
160
|
+
- lib/light_serializer/helpers/with_custom_root.rb
|
161
|
+
- lib/light_serializer/serialize_collection.rb
|
160
162
|
- lib/light_serializer/serializer.rb
|
161
163
|
- lib/light_serializer/version.rb
|
162
164
|
- light_serializer.gemspec
|