highlighter 0.1.0 → 0.3.0.pre.alpha

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1bb5ff98b735f8db19cce93c9c60a1770583bfe79f4f2eafde3edb416ac1720c
4
- data.tar.gz: abe29e519393a81c5c3dd5f4aa75c065584695c910badc26207a4d6295f30e1f
3
+ metadata.gz: 0da81f118da4f0f966398170d013bfbde42783d862a416b668d28f7680036ea1
4
+ data.tar.gz: b4419445f9c3427a7c65b4f12d138f17cc86f74d65587ee24ce45f3b8c0f09ab
5
5
  SHA512:
6
- metadata.gz: a0fa614bdeac9734737e552c2a3ca2081dfe1359b03786d818d315049433164e2e1488a96071c32d190bd0d57ab0e8a10b6529b4ebca1f1cdca13eb5416fafb1
7
- data.tar.gz: 1a698647c970a53d078f66093452593b84b5bbac22dde8f5d5a9e9180da73ed8cf2ad7f6048dac0d7d6f562398e56b6f8e146901adfed3223c89140ed9801308
6
+ metadata.gz: e7f0ef49bebdc089c5d0a11da23aaad9fcddb68dc5f21297b0505451ea0d4ea3e10345bf0884964d653567aa9c37ae61f7263a00f42c291d94270d14a6ffbb43
7
+ data.tar.gz: 0ff8249c987ba58b2a6a585d0caa3e8655c17d0af07ce5599ad29126ccbf890f5f7719f6401946b5c87966ada2cb3757447d0788230e8a67c7fa1447b49f8d78
data/.rubocop.yml CHANGED
@@ -11,3 +11,6 @@ Style/StringLiteralsInInterpolation:
11
11
 
12
12
  Layout/LineLength:
13
13
  Max: 120
14
+
15
+ Metrics/BlockLength:
16
+ Max: 600
data/CHANGELOG.md CHANGED
@@ -3,3 +3,11 @@
3
3
  ## [0.1.0] - 2022-08-13
4
4
 
5
5
  - Initial release
6
+
7
+ ## [0.2.0-alpha] - 2022-09-02
8
+
9
+ - Feature: add support to assign serializers on the fly
10
+
11
+ ## [0.3.0-alpha] - 2022-09-03
12
+
13
+ - Feature: rename attribute
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- highlighter (0.1.0)
4
+ highlighter (0.3.0.pre.alpha)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -85,7 +85,34 @@ And it will return:
85
85
  ]
86
86
  }
87
87
  ```
88
+ It also supports lambdas to set serializer on the fly:
89
+ ```ruby
90
+ class UserSerializer
91
+ include Highlighter::Serializer
88
92
 
