highlighter 0.4.0.pre.alpha → 0.6.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 +4 -4
- data/CHANGELOG.md +13 -1
- data/Gemfile.lock +1 -1
- data/README.md +37 -5
- data/lib/highlighter/attribute.rb +14 -2
- data/lib/highlighter/commands/attribute_serializer.rb +4 -1
- data/lib/highlighter/commands/object_serializer.rb +1 -1
- data/lib/highlighter/serializer.rb +6 -2
- data/lib/highlighter/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: da9405d1bba458ff552240bdeed67c2abcd06eeb584db494a9bbb33c92527573
|
4
|
+
data.tar.gz: d01563fa024c95fe7ae3ba9f7e6b14a2db3fb5d829bcfe32f7bd08ddec22db01
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 48a77c360a0e83fd0fc5c4d766fe8b0a9bd4d9b81fff7845f4d62fefbfc9c53d603d8d81f12cc867c580bdca46512275070bdf1a06f7677edf831f8e5b3ef3bd
|
7
|
+
data.tar.gz: 42061bf99fd777e7bcac9ad6fe5c8af85ab888926eaadc5e5ae5e28f873617ad6abbc13b3b450969acf40300f3acc5a263020a0cac39363b14273db121831713
|
data/CHANGELOG.md
CHANGED
@@ -10,4 +10,16 @@
|
|
10
10
|
|
11
11
|
## [0.3.0-alpha] - 2022-09-03
|
12
12
|
|
13
|
-
- Feature: rename attribute
|
13
|
+
- Feature: rename attribute
|
14
|
+
|
15
|
+
## [0.4.0-alpha] - 2022-09-04
|
16
|
+
|
17
|
+
- Feature: accept list of fields
|
18
|
+
|
19
|
+
## [0.5.0-alpha] - 2022-09-09
|
20
|
+
|
21
|
+
- Feature: field supports block
|
22
|
+
|
23
|
+
## [0.6.0-alpha] - 2022-11-19
|
24
|
+
|
25
|
+
- Feature: add support to choose which fields will be shown
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# Highlighter
|
2
2
|
|
3
|
-
|
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
|
|
@@ -68,11 +66,11 @@ cars = [
|
|
68
66
|
Car.new(id: 1, name: 'Polo', manufacturer: 'VW'),
|
69
67
|
Car.new(id: 2, name: 'Focus', manufacturer: 'Ford')
|
70
68
|
]
|
71
|
-
user = User.new(id: 1, name: "Kelly", cars: cars)
|
69
|
+
user = User.new(id: 1, name: "Kelly", cars: cars, )
|
72
70
|
```
|
73
71
|
Now that we have the instances of cars and user in place we just need to instatiate the serializer and call `to_h`
|
74
72
|
```ruby
|
75
|
-
UserSerializer.new(user).to_h
|
73
|
+
UserSerializer.new(user, show_description: false).to_h
|
76
74
|
```
|
77
75
|
And it will return:
|
78
76
|
```ruby
|
@@ -120,6 +118,40 @@ class UserSerializer
|
|
120
118
|
|
121
119
|
attributes :id, :name
|
122
120
|
end
|
121
|
+
```
|
122
|
+
```ruby
|
123
|
+
{
|
124
|
+
id: 1,
|
125
|
+
name: "Kelly"
|
126
|
+
}
|
127
|
+
```
|
128
|
+
Passing a block:
|
129
|
+
```ruby
|
130
|
+
class UserSerializer
|
131
|
+
include Highlighter::Serializer
|
132
|
+
|
133
|
+
attributes :id, :name
|
134
|
+
attribute :description do |object|
|
135
|
+
"My name is #{object.name}"
|
136
|
+
end
|
137
|
+
end
|
138
|
+
```
|
139
|
+
```ruby
|
140
|
+
{
|
141
|
+
id: 1,
|
142
|
+
name: "Kelly",
|
143
|
+
description: "My name is John Wick"
|
144
|
+
}
|
145
|
+
```
|
146
|
+
Hiding field
|
147
|
+
```ruby
|
148
|
+
class UserSerializer
|
149
|
+
include Highlighter::Serializer
|
150
|
+
|
151
|
+
attributes :id, :name
|
152
|
+
attribute :description, if: ->(_object, options) { option[:show_description] }
|
153
|
+
end
|
154
|
+
|
123
155
|
```
|
124
156
|
```ruby
|
125
157
|
{
|
@@ -3,16 +3,28 @@
|
|
3
3
|
module Highlighter
|
4
4
|
# Holds info on attribute
|
5
5
|
class Attribute
|
6
|
-
attr_reader :field, :serializer, :rename_to
|
6
|
+
attr_reader :field, :serializer, :rename_to, :block, :show_if
|
7
7
|
|
8
|
-
def initialize(field:, serializer: nil, rename_to: nil)
|
8
|
+
def initialize(field:, serializer: nil, rename_to: nil, show_if: nil, &block)
|
9
9
|
@field = field
|
10
10
|
@serializer = serializer
|
11
11
|
@rename_to = rename_to
|
12
|
+
@show_if = show_if
|
13
|
+
@block = block
|
12
14
|
end
|
13
15
|
|
14
16
|
def name
|
15
17
|
rename_to.nil? ? field : rename_to
|
16
18
|
end
|
19
|
+
|
20
|
+
def block?
|
21
|
+
!block.nil?
|
22
|
+
end
|
23
|
+
|
24
|
+
def show?(object, options)
|
25
|
+
return true unless show_if.is_a? Proc
|
26
|
+
|
27
|
+
show_if.call(object, options)
|
28
|
+
end
|
17
29
|
end
|
18
30
|
end
|
@@ -14,7 +14,10 @@ module Highlighter
|
|
14
14
|
@options = options
|
15
15
|
end
|
16
16
|
|
17
|
-
def call
|
17
|
+
def call # rubocop:disable Metrics/AbcSize
|
18
|
+
return unless attribute.show?(object, options)
|
19
|
+
return attribute.block.call(object) if attribute.block?
|
20
|
+
|
18
21
|
value = object.send(attribute.field)
|
19
22
|
return serialize_array(value) if value.instance_of? Array
|
20
23
|
|
@@ -23,8 +23,12 @@ module Highlighter
|
|
23
23
|
|
24
24
|
# Add methods to expose and set up attributes
|
25
25
|
module ClassMethods
|
26
|
-
def attribute(field,
|
27
|
-
attribute_list << Attribute.new(field:,
|
26
|
+
def attribute(field, **options, &block)
|
27
|
+
attribute_list << Attribute.new(field:,
|
28
|
+
serializer: options[:serializer],
|
29
|
+
rename_to: options[:rename_to],
|
30
|
+
show_if: options[:if],
|
31
|
+
&block)
|
28
32
|
end
|
29
33
|
|
30
34
|
def attributes(*fields)
|
data/lib/highlighter/version.rb
CHANGED
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.
|
4
|
+
version: 0.6.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-
|
11
|
+
date: 2022-11-19 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Serializer ruby objects in simple and straightforward way
|
14
14
|
email:
|