light_serializer 0.0.4 → 0.0.5

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
  SHA256:
3
- metadata.gz: a3198639643c0b5f2580775f5ce64ccd4da834b0d4b1b06ace981a94c1eab325
4
- data.tar.gz: f87b229f92734a7e7f4b02646df7d162be92c7a426ca2abb53273878fb9d0875
3
+ metadata.gz: dc42bdb60c1c303a13fa6cb066dceb052f762612019c932c7db38363f8b5df94
4
+ data.tar.gz: 2464fcf561ba306e7c8c6b6377d9c3d4ed6f3ad9f0249b9836de002505bbcc56
5
5
  SHA512:
6
- metadata.gz: d56e18f443575c02c8d112db9b9ac2d150a34bdab8174e7dd9fe66c80e3373068b9be9b84895123f8445eb81d1c93ae0ab004e37043b50bed8f83485b94cd77e
7
- data.tar.gz: 25b04c4068c9d3ace57ed16ad555b44a89e6b1c093cc7eb0dc015a4d3477146a32e00e12b2a792de2a124de7bf48f5d319311eb457db459801d4ac0c9ecb44c6
6
+ metadata.gz: '00884b745cedcac93b091fbdc3e58ce2c9d848112134580fa42c057736e680e26318fb4e3a09932fbd0781d504f51a871fc50a1a318e9ec8d6e7d56d4b6a4d4f'
7
+ data.tar.gz: 4a4b08cdc134007968182bcf6abf4512ccd720e8dc343f5367c75b0f7dcacb3b82d078f300a5f62086b1cba84d4c65cdfe2957764ef1fa5f1b0629ec5af10af4
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- light_serializer (0.0.4)
4
+ light_serializer (0.0.5)
5
5
  oj (~> 3)
6
6
 
7
7
  GEM
@@ -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 do
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] = nested_serializer.new(value, context: serializer.context).to_hash
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,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LightSerializer::Helpers
4
+ module WithCustomRoot
5
+ def with_custom_root(root)
6
+ root ? { root.to_sym => yield } : yield
7
+ end
8
+ end
9
+ end
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LightSerializer
4
- VERSION = '0.0.4'
4
+ VERSION = '0.0.5'
5
5
  end
@@ -3,6 +3,8 @@
3
3
  module LightSerializer
4
4
  end
5
5
 
6
+ require 'light_serializer/helpers/with_custom_root'
6
7
  require 'light_serializer/hashed_object'
7
8
  require 'light_serializer/serializer'
9
+ require 'light_serializer/serialize_collection'
8
10
  require 'light_serializer/version'
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
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-06 00:00:00.000000000 Z
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