highlighter 0.2.0.pre.alpha → 0.5.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: b858676f9797dd49895ba65127dad51d3e4884592a7c542f22b9e120b8c05618
4
- data.tar.gz: '049a27f4096feba8cf4967fea14d249cee3d9093416206e99389813966b01420'
3
+ metadata.gz: fed8b6305a5a4231734f7658d2577617ad4c32df1ec070871371c70225ae5296
4
+ data.tar.gz: f2c18df64f442916800fc6011849cce93e1256e4bf8a3ec93830e87bf3eefd0b
5
5
  SHA512:
6
- metadata.gz: 7444b6856724a83cb30a8533164967cf949bd9e279091d593123ef9c694a6f0b581bfe01ccceba6c7252725bd64a7db185372e1a0ab495183ab6c4e216fdb225
7
- data.tar.gz: b04bd46c3ba05d7d8deca7a17b2807ee1c7e620492d5f1f96442cd47aefbf4e8dad78608c1a298f24ed327bff2bead87ad7fabc8391db592dee6e59541784d8e
6
+ metadata.gz: 79a2cb71a0e784547f3f10e006f22a96f516b8428b3a7e66892bdf067dfa8c9ddd97ce3dc3b338fe88c22a88a3d28ca3050b0ccc4f7fc359cda9f815b682e193
7
+ data.tar.gz: 85244d986e0ff0c5545e74f6df9d740f8f1f97af0d9ad2e890a80ca5b0d9b8cf3384b872abdacc74d4f1f593974da9d7c8f171a5a2986eed2008702b0a4cb7db
data/CHANGELOG.md CHANGED
@@ -7,3 +7,7 @@
7
7
  ## [0.2.0-alpha] - 2022-09-02
8
8
 
9
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.2.0.pre.alpha)
4
+ highlighter (0.5.0.pre.alpha)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # Highlighter
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/highlighter`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ An easy way to serialize ruby objects :)
6
4
 
7
5
  ## Installation
8
6
 
@@ -98,6 +96,36 @@ end
98
96
  ```ruby
99
97
  UserSerializer.new(user, custom_serializer: CarSerializer).to_h
100
98
  ```
99
+ Renaming attribute:
100
+ ```ruby
101
+ class UserSerializer
102
+ include Highlighter::Serializer
103
+
104
+ attribute :id
105
+ attribute :name, rename_to: :full_name
106
+ end
107
+ ```
108
+ ```ruby
109
+ {
110
+ id: 1,
111
+ full_name: "Kelly"
112
+ }
113
+ ```
114
+ Passing a list of fields:
115
+ ```ruby
116
+ class UserSerializer
117
+ include Highlighter::Serializer
118
+
119
+ attributes :id, :name
120
+ end
121
+ ```
122
+ ```ruby
123
+ {
124
+ id: 1,
125
+ name: "Kelly"
126
+ }
127
+ ```
128
+
101
129
  ## Development
102
130
 
103
131
  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,21 @@
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, :block
7
7
 
8
- def initialize(name:, serializer: nil)
9
- @name = name
8
+ def initialize(field:, serializer: nil, rename_to: nil, &block)
9
+ @field = field
10
10
  @serializer = serializer
11
+ @rename_to = rename_to
12
+ @block = block
13
+ end
14
+
15
+ def name
16
+ rename_to.nil? ? field : rename_to
17
+ end
18
+
19
+ def block?
20
+ !block.nil?
11
21
  end
12
22
  end
13
23
  end
@@ -15,7 +15,9 @@ module Highlighter
15
15
  end
16
16
 
17
17
  def call
18
- value = object.send(attribute.name)
18
+ return attribute.block.call(object) if attribute.block?
19
+
20
+ value = object.send(attribute.field)
19
21
  return serialize_array(value) if value.instance_of? Array
20
22
 
21
23
  apply_serializer(value, attribute)
@@ -16,20 +16,31 @@ module Highlighter
16
16
  return if @object.nil?
17
17
 
18
18
  Commands::ObjectSerializer.call(object: @object,
19
- attributes: self.class.attributes,
19
+ attributes: self.class.attribute_list,
20
20
  options: @options)
21
21
  end
22
22
  end
23
23
 
24
24
  # Add methods to expose and set up attributes
25
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:)
26
+ def attribute(field, serializer: nil, rename_to: nil, &block)
27
+ attribute_list << if block_given?
28
+ Attribute.new(field:, serializer:, rename_to:, &block)
29
+ else
30
+ Attribute.new(field:, serializer:, rename_to:)
31
+ end
29
32
  end
30
33
 
31
- def attributes
32
- instance_variable_get(:@attributes)
34
+ def attributes(*fields)
35
+ fields.each do |field|
36
+ attribute_list << Attribute.new(field:)
37
+ end
38
+ end
39
+
40
+ def attribute_list
41
+ instance_variable_set(:@attribute_list, []) unless instance_variable_defined?(:@attribute_list)
42
+
43
+ instance_variable_get(:@attribute_list)
33
44
  end
34
45
  end
35
46
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Highlighter
4
- VERSION = "0.2.0-alpha"
4
+ VERSION = "0.5.0-alpha"
5
5
  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.2.0.pre.alpha
4
+ version: 0.5.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-09-02 00:00:00.000000000 Z
11
+ date: 2022-09-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Serializer ruby objects in simple and straightforward way
14
14
  email: