seri 1.1.9 → 1.1.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +2 -1
- data/.rubocop.yml +2 -0
- data/Gemfile.lock +10 -10
- data/lib/serializer/value.rb +43 -41
- data/lib/serializer/value_fetcher.rb +49 -47
- data/lib/serializer/version.rb +1 -1
- data/seri.gemspec +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 394f107e4e7ca59a2c4951316e4223ed8f9dec2d14d70610be80079a69974cb2
|
4
|
+
data.tar.gz: 59e58c2f0802df12dff7f3ec1da988978d95c2c1e427113124b3c488fa4bed0c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 004c1bf17667b3da131e4695e9a093d04d4a975ebf4f86644b0bdd4765753b4549120115484ab8285d275b938cc299e31e6270359a9a37a87b5ca055c44f885c
|
7
|
+
data.tar.gz: c3870a1ecd13aae4f159fa8b82547c06edb0015bad397822bb209a0d34e85b2d535545bd5d03ddd7defa542b5f316f352f3c57f7208d28c14224153e79ebc173
|
data/.circleci/config.yml
CHANGED
@@ -4,7 +4,7 @@ jobs:
|
|
4
4
|
build:
|
5
5
|
docker:
|
6
6
|
# specify the version you desire here
|
7
|
-
- image: circleci/ruby:2.
|
7
|
+
- image: circleci/ruby:2.7.1
|
8
8
|
|
9
9
|
working_directory: ~/repo
|
10
10
|
|
@@ -21,6 +21,7 @@ jobs:
|
|
21
21
|
- run:
|
22
22
|
name: install dependencies
|
23
23
|
command: |
|
24
|
+
gem install bundler:1.17.3
|
24
25
|
bundle install --jobs=4 --retry=3 --path vendor/bundle
|
25
26
|
|
26
27
|
- save_cache:
|
data/.rubocop.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
seri (1.1.
|
4
|
+
seri (1.1.10)
|
5
5
|
oj (~> 3.7)
|
6
6
|
|
7
7
|
GEM
|
@@ -10,13 +10,13 @@ GEM
|
|
10
10
|
ast (2.4.0)
|
11
11
|
diff-lcs (1.3)
|
12
12
|
jaro_winkler (1.5.4)
|
13
|
-
oj (3.10.
|
13
|
+
oj (3.10.6)
|
14
14
|
parallel (1.19.1)
|
15
|
-
parser (2.7.
|
15
|
+
parser (2.7.1.2)
|
16
16
|
ast (~> 2.4.0)
|
17
|
-
psych (3.1.0)
|
18
17
|
rainbow (3.0.0)
|
19
18
|
rake (13.0.1)
|
19
|
+
rexml (3.2.4)
|
20
20
|
rspec (3.9.0)
|
21
21
|
rspec-core (~> 3.9.0)
|
22
22
|
rspec-expectations (~> 3.9.0)
|
@@ -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.82.0)
|
34
34
|
jaro_winkler (~> 1.5.1)
|
35
35
|
parallel (~> 1.10)
|
36
|
-
parser (>= 2.
|
37
|
-
psych (>= 3.1.0)
|
36
|
+
parser (>= 2.7.0.1)
|
38
37
|
rainbow (>= 2.2.2, < 4.0)
|
38
|
+
rexml
|
39
39
|
ruby-progressbar (~> 1.7)
|
40
|
-
unicode-display_width (>= 1.4.0, <
|
40
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
41
41
|
ruby-progressbar (1.10.1)
|
42
|
-
unicode-display_width (1.
|
42
|
+
unicode-display_width (1.7.0)
|
43
43
|
|
44
44
|
PLATFORMS
|
45
45
|
ruby
|
@@ -48,7 +48,7 @@ DEPENDENCIES
|
|
48
48
|
bundler (~> 1.16)
|
49
49
|
rake (~> 13.0)
|
50
50
|
rspec (~> 3.0)
|
51
|
-
rubocop (~> 0.
|
51
|
+
rubocop (~> 0.82)
|
52
52
|
seri!
|
53
53
|
|
54
54
|
BUNDLED WITH
|
data/lib/serializer/value.rb
CHANGED
@@ -1,58 +1,60 @@
|
|
1
|
-
class
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
1
|
+
class Serializer
|
2
|
+
class Value
|
3
|
+
def initialize(attribute, scope = nil)
|
4
|
+
@attribute = attribute
|
5
|
+
@scope = scope
|
6
|
+
end
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
8
|
+
def extraction_key
|
9
|
+
@attribute.from || @attribute.key
|
10
|
+
end
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
12
|
+
def precondition?
|
13
|
+
raise NotImplementedError, 'needs a method called precondition?'
|
14
|
+
end
|
14
15
|
|
15
|
-
|
16
|
-
|
16
|
+
def value
|
17
|
+
raise NotImplementedError, 'needs a method called value'
|
18
|
+
end
|
17
19
|
end
|
18
|
-
end
|
19
20
|
|
20
|
-
class StaticValue < Value
|
21
|
-
|
22
|
-
|
23
|
-
|
21
|
+
class StaticValue < Value
|
22
|
+
def precondition?
|
23
|
+
@attribute.options.key?(:static_value)
|
24
|
+
end
|
24
25
|
|
25
|
-
|
26
|
-
|
26
|
+
def value
|
27
|
+
@attribute.options.fetch(:static_value)
|
28
|
+
end
|
27
29
|
end
|
28
|
-
end
|
29
30
|
|
30
|
-
class SerializedValue < Value
|
31
|
-
|
32
|
-
|
33
|
-
|
31
|
+
class SerializedValue < Value
|
32
|
+
def precondition?
|
33
|
+
@scope.respond_to?(extraction_key)
|
34
|
+
end
|
34
35
|
|
35
|
-
|
36
|
-
|
36
|
+
def value
|
37
|
+
@scope.public_send(extraction_key)
|
38
|
+
end
|
37
39
|
end
|
38
|
-
end
|
39
40
|
|
40
|
-
class HashValue < Value
|
41
|
-
|
42
|
-
|
43
|
-
|
41
|
+
class HashValue < Value
|
42
|
+
def precondition?
|
43
|
+
@scope.is_a?(Hash)
|
44
|
+
end
|
44
45
|
|
45
|
-
|
46
|
-
|
46
|
+
def value
|
47
|
+
@scope[extraction_key]
|
48
|
+
end
|
47
49
|
end
|
48
|
-
end
|
49
50
|
|
50
|
-
class ObjectValue < Value
|
51
|
-
|
52
|
-
|
53
|
-
|
51
|
+
class ObjectValue < Value
|
52
|
+
def precondition?
|
53
|
+
@scope.respond_to?(extraction_key)
|
54
|
+
end
|
54
55
|
|
55
|
-
|
56
|
-
|
56
|
+
def value
|
57
|
+
@scope.public_send(extraction_key)
|
58
|
+
end
|
57
59
|
end
|
58
60
|
end
|
@@ -1,59 +1,61 @@
|
|
1
|
-
class
|
2
|
-
class
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
1
|
+
class Serializer
|
2
|
+
class ValueFetcher
|
3
|
+
class SerializerError < StandardError; end
|
4
|
+
|
5
|
+
ARRAYS = %w[
|
6
|
+
Array
|
7
|
+
ActiveRecord_AssociationRelation
|
8
|
+
ActiveRecord_Associations_CollectionProxy
|
9
|
+
].freeze
|
10
|
+
|
11
|
+
def self.fetch(attribute, object, serializer)
|
12
|
+
new(attribute, object, serializer).fetch
|
13
|
+
end
|
13
14
|
|
14
|
-
|
15
|
+
private_class_method :new
|
15
16
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
17
|
+
def initialize(attribute, object, serializer)
|
18
|
+
@attribute = attribute
|
19
|
+
@values = [
|
20
|
+
StaticValue.new(attribute),
|
21
|
+
SerializedValue.new(attribute, serializer),
|
22
|
+
HashValue.new(attribute, object),
|
23
|
+
ObjectValue.new(attribute, object)
|
24
|
+
]
|
25
|
+
end
|
25
26
|
|
26
|
-
|
27
|
-
|
27
|
+
def fetch
|
28
|
+
serializer = @attribute.serializer
|
28
29
|
|
29
|
-
|
30
|
+
return value unless serializer
|
30
31
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
32
|
+
if array?
|
33
|
+
value.map { |item| serializer.new(item).to_h }
|
34
|
+
else
|
35
|
+
serializer.new(value).to_h
|
36
|
+
end
|
35
37
|
end
|
36
|
-
end
|
37
38
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
39
|
+
# Fetches a value from an attribute by checking if there's a ..
|
40
|
+
# .. static value set, or a ..
|
41
|
+
# .. method defined in the serializer, or a ..
|
42
|
+
# .. method/attribute defined in the object or ..
|
43
|
+
# .. it raises an error
|
44
|
+
def value
|
45
|
+
@value ||= begin
|
46
|
+
extracted_value = @values.detect(&:precondition?)
|
47
|
+
|
48
|
+
if extracted_value.nil?
|
49
|
+
raise SerializerError,
|
50
|
+
"unknown attribute '#{@values[0].extraction_key}'"
|
51
|
+
end
|
52
|
+
|
53
|
+
extracted_value.value
|
50
54
|
end
|
51
|
-
|
52
|
-
extracted_value.value
|
53
55
|
end
|
54
|
-
end
|
55
56
|
|
56
|
-
|
57
|
-
|
57
|
+
def array?
|
58
|
+
ARRAYS.any? { |match| value.class.to_s.end_with?(match) }
|
59
|
+
end
|
58
60
|
end
|
59
61
|
end
|
data/lib/serializer/version.rb
CHANGED
data/seri.gemspec
CHANGED
@@ -29,5 +29,5 @@ Gem::Specification.new do |spec|
|
|
29
29
|
spec.add_development_dependency 'bundler', '~> 1.16'
|
30
30
|
spec.add_development_dependency 'rake', '~> 13.0'
|
31
31
|
spec.add_development_dependency 'rspec', '~> 3.0'
|
32
|
-
spec.add_development_dependency 'rubocop', '~> 0.
|
32
|
+
spec.add_development_dependency 'rubocop', '~> 0.82'
|
33
33
|
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.1.
|
4
|
+
version: 1.1.10
|
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-05-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oj
|
@@ -72,14 +72,14 @@ dependencies:
|
|
72
72
|
requirements:
|
73
73
|
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: 0.
|
75
|
+
version: '0.82'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: 0.
|
82
|
+
version: '0.82'
|
83
83
|
description: A basic serializer
|
84
84
|
email:
|
85
85
|
- gerard@wetransfer.com
|