seri 1.1.10 → 2.0.3

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: 394f107e4e7ca59a2c4951316e4223ed8f9dec2d14d70610be80079a69974cb2
4
- data.tar.gz: 59e58c2f0802df12dff7f3ec1da988978d95c2c1e427113124b3c488fa4bed0c
3
+ metadata.gz: 97c393ef117d1729bbd8459bdd47c1726e8a6a1436e39cb73bb2108bd0817bec
4
+ data.tar.gz: 519552ef6a2ce6de01293fcbfce32a7afdbc4535d56233bda5d8c4687b45e3db
5
5
  SHA512:
6
- metadata.gz: 004c1bf17667b3da131e4695e9a093d04d4a975ebf4f86644b0bdd4765753b4549120115484ab8285d275b938cc299e31e6270359a9a37a87b5ca055c44f885c
7
- data.tar.gz: c3870a1ecd13aae4f159fa8b82547c06edb0015bad397822bb209a0d34e85b2d535545bd5d03ddd7defa542b5f316f352f3c57f7208d28c14224153e79ebc173
6
+ metadata.gz: a8df6fab6b789c59070760a40c5a95251933bda9eaaace98b2eed8275e75b35e967877640195fd4a04f3592c7b62c4adb51aeaf3bd0f00a2169b50ab6d2af755
7
+ data.tar.gz: 6fee2110d91fd341a46d234a4203059e95623f721d31b12637c2369302e22d00541616b24b3e35eb55eba6450d0e3c6755642a9ce84408f02d517f6230a0f596
@@ -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.1
7
+ - image: circleci/ruby:3.0.0
8
8
 
9
9
  working_directory: ~/repo
10
10
 
@@ -21,7 +21,7 @@ jobs:
21
21
  - run:
22
22
  name: install dependencies
23
23
  command: |
24
- gem install bundler:1.17.3
24
+ gem install bundler:2.2.5
25
25
  bundle install --jobs=4 --retry=3 --path vendor/bundle
26
26
 
27
27
  - save_cache:
@@ -1,3 +1,5 @@
1
+ AllCops:
2
+ NewCops: disable
1
3
  Metrics/BlockLength:
2
4
  Exclude:
3
5
  - 'spec/**/*.rb'
@@ -8,3 +10,5 @@ Style/FrozenStringLiteralComment:
8
10
  # This is causing breaking changes:
9
11
  Style/ExpandPathArguments:
10
12
  Enabled: false
13
+ Gemspec/RequiredRubyVersion:
14
+ Enabled: false
@@ -1,43 +1,51 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- seri (1.1.10)
4
+ seri (2.0.3)
5
5
  oj (~> 3.7)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- ast (2.4.0)
11
- diff-lcs (1.3)
12
- jaro_winkler (1.5.4)
13
- oj (3.10.6)
14
- parallel (1.19.1)
15
- parser (2.7.1.2)
16
- ast (~> 2.4.0)
10
+ ast (2.4.1)
11
+ coderay (1.1.3)
12
+ diff-lcs (1.4.4)
13
+ method_source (1.0.0)
14
+ oj (3.10.18)
15
+ parallel (1.19.2)
16
+ parser (2.7.2.0)
17
+ ast (~> 2.4.1)
18
+ pry (0.13.1)
19
+ coderay (~> 1.1)
20
+ method_source (~> 1.0)
17
21
  rainbow (3.0.0)
18
- rake (13.0.1)
22
+ rake (13.0.3)
23
+ regexp_parser (1.8.2)
19
24
  rexml (3.2.4)
20
- rspec (3.9.0)
21
- rspec-core (~> 3.9.0)
22
- rspec-expectations (~> 3.9.0)
23
- rspec-mocks (~> 3.9.0)
24
- rspec-core (3.9.1)
25
- rspec-support (~> 3.9.1)
26
- rspec-expectations (3.9.1)
25
+ rspec (3.10.0)
26
+ rspec-core (~> 3.10.0)
27
+ rspec-expectations (~> 3.10.0)
28
+ rspec-mocks (~> 3.10.0)
29
+ rspec-core (3.10.0)
30
+ rspec-support (~> 3.10.0)
31
+ rspec-expectations (3.10.0)
27
32
  diff-lcs (>= 1.2.0, < 2.0)
28
- rspec-support (~> 3.9.0)
29
- rspec-mocks (3.9.1)
33
+ rspec-support (~> 3.10.0)
34
+ rspec-mocks (3.10.0)
30
35
  diff-lcs (>= 1.2.0, < 2.0)
31
- rspec-support (~> 3.9.0)
32
- rspec-support (3.9.2)
33
- rubocop (0.82.0)
34
- jaro_winkler (~> 1.5.1)
36
+ rspec-support (~> 3.10.0)
37
+ rspec-support (3.10.0)
38
+ rubocop (0.93.1)
35
39
  parallel (~> 1.10)
36
- parser (>= 2.7.0.1)
40
+ parser (>= 2.7.1.5)
37
41
  rainbow (>= 2.2.2, < 4.0)
42
+ regexp_parser (>= 1.8)
38
43
  rexml
44
+ rubocop-ast (>= 0.6.0)
39
45
  ruby-progressbar (~> 1.7)
40
46
  unicode-display_width (>= 1.4.0, < 2.0)
47
+ rubocop-ast (0.8.0)
48
+ parser (>= 2.7.1.5)
41
49
  ruby-progressbar (1.10.1)
42
50
  unicode-display_width (1.7.0)
43
51
 
@@ -46,10 +54,11 @@ PLATFORMS
46
54
 
47
55
  DEPENDENCIES
48
56
  bundler (~> 1.16)
57
+ pry (~> 0.13)
49
58
  rake (~> 13.0)
50
59
  rspec (~> 3.0)
51
60
  rubocop (~> 0.82)
52
61
  seri!
53
62
 
54
63
  BUNDLED WITH
55
- 1.17.3
64
+ 2.2.5
data/README.md CHANGED
@@ -33,13 +33,15 @@ Or install it yourself as:
33
33
  A serializer can be used as such:
34
34
 
35
35
  ```ruby
36
+ require 'seri'
37
+
36
38
  # example class:
37
39
  class Car
38
40
  attr_accessor :mileage
39
41
  end
40
42
 
41
43
  # example serializer:
42
- class CarSerializer < Serializer
44
+ class CarSerializer < Seri::Serializer
43
45
  attribute :mileage
44
46
  attribute :brand
45
47
  attribute :mileage_alias, from: :mileage
@@ -94,14 +96,14 @@ Result from `#to_json`:
94
96
  }
95
97
  ```
96
98
 
97
- In turn there's also a `GroupSerializer` available which can take a group of
99
+ In turn there's also a `Seri::GroupSerializer` available which can take a group of
98
100
  cars and turn them into a serialized Array. If we extend the example from
99
101
  earlier we can do:
100
102
 
101
103
  ```ruby
102
104
  # example:
103
105
  cars = [car, car]
104
- group_serializer = GroupSerializer.new(cars, serializer: CarSerializer)
106
+ group_serializer = Seri::GroupSerializer.new(cars, serializer: CarSerializer)
105
107
 
106
108
  group_serializer.to_json
107
109
  ```
@@ -148,11 +150,11 @@ class B
148
150
  end
149
151
  end
150
152
 
151
- class ASerializer < Serializer
153
+ class ASerializer < Seri::Serializer
152
154
  attribute :some_amazing_attribute
153
155
  end
154
156
 
155
- class BSerializer < Serializer
157
+ class BSerializer < Seri::Serializer
156
158
  attribute :lots_of_a, from: :aaa, serializer: ASerializer
157
159
  end
158
160
  ```
@@ -0,0 +1 @@
1
+ require 'serializer'
@@ -3,34 +3,36 @@ require 'serializer/value'
3
3
  require 'serializer/value_fetcher'
4
4
  require 'serializer/group_serializer'
5
5
 
6
- class Serializer
7
- Attribute = Struct.new(:key, :condition, :from, :serializer, :options)
6
+ module Seri
7
+ class Serializer
8
+ Attribute = Struct.new(:key, :condition, :from, :serializer, :options)
8
9
 
9
- def self.attributes
10
- @attributes ||= []
11
- end
10
+ def self.attributes
11
+ @attributes ||= []
12
+ end
12
13
 
13
- def self.attribute(key, condition: nil, from: nil, serializer: nil, **options)
14
- attributes.push(Attribute.new(key, condition, from, serializer, options))
15
- end
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
- attr_accessor :object, :scope
18
+ attr_accessor :object, :scope
18
19
 
