seri 1.1.9 → 2.0.2

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: 3f3d7fa0dae40ce8b0e43a174185c435c048fed3ae38242a735592ce1d5bc1bc
4
- data.tar.gz: b70a1a82b0b75b6396ba5cf78db892b23cd64d60c75a9d7948a7b667af9e9b51
3
+ metadata.gz: 2db241b65f4763c608dd98ca458ce4563fba0657207b983db687b70b3ed17b23
4
+ data.tar.gz: e478ae294f897daf1518ec9cb45f1ef00dbc98f4c8fc60b012a94352d057c078
5
5
  SHA512:
6
- metadata.gz: 323161f0982a5f25b138b63e929fde6452378efd35023c69761476b653fe6d23d6ddcb5571ff3ee82fe658710f5c9e60bb493dd4c19fe7320632362c49d24558
7
- data.tar.gz: d20c18753f7d19bce1fbe02c0466c4e5ff9d6913568b29f4dc2490a8a156b67c7dd6980e21986192f548f48e84e78f154ba4c5fe299255ac7d90e534392c05a0
6
+ metadata.gz: d1248e83e33d45d803dffe284c4a89baa7d3755c76d5083b58140dfa635db2735a1728f39f06ce7bc0945240cb33f0396a6f52e9f48e2b48fd486e52036626b0
7
+ data.tar.gz: 240e560a38d9c51dfa904a908417ac59393aca4ba7f3db6f5509dfcd6070a37c0d888f5dd6c58e2b2ed08bd370c7b2f949c100315344ad60bdf86c6afd1e9b78
@@ -4,7 +4,7 @@ jobs:
4
4
  build:
5
5
  docker:
6
6
  # specify the version you desire here
7
- - image: circleci/ruby:2.5.3-node-browsers
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:
@@ -1,8 +1,14 @@
1
+ AllCops:
2
+ NewCops: disable
1
3
  Metrics/BlockLength:
2
4
  Exclude:
3
5
  - 'spec/**/*.rb'
4
6
  Style/Documentation:
5
7
  Enabled: false
8
+ Style/FrozenStringLiteralComment:
9
+ Enabled: false
6
10
  # This is causing breaking changes:
7
11
  Style/ExpandPathArguments:
8
12
  Enabled: false
13
+ Gemspec/RequiredRubyVersion:
14
+ Enabled: false
@@ -1,54 +1,63 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- seri (1.1.9)
4
+ seri (2.0.1)
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.5)
14
- parallel (1.19.1)
15
- parser (2.7.0.5)
16
- ast (~> 2.4.0)
17
- psych (3.1.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)
18
21
  rainbow (3.0.0)
19
- rake (13.0.1)
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.4)
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.67.2)
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.5, != 2.5.1.1)
37
- psych (>= 3.1.0)
40
+ parser (>= 2.7.1.5)
38
41
  rainbow (>= 2.2.2, < 4.0)
42
+ regexp_parser (>= 1.8)
43
+ rexml
44
+ rubocop-ast (>= 0.6.0)
39
45
  ruby-progressbar (~> 1.7)
40
- unicode-display_width (>= 1.4.0, < 1.6)
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
- unicode-display_width (1.5.0)
50
+ unicode-display_width (1.7.0)
43
51
 
44
52
  PLATFORMS
45
53
  ruby
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
- rubocop (~> 0.67.2)
60
+ rubocop (~> 0.82)
52
61
  seri!
53
62
 
54
63
  BUNDLED WITH
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,58 +1,62 @@
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)
1
+ module Seri
2
+ class Serializer
3
+ class Value
4
+ def initialize(attribute, scope = nil)
5
+ @attribute = attribute
6
+ @scope = scope
7
+ end
8
+
9
+ def extraction_key
10
+ @attribute.from || @attribute.key
11
+ end
12
+
13
+ def precondition?
14
+ raise NotImplementedError, 'needs a method called precondition?'
15
+ end
16
+
17
+ def value
18
+ raise NotImplementedError, 'needs a method called value'
19
+ end
20
+ end
21
+
22
+ class StaticValue < Value
23
+ def precondition?
24
+ @attribute.options.key?(:static_value)
25
+ end
26
+
27
+ def value
28
+ @attribute.options.fetch(:static_value)
29
+ end
30
+ end
31
+
32
+ class SerializedValue < Value
33
+ def precondition?
34
+ @scope.respond_to?(extraction_key)
35
+ end
36
+
37
+ def value
38
+ @scope.public_send(extraction_key)
39
+ end
40
+ end
41
+
42
+ class HashValue < Value
43
+ def precondition?
44
+ @scope.is_a?(Hash)
45
+ end
46
+
47
+ def value
48
+ @scope[extraction_key]
49
+ end
50
+ end
51
+
52
+ class ObjectValue < Value
53
+ def precondition?
54
+ @scope.respond_to?(extraction_key)
55
+ end
56
+
57
+ def value
58
+ @scope.public_send(extraction_key)
59
+ end
60
+ end
57
61
  end
58
62
  end
@@ -1,59 +1,53 @@
1
- class ValueFetcher
2
- class SerializerError < StandardError; end
1
+ module Seri
2
+ class Serializer
3
+ class ValueFetcher
4
+ class SerializerError < StandardError; end
3
5
 
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
6
+ def self.fetch(attribute, object, serializer)
7
+ new(attribute, object, serializer).fetch
8
+ end
13
9
 
14
- private_class_method :new
10
+ private_class_method :new
15
11
 
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
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
25
21
 
26
- def fetch
27
- serializer = @attribute.serializer
22
+ def fetch
23
+ serializer = @attribute.serializer
28
24
 
29
- return value unless serializer
25
+ return value unless serializer
30
26
 
31
- if 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}'"
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
31
+ end
50
32
  end
51
33
 
52
- extracted_value.value
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
53
51
  end
54
52
  end
55
-
56
- def array?
57
- ARRAYS.any? { |match| value.class.to_s.end_with?(match) }
58
- end
59
53
  end
@@ -1,3 +1,5 @@
1
- class Serializer
2
- VERSION = '1.1.9'.freeze
1
+ module Seri
2
+ class Serializer
3
+ VERSION = '2.0.2'.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,7 +27,8 @@ 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
- spec.add_development_dependency 'rubocop', '~> 0.67.2'
33
+ spec.add_development_dependency 'rubocop', '~> 0.82'
33
34
  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.9
4
+ version: 2.0.2
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-03-30 00:00:00.000000000 Z
11
+ date: 2021-01-04 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
@@ -72,14 +86,14 @@ dependencies:
72
86
  requirements:
73
87
  - - "~>"
74
88
  - !ruby/object:Gem::Version
75
- version: 0.67.2
89
+ version: '0.82'
76
90
  type: :development
77
91
  prerelease: false
78
92
  version_requirements: !ruby/object:Gem::Requirement
79
93
  requirements:
80
94
  - - "~>"
81
95
  - !ruby/object:Gem::Version
82
- version: 0.67.2
96
+ version: '0.82'
83
97
  description: A basic serializer
84
98
  email:
85
99
  - gerard@wetransfer.com
@@ -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
@@ -124,7 +139,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
124
139
  version: '0'
125
140
  requirements: []
126
141
  rubygems_version: 3.1.2
127
- signing_key:
142
+ signing_key:
128
143
  specification_version: 4
129
144
  summary: A basic serializer
130
145
  test_files: []