highlighter 0.1.1 → 0.2.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: b858676f9797dd49895ba65127dad51d3e4884592a7c542f22b9e120b8c05618
4
+ data.tar.gz: '049a27f4096feba8cf4967fea14d249cee3d9093416206e99389813966b01420'
5
5
  SHA512:
6
- metadata.gz: 9285161b7647b13e7a02b4002b4fada32c4f25e6c1a7918f3914fcdfcfc9ef572459483589f8257c80b491f35fc649a606f895ec3c33c6dd29d06d470691f83e
7
- data.tar.gz: 156b4030920abcf5e7dcf49f717820bb1ee1fcf832bd5faa1bd8fc6bae7306590736b0ec9537425591fb8804b7655a582e244ba7642f0c22d2180488d4409523
6
+ metadata.gz: 7444b6856724a83cb30a8533164967cf949bd9e279091d593123ef9c694a6f0b581bfe01ccceba6c7252725bd64a7db185372e1a0ab495183ab6c4e216fdb225
7
+ data.tar.gz: b04bd46c3ba05d7d8deca7a17b2807ee1c7e620492d5f1f96442cd47aefbf4e8dad78608c1a298f24ed327bff2bead87ad7fabc8391db592dee6e59541784d8e
data/CHANGELOG.md CHANGED
@@ -3,3 +3,7 @@
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
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.2.0.pre.alpha)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -85,7 +85,19 @@ 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
+ ```
89
101
  ## Development
90
102
 
91
103
  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.
@@ -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.name)
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,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
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.attributes,
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)
28
- end
29
-
30
- def serialize_array(array, attr)
31
- array.map do |value|
32
- apply_serializer(value, attr)
33
- end
34
- end
35
-
36
- def apply_serializer(value, attr)
37
- return value if attr.serializer.nil?
38
-
39
- attr.serializer.new(value).to_h
24
+ # Add methods to expose and set up attributes
25
+ module ClassMethods
26
+ def attribute(name, serializer: nil)
27
+ instance_variable_set(:@attributes, []) unless instance_variable_defined?(:@attributes)
28
+ instance_variable_get(:@attributes) << Attribute.new(name:, serializer:)
40
29
  end
41
30
 
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
48
-
49
- def attributes
50
- instance_variable_get(:@attributes)
51
- end
31
+ def attributes
32
+ instance_variable_get(:@attributes)
52
33
  end
53
34
  end
54
35
  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.2.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.2.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-02 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: