seri 1.1.11 → 2.0.4

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: 93d4a3c7d7f2166ee7b8f3a32a398b4047f46d19a73ce59cca497cc74843cfe4
4
- data.tar.gz: 4e1e7a46dc70f2f1a0d9ffb93b11b625da277beee9593348736f91fd912a31f6
3
+ metadata.gz: 7a3a1187f09d0137341747e027fab35d148056c102fc4344e1019e29128d5e0e
4
+ data.tar.gz: af33fc079fff5f053fa28b5b914a82c9989e201002cf1e626d45d941f8277efe
5
5
  SHA512:
6
- metadata.gz: 0ce7148279857a7c35a535e250124dbb2b2a3957b246ab0c737d631ec9129451977f53e9abdb65d403acd72650587648d497dad330c80f710c31033b5cbff563
7
- data.tar.gz: d433c26c41157799e77a6f8d786b3d7418cfe6147fe70187dd34dc9c44eea617b6ce9918df0c375e77e2fa1203607d968e1c7a3a098cd9b8c1cb365a53ae71ec
6
+ metadata.gz: f6b36c95ee331cdc57125bb115b85780ed4fd7bf9181feecf6c4f9e40858967e68ca7d5ebb92de05cea1bf927a28bf295f46417d5bf905c32e3ad767ac839e05
7
+ data.tar.gz: 32b3c7ccc01c298d2f131922ee93dafbb213be35c553bcc79211bebfc374b3a1cbfdb4586cfd7bd311a411c4852aa335ef8a654de917432f4c2c4a60a4272c6b
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.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:
data/.rubocop.yml CHANGED
@@ -1,3 +1,5 @@
1
+ AllCops:
2
+ NewCops: disable
1
3
  Metrics/BlockLength:
2
4
  Exclude:
3
5
  - 'spec/**/*.rb'
@@ -8,31 +10,5 @@ Style/FrozenStringLiteralComment:
8
10
  # This is causing breaking changes:
9
11
  Style/ExpandPathArguments:
10
12
  Enabled: false
11
- Layout/EmptyLinesAroundAttributeAccessor:
12
- Enabled: false
13
- Layout/SpaceAroundMethodCallOperator:
14
- Enabled: false
15
- Lint/DeprecatedOpenSSLConstant:
16
- Enabled: false
17
- Lint/MixedRegexpCaptureTypes:
18
- Enabled: false
19
- Lint/RaiseException:
20
- Enabled: false
21
- Lint/StructNewOverride:
22
- Enabled: false
23
- Style/ExponentialNotation:
24
- Enabled: false
25
- Style/HashEachMethods:
26
- Enabled: false
27
- Style/HashTransformKeys:
28
- Enabled: false
29
- Style/HashTransformValues:
30
- Enabled: false
31
- Style/RedundantFetchBlock:
32
- Enabled: false
33
- Style/RedundantRegexpCharacterClass:
34
- Enabled: false
35
- Style/RedundantRegexpEscape:
36
- Enabled: false
37
- Style/SlicingWithRange:
13
+ Gemspec/RequiredRubyVersion:
38
14
  Enabled: false
data/Gemfile.lock CHANGED
@@ -1,46 +1,51 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- seri (1.1.11)
4
+ seri (2.0.4)
5
5
  oj (~> 3.7)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
10
  ast (2.4.1)
11
- diff-lcs (1.3)
12
- oj (3.10.6)
11
+ coderay (1.1.3)
12
+ diff-lcs (1.4.4)
13
+ method_source (1.0.0)
14
+ oj (3.11.7)
13
15
  parallel (1.19.2)
14
- parser (2.7.1.4)
16
+ parser (2.7.2.0)
15
17
  ast (~> 2.4.1)
18
+ pry (0.14.1)
19
+ coderay (~> 1.1)
20
+ method_source (~> 1.0)
16
21
  rainbow (3.0.0)
17
- rake (13.0.1)
18
- regexp_parser (1.7.1)
19
- 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)
22
+ rake (13.0.3)
23
+ regexp_parser (1.8.2)
24
+ rexml (3.2.5)
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.86.0)
36
+ rspec-support (~> 3.10.0)
37
+ rspec-support (3.10.0)
38
+ rubocop (0.93.1)
34
39
  parallel (~> 1.10)
35
- parser (>= 2.7.0.1)
40
+ parser (>= 2.7.1.5)
36
41
  rainbow (>= 2.2.2, < 4.0)
37
- regexp_parser (>= 1.7)
42
+ regexp_parser (>= 1.8)
38
43
  rexml
39
- rubocop-ast (>= 0.0.3, < 1.0)
44
+ rubocop-ast (>= 0.6.0)
40
45
  ruby-progressbar (~> 1.7)
41
46
  unicode-display_width (>= 1.4.0, < 2.0)
42
- rubocop-ast (0.0.3)
43
- parser (>= 2.7.0.1)
47
+ rubocop-ast (0.8.0)
48
+ parser (>= 2.7.1.5)
44
49
  ruby-progressbar (1.10.1)
45
50
  unicode-display_width (1.7.0)
46
51
 
@@ -48,11 +53,12 @@ PLATFORMS
48
53
  ruby
49
54
 
50
55
  DEPENDENCIES
51
- bundler (~> 1.16)
56
+ bundler (>= 2.2.10)
57
+ pry (~> 0.13)
52
58
  rake (~> 13.0)
53
59
  rspec (~> 3.0)
54
60
  rubocop (~> 0.82)
55
61
  seri!
56
62
 
57
63
  BUNDLED WITH
58
- 1.17.3
64
+ 2.2.21
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
  ```
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
- 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.11'.freeze
1
+ module Seri
2
+ class Serializer
3
+ VERSION = '2.0.4'.freeze
4
+ end
3
5
  end
data/seri.gemspec CHANGED
@@ -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
 
@@ -26,7 +26,8 @@ Gem::Specification.new do |spec|
26
26
 
27
27
  spec.add_dependency 'oj', '~> 3.7'
28
28
 
29
- spec.add_development_dependency 'bundler', '~> 1.16'
29
+ spec.add_development_dependency 'bundler', '>= 2.2.10'
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.11
4
+ version: 2.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - grdw
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-30 00:00:00.000000000 Z
11
+ date: 2021-06-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oj
@@ -26,18 +26,32 @@ dependencies:
26
26
  version: '3.7'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.2.10
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 2.2.10
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
45
  - - "~>"
32
46
  - !ruby/object:Gem::Version
33
- version: '1.16'
47
+ version: '0.13'
34
48
  type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
52
  - - "~>"
39
53
  - !ruby/object:Gem::Version
40
- version: '1.16'
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
@@ -123,7 +138,7 @@ 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
141
+ rubygems_version: 3.2.3
127
142
  signing_key:
128
143
  specification_version: 4
129
144
  summary: A basic serializer