seri 1.0.2 → 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/serializer/value.rb +58 -0
- data/lib/serializer/value_fetcher.rb +59 -0
- data/lib/serializer/version.rb +1 -1
- data/lib/serializer.rb +3 -43
- 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: '090b3dd7fc4be4bb07693768f57ae5b050a6227c893237d58ab278418ae5fd6b'
|
4
|
+
data.tar.gz: 8f163f78cd03f5587d8a4817e79b08082c6ba14f2e8fc900dcf3fb6f49d310f2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
data/lib/serializer/version.rb
CHANGED
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] =
|
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.
|
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-
|
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
|