seri 1.0.2 → 1.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5adc32c82dea64e690e2a39b2b2dd1cbfc5bcd8816a8d34d271cd14d71edfd95
4
- data.tar.gz: 5415cb4b3ac84b365ad4dd34bb81dc3d3edc9d12697556b31078d74bc4a574c8
3
+ metadata.gz: '090b3dd7fc4be4bb07693768f57ae5b050a6227c893237d58ab278418ae5fd6b'
4
+ data.tar.gz: 8f163f78cd03f5587d8a4817e79b08082c6ba14f2e8fc900dcf3fb6f49d310f2
5
5
  SHA512:
6
- metadata.gz: 567a40baa057f07add6ad38aedaf6f04303e93a67d41e056a30f25adbd2ccb3580af5270f5ae15c6daf7db805f383aef191b1bd2754150f43965206073e688cc
7
- data.tar.gz: aa5d0a9a533845d21dd9fc9a9e7538eed5bc88bbb06b361cb0ce6454f9ce32f14a990e2b9e8425061068a0970d049529d5dcddea3118a2759f66ac1d5afe17fd
6
+ metadata.gz: a31af8088e22f3dc4e6b26f77c59d8eee27c1e0f43084d4cb0c261dc6f61fe825a704636c751cc11aec63b6a4e7393148ff237c8ad3219d20976049e3a2a6489
7
+ data.tar.gz: df133caeaf514a0ff001c426a5e5078ec99bfc5177b57690ade401e06b2ff473ffe34b2f0aeea38e73a69ba5fe89625022ffc5d5a2185c13c2b85a4dd737463c
@@ -0,0 +1,58 @@
1
+ class Value
2
+ def initialize(attribute, scope = nil)
3
+ @attribute = attribute
4
+ @scope = scope
5
+ end
6
+
7
+ def extraction_key
8
+ @attribute.from || @attribute.key
9
+ end
10
+
11
+ def precondition?
12
+ raise NotImplementedError, 'needs a method called precondition?'
13
+ end
14
+
15
+ def value
16
+ raise NotImplementedError, 'needs a method called value'
17
+ end
18
+ end
19
+
20
+ class StaticValue < Value
21
+ def precondition?
22
+ @attribute.options.key?(:static_value)
23
+ end
24
+
25
+ def value
26
+ @attribute.options.fetch(:static_value)
27
+ end
28
+ end
29
+
30
+ class SerializedValue < Value
31
+ def precondition?
32
+ @scope.respond_to?(extraction_key)
33
+ end
34
+
35
+ def value
36
+ @scope.public_send(extraction_key)
37
+ end
38
+ end
39
+
40
+ class HashValue < Value
41
+ def precondition?
42
+ @scope.is_a?(Hash) && @scope.key?(extraction_key)
43
+ end
44
+
45
+ def value
46
+ @scope.fetch(extraction_key)
47
+ end
48
+ end
49
+
50
+ class ObjectValue < Value
51
+ def precondition?
52
+ @scope.respond_to?(extraction_key)
53
+ end
54
+
55
+ def value
56
+ @scope.public_send(extraction_key)
57
+ end
58
+ end
@@ -0,0 +1,59 @@
1
+ class ValueFetcher
2
+ class SerializerError < StandardError; end
3
+
4
+ ARRAYS = %w[
5
+ Array
6
+ ActiveRecord_AssociationRelation
7
+ ActiveRecord_Associations_CollectionProxy
8
+ ].freeze
9
+
10
+ def self.fetch(attribute, object, serializer)
11
+ new(attribute, object, serializer).fetch
12
+ end
13
+
14
+ private_class_method :new
15
+
16
+ def initialize(attribute, object, serializer)
17
+ @attribute = attribute
18
+ @values = [
19
+ StaticValue.new(attribute),
20
+ SerializedValue.new(attribute, serializer),
21
+ HashValue.new(attribute, object),
22
+ ObjectValue.new(attribute, object)
23
+ ]
24
+ end
25
+
26
+ def fetch
27
+ serializer = @attribute.serializer
28
+
29
+ return value unless serializer
30
+
31
+ if is_array?
32
+ value.map { |item| serializer.new(item).to_h }
33
+ else
34
+ serializer.new(value).to_h
35
+ end
36
+ end
37
+
38
+ # Fetches a value from an attribute by checking if there's a ..
39
+ # .. static value set, or a ..
40
+ # .. method defined in the serializer, or a ..
41
+ # .. method/attribute defined in the object or ..
42
+ # .. it raises an error
43
+ def value
44
+ @value ||= begin
45
+ extracted_value = @values.detect(&:precondition?)
46
+
47
+ if extracted_value.nil?
48
+ raise SerializerError,
49
+ "unknown attribute '#{@values[0].extraction_key}'"
50
+ end
51
+
52
+ extracted_value.value
53
+ end
54
+ end
55
+
56
+ def is_array?
57
+ ARRAYS.any? { |match| value.class.to_s.end_with?(match) }
58
+ end
59
+ end
@@ -1,3 +1,3 @@
1
1
  class Serializer