93
+ attribute :id
94
+ attribute :name
95
+ attribute :cars, serializer: ->(options) { options[:custom_serializer] }
96
+ end
97
+ ```
98
+ ```ruby
99
+ UserSerializer.new(user, custom_serializer: CarSerializer).to_h
100
+ ```
101
+ Renaming attribute:
102
+ ```ruby
103
+ class UserSerializer
104
+ include Highlighter::Serializer
105
+
106
+ attribute :id
107
+ attribute :name, rename_to: :full_name
108
+ end
109
+ ```
110
+ ```ruby
111
+ {
112
+ id: 1,
113
+ full_name: "Kelly"
114
+ }
115
+ ```
89
116
  ## Development
90
117
 
91
118
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -3,11 +3,16 @@
3
3
  module Highlighter
4
4
  # Holds info on attribute
5
5
  class Attribute
6
- attr_reader :name, :serializer
6
+ attr_reader :field, :serializer, :rename_to
7
7
 
8
- def initialize(name:, serializer: nil)
9
- @name = name
8
+ def initialize(field:, serializer: nil, rename_to: nil)
9
+ @field = field
10
10
  @serializer = serializer
11
+ @rename_to = rename_to
12
+ end
13
+
14
+ def name
15
+ rename_to.nil? ? field : rename_to
11
16
  end
12
17
  end
13
18
  end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Highlighter
4
+ module Commands
5
+ # Command to serialize attribute
6
+ class AttributeSerializer
7
+ def self.call(object:, attribute:, options: {})
8
+ new(object:, attribute:, options:).call
9
+ end
10
+
11
+ def initialize(object:, attribute:, options:)
12
+ @object = object
13
+ @attribute = attribute
14
+ @options = options
15
+ end
16
+
17
+ def call
18
+ value = object.send(attribute.field)
19
+ return serialize_array(value) if value.instance_of? Array
20
+
21
+ apply_serializer(value, attribute)
22
+ end
23
+
24
+ private
25
+
26
+ attr_reader :object, :attribute, :options
27
+
28
+ def serialize_array(array)
29
+ array.map do |value|
30
+ apply_serializer(value, attribute)
31
+ end
32
+ end
33
+
34
+ def apply_serializer(value, attr)
35
+ serializer = attr.serializer
36
+ return value if serializer.nil?
37
+
38
+ serializer = serializer.call(options) if serializer.is_a? Proc
39
+
40
+ serializer.new(value).to_h
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Highlighter
4
+ module Commands
5
+ # Command to serialize object
6
+ class ObjectSerializer
7
+ def self.call(object:, attributes:, options:)
8
+ new(object:, attributes:, options:).call
9
+ end
10
+
11
+ def initialize(object:, attributes:, options:)
12
+ @object = object
13
+ @attributes = attributes
14
+ @options = options
15
+ end
16
+
17
+ def call
18
+ attributes.each_with_object({}) do |attr, hash|
19
+ hash[attr.name] = serialize_attribute(attr)
20
+ hash
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ attr_reader :object, :attributes, :options
27
+
28
+ def serialize_attribute(attribute)
29
+ AttributeSerializer.call(object:, attribute:, options:)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -7,46 +7,29 @@ module Highlighter
7
7
  base.extend(ClassMethods)
8
8
  end
9
9
 
10
- def initialize(object)
10
+ def initialize(object, options = {})
11
11
  @object = object
12
+ @options = options
12
13
  end
13
14
 
14
15
  def to_h
15
- self.class.attributes.each_with_object({}) do |attr, hash|
16
- hash[attr.name] = serialize_attribute(attr)
17
- hash
18
- end
19
- end
20
-
21
- def serialize_attribute(attr)
22
- value = @object.send(attr.name)
23
- return serialize_array(value, attr) if value.instance_of? Array
24
-
25
- apply_serializer(value, attr)
26
- end
16
+ return if @object.nil?
27
17
 
28
- def serialize_array(array, attr)
29
- array.map do |value|
30
- apply_serializer(value, attr)
31
- end
18
+ Commands::ObjectSerializer.call(object: @object,
19
+ attributes: self.class.attributes,
20
+ options: @options)
32
21
  end
22
+ end
33
23
 
34
- def apply_serializer(value, attr)
35
- return value if attr.serializer.nil?
36
-
37
- attr.serializer.new(value).to_h
24
+ # Add methods to expose and set up attributes
25
+ module ClassMethods
26
+ def attribute(field, serializer: nil, rename_to: nil)
27
+ instance_variable_set(:@attributes, []) unless instance_variable_defined?(:@attributes)
28
+ instance_variable_get(:@attributes) << Attribute.new(field:, serializer:, rename_to:)
38
29
  end
39
30
 
40
- # Add methods to expose and set up attributes
41
- module ClassMethods
42
- def attribute(name, serializer: nil)
43
- instance_variable_set(:@attributes, []) unless instance_variable_defined?(:@attributes)
44
- instance_variable_get(:@attributes) << Attribute.new(name:, serializer:)
45
- end
46
-
47
- def attributes
48
- instance_variable_get(:@attributes)
49
- end
31
+ def attributes
32
+ instance_variable_get(:@attributes)
50
33
  end
51
34
  end
52
35
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Highlighter
4
- VERSION = "0.1.0"
4
+ VERSION = "0.3.0-alpha"
5
5
  end
data/lib/highlighter.rb CHANGED
@@ -3,6 +3,8 @@
3
3
  require_relative "highlighter/version"
4
4
  require_relative "highlighter/serializer"
5
5
  require_relative "highlighter/attribute"
6
+ require_relative "highlighter/commands/object_serializer"
7
+ require_relative "highlighter/commands/attribute_serializer"
6
8
 
7
9
  module Highlighter
8
10
  class Error < StandardError; end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: highlighter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0.pre.alpha
5
5
  platform: ruby
6
6
  authors:
7
7
  - Caio Rodriguez
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-08-20 00:00:00.000000000 Z
11
+ date: 2022-09-03 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Serializer ruby objects in simple and straightforward way
14
14
  email:
@@ -28,6 +28,8 @@ files:
28
28
  - highlighter.gemspec
29
29
  - lib/highlighter.rb
30
30
  - lib/highlighter/attribute.rb
31
+ - lib/highlighter/commands/attribute_serializer.rb
32
+ - lib/highlighter/commands/object_serializer.rb
31
33
  - lib/highlighter/serializer.rb
32
34
  - lib/highlighter/version.rb
33
35
  - sig/highlighter.rbs
@@ -49,9 +51,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
49
51
  version: 3.1.2
50
52
  required_rubygems_version: !ruby/object:Gem::Requirement
51
53
  requirements:
52
- - - ">="
54
+ - - ">"
53
55
  - !ruby/object:Gem::Version
54
- version: '0'
56
+ version: 1.3.1
55
57
  requirements: []
56
58
  rubygems_version: 3.3.7
57
59
  signing_key: