seri 1.1.11 → 2.0.0
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 +6 -6
- data/lib/seri.rb +1 -0
- data/lib/serializer.rb +23 -21
- data/lib/serializer/group_serializer.rb +16 -14
- data/lib/serializer/value.rb +44 -42
- data/lib/serializer/value_fetcher.rb +50 -48
- data/lib/serializer/version.rb +4 -2
- data/seri.gemspec +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a280328ee692f5395dd9ff0fe6bde3c684f6131131b69ed059a9ff58403a79ec
|
4
|
+
data.tar.gz: 4b817c4d1aa442835a5227ab0f88758644378322922884317e087c79f594f88f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 05fae53fffb74d5fbdf639a855703dba418fb2ce3d82ec81fde010cce67055c0be015f50953c54d7cbe260eb0f148216513ecca21e6c5d3de1b11cf03c954d77
|
7
|
+
data.tar.gz: 2828f68b71f05e813cd213a96e12c68078a5bdb373a74e8ea9021286d4a0745730ca45ee96573573454f887f191e65f7cc555e146c164fea5dfb2c939e33e2ef
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
seri (
|
4
|
+
seri (2.0.0)
|
5
5
|
oj (~> 3.7)
|
6
6
|
|
7
7
|
GEM
|
@@ -9,7 +9,7 @@ GEM
|
|
9
9
|
specs:
|
10
10
|
ast (2.4.1)
|
11
11
|
diff-lcs (1.3)
|
12
|
-
oj (3.10.
|
12
|
+
oj (3.10.8)
|
13
13
|
parallel (1.19.2)
|
14
14
|
parser (2.7.1.4)
|
15
15
|
ast (~> 2.4.1)
|
@@ -30,16 +30,16 @@ GEM
|
|
30
30
|
diff-lcs (>= 1.2.0, < 2.0)
|
31
31
|
rspec-support (~> 3.9.0)
|
32
32
|
rspec-support (3.9.2)
|
33
|
-
rubocop (0.
|
33
|
+
rubocop (0.88.0)
|
34
34
|
parallel (~> 1.10)
|
35
|
-
parser (>= 2.7.
|
35
|
+
parser (>= 2.7.1.1)
|
36
36
|
rainbow (>= 2.2.2, < 4.0)
|
37
37
|
regexp_parser (>= 1.7)
|
38
38
|
rexml
|
39
|
-
rubocop-ast (>= 0.0
|
39
|
+
rubocop-ast (>= 0.1.0, < 1.0)
|
40
40
|
ruby-progressbar (~> 1.7)
|
41
41
|
unicode-display_width (>= 1.4.0, < 2.0)
|
42
|
-
rubocop-ast (0.0
|
42
|
+
rubocop-ast (0.1.0)
|
43
43
|
parser (>= 2.7.0.1)
|
44
44
|
ruby-progressbar (1.10.1)
|
45
45
|
unicode-display_width (1.7.0)
|
data/lib/seri.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'serializer'
|
data/lib/serializer.rb
CHANGED
@@ -3,34 +3,36 @@ require 'serializer/value'
|
|
3
3
|
require 'serializer/value_fetcher'
|
4
4
|
require 'serializer/group_serializer'
|
5
5
|
|
6
|
-
|
7
|
-
|
6
|
+
module Seri
|
7
|
+
class Serializer
|
8
|
+
Attribute = Struct.new(:key, :condition, :from, :serializer, :options)
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
10
|
+
def self.attributes
|
11
|
+
@attributes ||= []
|
12
|
+
end
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
|
14
|
+
def self.attribute(key, condition: nil, from: nil, serializer: nil, **options)
|
15
|
+
attributes.push(Attribute.new(key, condition, from, serializer, options))
|
16
|
+
end
|
16
17
|
|
17
|
-
|
18
|
+
attr_accessor :object, :scope
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
20
|
+
def initialize(object, scope: {})
|
21
|
+
@object = object
|
22
|
+
@scope = scope
|
23
|
+
end
|
23
24
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
25
|
+
# Loops over all attributes and skips if a condition is defined and falsey
|
26
|
+
def to_h
|
27
|
+
self.class.attributes.each_with_object({}) do |attribute, obj|
|
28
|
+
next if attribute.condition && !public_send(attribute.condition)
|
28
29
|
|
29
|
-
|
30
|
+
obj[attribute.key] = ValueFetcher.fetch(attribute, object, self)
|
31
|
+
end
|
30
32
|
end
|
31
|
-
end
|
32
33
|
|
33
|
-
|
34
|
-
|
34
|
+
def to_json(*)
|
35
|
+
Oj.dump(to_h, mode: :json)
|
36
|
+
end
|
35
37
|
end
|
36
38
|
end
|
@@ -1,19 +1,21 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
module Seri
|
2
|
+
class GroupSerializer
|
3
|
+
def initialize(objects, serializer: nil, scope: {})
|
4
|
+
raise ArgumentError, 'serializer needs to be specified' if serializer.nil?
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
@objects = objects
|
7
|
+
@serializer = serializer
|
8
|
+
@scope = scope
|
9
|
+
end
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
11
|
+
def to_json(*)
|
12
|
+
Oj.dump(to_h, mode: :json)
|
13
|
+
end
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
def to_h
|
16
|
+
@objects
|
17
|
+
.map { |object| @serializer.new(object, scope: @scope) }
|
18
|
+
.map(&:to_h)
|
19
|
+
end
|
18
20
|
end
|
19
21
|
end
|
data/lib/serializer/value.rb
CHANGED
@@ -1,60 +1,62 @@
|
|
1
|
-
|
2
|
-
class
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
1
|
+
module Seri
|
2
|
+
class Serializer
|
3
|
+
class Value
|
4
|
+
def initialize(attribute, scope = nil)
|
5
|
+
@attribute = attribute
|
6
|
+
@scope = scope
|
7
|
+
end
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
9
|
+
def extraction_key
|
10
|
+
@attribute.from || @attribute.key
|
11
|
+
end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
def precondition?
|
14
|
+
raise NotImplementedError, 'needs a method called precondition?'
|
15
|
+
end
|
15
16
|
|
16
|
-
|
17
|
-
|
17
|
+
def value
|
18
|
+
raise NotImplementedError, 'needs a method called value'
|
19
|
+
end
|
18
20
|
end
|
19
|
-
end
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
22
|
+
class StaticValue < Value
|
23
|
+
def precondition?
|
24
|
+
@attribute.options.key?(:static_value)
|
25
|
+
end
|
25
26
|
|
26
|
-
|
27
|
-
|
27
|
+
def value
|
28
|
+
@attribute.options.fetch(:static_value)
|
29
|
+
end
|
28
30
|
end
|
29
|
-
end
|
30
31
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
32
|
+
class SerializedValue < Value
|
33
|
+
def precondition?
|
34
|
+
@scope.respond_to?(extraction_key)
|
35
|
+
end
|
35
36
|
|
36
|
-
|
37
|
-
|
37
|
+
def value
|
38
|
+
@scope.public_send(extraction_key)
|
39
|
+
end
|
38
40
|
end
|
39
|
-
end
|
40
41
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
42
|
+
class HashValue < Value
|
43
|
+
def precondition?
|
44
|
+
@scope.is_a?(Hash)
|
45
|
+
end
|
45
46
|
|
46
|
-
|
47
|
-
|
47
|
+
def value
|
48
|
+
@scope[extraction_key]
|
49
|
+
end
|
48
50
|
end
|
49
|
-
end
|
50
51
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
52
|
+
class ObjectValue < Value
|
53
|
+
def precondition?
|
54
|
+
@scope.respond_to?(extraction_key)
|
55
|
+
end
|
55
56
|
|
56
|
-
|
57
|
-
|
57
|
+
def value
|
58
|
+
@scope.public_send(extraction_key)
|
59
|
+
end
|
58
60
|
end
|
59
61
|
end
|
60
62
|
end
|
@@ -1,61 +1,63 @@
|
|
1
|
-
|
2
|
-
class
|
3
|
-
class
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
1
|
+
module Seri
|
2
|
+
class Serializer
|
3
|
+
class ValueFetcher
|
4
|
+
class SerializerError < StandardError; end
|
5
|
+
|
6
|
+
ARRAYS = %w[
|
7
|
+
Array
|
8
|
+
ActiveRecord_AssociationRelation
|
9
|
+
ActiveRecord_Associations_CollectionProxy
|
10
|
+
].freeze
|
11
|
+
|
12
|
+
def self.fetch(attribute, object, serializer)
|
13
|
+
new(attribute, object, serializer).fetch
|
14
|
+
end
|
14
15
|
|
15
|
-
|
16
|
+
private_class_method :new
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
18
|
+
def initialize(attribute, object, serializer)
|
19
|
+
@attribute = attribute
|
20
|
+
@values = [
|
21
|
+
StaticValue.new(attribute),
|
22
|
+
SerializedValue.new(attribute, serializer),
|
23
|
+
HashValue.new(attribute, object),
|
24
|
+
ObjectValue.new(attribute, object)
|
25
|
+
]
|
26
|
+
end
|
26
27
|
|
27
|
-
|
28
|
-
|
28
|
+
def fetch
|
29
|
+
serializer = @attribute.serializer
|
29
30
|
|
30
|
-
|
31
|
+
return value unless serializer
|
31
32
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
33
|
+
if array?
|
34
|
+
value.map { |item| serializer.new(item).to_h }
|
35
|
+
else
|
36
|
+
serializer.new(value).to_h
|
37
|
+
end
|
36
38
|
end
|
37
|
-
end
|
38
39
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
40
|
+
# Fetches a value from an attribute by checking if there's a ..
|
41
|
+
# .. static value set, or a ..
|
42
|
+
# .. method defined in the serializer, or a ..
|
43
|
+
# .. method/attribute defined in the object or ..
|
44
|
+
# .. it raises an error
|
45
|
+
def value
|
46
|
+
@value ||= begin
|
47
|
+
extracted_value = @values.detect(&:precondition?)
|
48
|
+
|
49
|
+
if extracted_value.nil?
|
50
|
+
raise SerializerError,
|
51
|
+
"unknown attribute '#{@values[0].extraction_key}'"
|
52
|
+
end
|
53
|
+
|
54
|
+
extracted_value.value
|
51
55
|
end
|
52
|
-
|
53
|
-
extracted_value.value
|
54
56
|
end
|
55
|
-
end
|
56
57
|
|
57
|
-
|
58
|
-
|
58
|
+
def array?
|
59
|
+
ARRAYS.any? { |match| value.class.to_s.end_with?(match) }
|
60
|
+
end
|
59
61
|
end
|
60
62
|
end
|
61
63
|
end
|
data/lib/serializer/version.rb
CHANGED
data/seri.gemspec
CHANGED
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:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- grdw
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-08-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oj
|
@@ -98,6 +98,7 @@ files:
|
|
98
98
|
- LICENSE.txt
|
99
99
|
- README.md
|
100
100
|
- Rakefile
|
101
|
+
- lib/seri.rb
|
101
102
|
- lib/serializer.rb
|
102
103
|
- lib/serializer/group_serializer.rb
|
103
104
|
- lib/serializer/value.rb
|