19
- def initialize(object, scope: {})
20
- @object = object
21
- @scope = scope
22
- end
20
+ def initialize(object, scope: {})
21
+ @object = object
22
+ @scope = scope
23
+ end
23
24
 
24
- # Loops over all attributes and skips if a condition is defined and falsey
25
- def to_h
26
- self.class.attributes.each_with_object({}) do |attribute, obj|
27
- next if attribute.condition && !public_send(attribute.condition)
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
- obj[attribute.key] = ValueFetcher.fetch(attribute, object, self)
30
+ obj[attribute.key] = ValueFetcher.fetch(attribute, object, self)
31
+ end
30
32
  end
31
- end
32
33
 
33
- def to_json(*)
34
- Oj.dump(to_h, mode: :json)
34
+ def to_json(*)
35
+ Oj.dump(to_h, mode: :json)
36
+ end
35
37
  end
36
38
  end
@@ -1,19 +1,21 @@
1
- class GroupSerializer
2
- def initialize(objects, serializer: nil, scope: {})
3
- raise ArgumentError, 'serializer needs to be specified' if serializer.nil?
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
- @objects = objects
6
- @serializer = serializer
7
- @scope = scope
8
- end
6
+ @objects = objects
7
+ @serializer = serializer
8
+ @scope = scope
9
+ end
9
10
 
10
- def to_json(*)
11
- Oj.dump(to_h, mode: :json)
12
- end
11
+ def to_json(*)
12
+ Oj.dump(to_h, mode: :json)
13
+ end
13
14
 
14
- def to_h
15
- @objects
16
- .map { |object| @serializer.new(object, scope: @scope) }
17
- .map(&:to_h)
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
@@ -1,60 +1,62 @@
1
- class Serializer
2
- class Value
3
- def initialize(attribute, scope = nil)
4
- @attribute = attribute
5
- @scope = scope
6
- end
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
- def extraction_key
9
- @attribute.from || @attribute.key
10
- end
9
+ def extraction_key
10
+ @attribute.from || @attribute.key
11
+ end
11
12
 
12
- def precondition?
13
- raise NotImplementedError, 'needs a method called precondition?'
14
- end
13
+ def precondition?
14
+ raise NotImplementedError, 'needs a method called precondition?'
15
+ end
15
16
 
16
- def value
17
- raise NotImplementedError, 'needs a method called value'
17
+ def value
18
+ raise NotImplementedError, 'needs a method called value'
19
+ end
18
20
  end
19
- end
20
21
 
21
- class StaticValue < Value
22
- def precondition?
23
- @attribute.options.key?(:static_value)
24
- end
22
+ class StaticValue < Value
23
+ def precondition?
24
+ @attribute.options.key?(:static_value)
25
+ end
25
26
 
26
- def value
27
- @attribute.options.fetch(:static_value)
27
+ def value
28
+ @attribute.options.fetch(:static_value)
29
+ end
28
30
  end
29
- end
30
31
 
31
- class SerializedValue < Value
32
- def precondition?
33
- @scope.respond_to?(extraction_key)
34
- end
32
+ class SerializedValue < Value
33
+ def precondition?
34
+ @scope.respond_to?(extraction_key)
35
+ end
35
36
 
36
- def value
37
- @scope.public_send(extraction_key)
37
+ def value
38
+ @scope.public_send(extraction_key)
39
+ end
38
40
  end
39
- end
40
41
 
41
- class HashValue < Value
42
- def precondition?
43
- @scope.is_a?(Hash)
44
- end
42
+ class HashValue < Value
43
+ def precondition?
44
+ @scope.is_a?(Hash)
45
+ end
45
46
 
46
- def value
47
- @scope[extraction_key]
47
+ def value
48
+ @scope[extraction_key]
49
+ end
48
50
  end
49
- end
50
51
 
51
- class ObjectValue < Value
52
- def precondition?
53
- @scope.respond_to?(extraction_key)
54
- end
52
+ class ObjectValue < Value
53
+ def precondition?
54
+ @scope.respond_to?(extraction_key)
55
+ end
55
56
 
56
- def value
57
- @scope.public_send(extraction_key)
57
+ def value
58
+ @scope.public_send(extraction_key)
59
+ end
58
60
  end
59
61
  end
60
62
  end
