seri 1.1.11 → 2.0.0

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: a280328ee692f5395dd9ff0fe6bde3c684f6131131b69ed059a9ff58403a79ec
4
+ data.tar.gz: 4b817c4d1aa442835a5227ab0f88758644378322922884317e087c79f594f88f
5
5
  SHA512:
6
- metadata.gz: 0ce7148279857a7c35a535e250124dbb2b2a3957b246ab0c737d631ec9129451977f53e9abdb65d403acd72650587648d497dad330c80f710c31033b5cbff563
7
- data.tar.gz: d433c26c41157799e77a6f8d786b3d7418cfe6147fe70187dd34dc9c44eea617b6ce9918df0c375e77e2fa1203607d968e1c7a3a098cd9b8c1cb365a53ae71ec
6
+ metadata.gz: 05fae53fffb74d5fbdf639a855703dba418fb2ce3d82ec81fde010cce67055c0be015f50953c54d7cbe260eb0f148216513ecca21e6c5d3de1b11cf03c954d77
7
+ data.tar.gz: 2828f68b71f05e813cd213a96e12c68078a5bdb373a74e8ea9021286d4a0745730ca45ee96573573454f887f191e65f7cc555e146c164fea5dfb2c939e33e2ef
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- seri (1.1.11)
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.6)
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.86.0)
33
+ rubocop (0.88.0)
34
34
  parallel (~> 1.10)
35
- parser (>= 2.7.0.1)
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.3, < 1.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.3)
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)
@@ -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,63 @@
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
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
- private_class_method :new
16
+ private_class_method :new
16
17
 
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
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
- def fetch
28
- serializer = @attribute.serializer
28
+ def fetch
29
+ serializer = @attribute.serializer
29
30
 
30
- return value unless serializer
31
+ return value unless serializer
31
32
 
32
- if array?
33
- value.map { |item| serializer.new(item).to_h }
34
- else
35
- serializer.new(value).to_h
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
- # 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}'"
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
- def array?
58
- ARRAYS.any? { |match| value.class.to_s.end_with?(match) }
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
@@ -1,3 +1,5 @@
1
- class Serializer
2
- VERSION = '1.1.11'.freeze
1
+ module Seri
2
+ class Serializer
3
+ VERSION = '2.0.0'.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
 
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.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-06-30 00:00:00.000000000 Z
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