2
- VERSION = '1.0.2'.freeze
2
+ VERSION = '1.1.2'.freeze
3
3
  end
data/lib/serializer.rb CHANGED
@@ -1,15 +1,9 @@
1
1
  require 'serializer/version'
2
+ require 'serializer/value'
3
+ require 'serializer/value_fetcher'
2
4
  require 'serializer/group_serializer'
3
5
 
4
6
  class Serializer
5
- class SerializerError < StandardError; end
6
-
7
- ARRAYS = %w[
8
- Array
9
- ActiveRecord_AssociationRelation
10
- ActiveRecord_Associations_CollectionProxy
11
- ].freeze
12
-
13
7
  Attribute = Struct.new(:key, :condition, :from, :serializer, :options)
14
8
 
15
9
  def self.attributes
@@ -32,7 +26,7 @@ class Serializer
32
26
  self.class.attributes.each_with_object({}) do |attribute, obj|
33
27
  next if attribute.condition && !public_send(attribute.condition)
34
28
 
35
- obj[attribute.key] = serialize_value(attribute)
29
+ obj[attribute.key] = ValueFetcher.fetch(attribute, object, self)
36
30
  end
37
31
  end
38
32
 
@@ -41,38 +35,4 @@ class Serializer
41
35
  Oj.dump(to_h, mode: :json)
42
36
  end
43
37
  end
44
-
45
- private
46
-
47
- def serialize_value(attribute)
48
- value = extract_value(attribute)
49
- serializer = attribute.serializer
50
-
51
- return value unless serializer
52
-
53
- if ARRAYS.any? { |match| value.class.to_s.end_with?(match) }
54
- value.map { |v| serializer.new(v).to_h }
55
- else
56
- serializer.new(value).to_h
57
- end
58
- end
59
-
60
- # Fetches a value from an attribute by checking if there's a ..
61
- # .. static value set, or a ..
62
- # .. method defined in the serializer, or a ..
63
- # .. method/attribute defined in the object or ..
64
- # .. it raises an error
65
- def extract_value(attribute)
66
- extraction_key = attribute.from || attribute.key
67
-
68
- if attribute.options.key?(:static_value)
69
- attribute.options.fetch(:static_value)
70
- elsif respond_to?(extraction_key)
71
- public_send(extraction_key)
72
- elsif object.respond_to?(extraction_key)
73
- object.public_send(extraction_key)
74
- else
75
- raise SerializerError, "unknown attribute '#{extraction_key}'"
76
- end
77
- end
78
38
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: seri
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - grdw
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-04-12 00:00:00.000000000 Z
11
+ date: 2019-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: appsignal
@@ -116,6 +116,8 @@ files:
116
116
  - bin/setup
117
117
  - lib/serializer.rb
118
118
  - lib/serializer/group_serializer.rb
119
+ - lib/serializer/value.rb
120
+ - lib/serializer/value_fetcher.rb
119
121
  - lib/serializer/version.rb
120
122
  - seri.gemspec
121
123
  homepage: https://github.com/grdw/seri