@@ -1,61 +1,53 @@
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
1
+ module Seri
2
+ class Serializer
3
+ class ValueFetcher
4
+ class SerializerError < StandardError; end
14
5
 
15
- private_class_method :new
6
+ def self.fetch(attribute, object, serializer)
7
+ new(attribute, object, serializer).fetch
8
+ end
16
9
 
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
10
+ private_class_method :new
26
11
 
27
- def fetch
28
- serializer = @attribute.serializer
12
+ def initialize(attribute, object, serializer)
13
+ @attribute = attribute
14
+ @values = [
15
+ StaticValue.new(attribute),
16
+ SerializedValue.new(attribute, serializer),
17
+ HashValue.new(attribute, object),
18
+ ObjectValue.new(attribute, object)
19
+ ]
20
+ end
29
21
 
30
- return value unless serializer
22
+ def fetch
23
+ serializer = @attribute.serializer
31
24
 
32
- if array?
33
- value.map { |item| serializer.new(item).to_h }
34
- else
35
- serializer.new(value).to_h
36
- end
37
- end
25
+ return value unless serializer
38
26
 
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}'"
27
+ if value.is_a?(Enumerable) && !value.is_a?(Hash)
28
+ value.map { |item| serializer.new(item).to_h }
29
+ else
30
+ serializer.new(value).to_h
51
31
  end
52
-
53
- extracted_value.value
54
32
  end
55
- end
56
33
 
57
- def array?
58
- ARRAYS.any? { |match| value.class.to_s.end_with?(match) }
34
+ # Fetches a value from an attribute by checking if there's a ..
35
+ # .. static value set, or a ..
36
+ # .. method defined in the serializer, or a ..
37
+ # .. method/attribute defined in the object or ..
38
+ # .. it raises an error
39
+ def value
40
+ @value ||= begin
41
+ extracted_value = @values.detect(&:precondition?)
42
+
43
+ if extracted_value.nil?
44
+ raise SerializerError,
45
+ "unknown attribute '#{@values[0].extraction_key}'"
46
+ end
47
+
48
+ extracted_value.value
49
+ end
50
+ end
59
51
  end
60
52
  end
61
53
  end
@@ -1,3 +1,5 @@
1
- class Serializer
2
- VERSION = '1.1.10'.freeze
1
+ module Seri
2
+ class Serializer
3
+ VERSION = '2.0.3'.freeze
4
+ end
3
5
  end
@@ -4,7 +4,7 @@ require 'serializer/version'
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'seri'
7
- spec.version = Serializer::VERSION
7
+ spec.version = Seri::Serializer::VERSION
8
8
  spec.authors = ['grdw']
9
9
  spec.email = ['gerard@wetransfer.com']
10
10
 
@@ -27,6 +27,7 @@ Gem::Specification.new do |spec|
27
27
  spec.add_dependency 'oj', '~> 3.7'
28
28
 
29
29
  spec.add_development_dependency 'bundler', '~> 1.16'
30
+ spec.add_development_dependency 'pry', '~> 0.13'
30
31
  spec.add_development_dependency 'rake', '~> 13.0'
31
32
  spec.add_development_dependency 'rspec', '~> 3.0'
32
33
  spec.add_development_dependency 'rubocop', '~> 0.82'
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.10
4
+ version: 2.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - grdw
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-06 00:00:00.000000000 Z
11
+ date: 2021-01-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oj
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.13'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.13'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rake
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -98,6 +112,7 @@ files:
98
112
  - LICENSE.txt
99
113
  - README.md
100
114
  - Rakefile
115
+ - lib/seri.rb
101
116
  - lib/serializer.rb
102
117
  - lib/serializer/group_serializer.rb
103
118
  - lib/serializer/value.rb
@@ -108,7 +123,7 @@ homepage: https://github.com/grdw/seri
108
123
  licenses:
109
124
  - MIT
110
125
  metadata: {}
111
- post_install_message:
126
+ post_install_message:
112
127
  rdoc_options: []
113
128
  require_paths:
114
129
  - lib
@@ -123,8 +138,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
138
  - !ruby/object:Gem::Version
124
139
  version: '0'
125
140
  requirements: []
126
- rubygems_version: 3.1.2
127
- signing_key:
141
+ rubygems_version: 3.2.3
142
+ signing_key:
128
143
  specification_version: 4
129
144
  summary: A basic serializer
130
145
  test_files: []