highlighter 0.1.1 → 0.4.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: 6c3c586f7e61fcde95b897c059b304e52a577b36ce47ee90af831c478a3fe9a4
4
- data.tar.gz: d881765b127d531a04892a7018a73c7fd0e7f86078796d4543a705eeead9c306
3
+ metadata.gz: fc57165cc3381a2749507abc7746f95d2ede9786820b7ff3e8d2f9624981fd66
4
+ data.tar.gz: 593a5b19b6d616a407dd7b214186c43d3d478514f2607b318957485e717dcb21
5
5
  SHA512:
6
- metadata.gz: 9285161b7647b13e7a02b4002b4fada32c4f25e6c1a7918f3914fcdfcfc9ef572459483589f8257c80b491f35fc649a606f895ec3c33c6dd29d06d470691f83e
7
- data.tar.gz: 156b4030920abcf5e7dcf49f717820bb1ee1fcf832bd5faa1bd8fc6bae7306590736b0ec9537425591fb8804b7655a582e244ba7642f0c22d2180488d4409523
6
+ metadata.gz: 70557187e4553b5049f2bf15da03c5f40710b6cc5daf133c15190d51d01193272f147d91e14b7c43a632c832abcfef2325cd2ae5b20608a3f95080c2525dc45d
7
+ data.tar.gz: 9601811f3dc47ef0396432d4fa949d1098a95dee414e7d3e921f59ee8662ce3a884d1a453e0434698c8763b2127d4b3b77630f3cf6e34ace048d944528eafbd3
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.1)
4
+ highlighter (0.4.0.pre.alpha)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -85,6 +85,48 @@ 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
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
+ ```
116
+ Passing a list of fields:
117
+ ```ruby
118
+ class UserSerializer
119
+ include Highlighter::Serializer
120
+
121
+ attributes :id, :name
122
+ end
123
+ ```
124
+ ```ruby
125
+ {
126
+ id: 1,
127
+ name: "Kelly"
128
+ }
129
+ ```
88
130
 
89
131
  ## Development
90
132
 
@@ -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,48 +7,36 @@ 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
16
  return if @object.nil?
16
17
 
17
- self.class.attributes.each_with_object({}) do |attr, hash|
18
- hash[attr.name] = serialize_attribute(attr)
19
- hash
20
- end
18
+ Commands::ObjectSerializer.call(object: @object,
19
+ attributes: self.class.attribute_list,
20
+ options: @options)
21
21
  end
22
+ end
22
23
 
23
- def serialize_attribute(attr)
24
- value = @object.send(attr.name)
25
- return serialize_array(value, attr) if value.instance_of? Array
26
-
27
- apply_serializer(value, attr)
24
+ # Add methods to expose and set up attributes
25
+ module ClassMethods
26
+ def attribute(field, serializer: nil, rename_to: nil)
27
+ attribute_list << Attribute.new(field:, serializer:, rename_to:)
28
28
  end
29
29
 
30
- def serialize_array(array, attr)
31
- array.map do |value|
32
- apply_serializer(value, attr)
30
+ def attributes(*fields)
31
+ fields.each do |field|
32
+ attribute_list << Attribute.new(field:)
33
33
  end
34
34
  end
35
35
 
36
- def apply_serializer(value, attr)
37
- return value if attr.serializer.nil?
38
-
39
- attr.serializer.new(value).to_h
40
- end
41
-
42
- # Add methods to expose and set up attributes
43
- module ClassMethods
44
- def attribute(name, serializer: nil)
45
- instance_variable_set(:@attributes, []) unless instance_variable_defined?(:@attributes)
46
- instance_variable_get(:@attributes) << Attribute.new(name:, serializer:)
47
- end
36
+ def attribute_list
37
+ instance_variable_set(:@attribute_list, []) unless instance_variable_defined?(:@attribute_list)
48
38
 
49
- def attributes
50
- instance_variable_get(:@attributes)
51
- end
39
+ instance_variable_get(:@attribute_list)
52
40
  end
53
41
  end
54
42
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Highlighter
4
- VERSION = "0.1.1"
4
+ VERSION = "0.4.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.1
4
+ version: 0.4.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-04 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: