highlighter 0.5.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 +36 -2
- data/lib/highlighter/attribute.rb +9 -2
- data/lib/highlighter/commands/attribute_serializer.rb +2 -1
- data/lib/highlighter/commands/object_serializer.rb +1 -1
- data/lib/highlighter/serializer.rb +6 -6
- 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
@@ -66,11 +66,11 @@ cars = [
|
|
66
66
|
Car.new(id: 1, name: 'Polo', manufacturer: 'VW'),
|
67
67
|
Car.new(id: 2, name: 'Focus', manufacturer: 'Ford')
|
68
68
|
]
|
69
|
-
user = User.new(id: 1, name: "Kelly", cars: cars)
|
69
|
+
user = User.new(id: 1, name: "Kelly", cars: cars, )
|
70
70
|
```
|
71
71
|
Now that we have the instances of cars and user in place we just need to instatiate the serializer and call `to_h`
|
72
72
|
```ruby
|
73
|
-
UserSerializer.new(user).to_h
|
73
|
+
UserSerializer.new(user, show_description: false).to_h
|
74
74
|
```
|
75
75
|
And it will return:
|
76
76
|
```ruby
|
@@ -118,6 +118,40 @@ class UserSerializer
|
|
118
118
|
|
119
119
|
attributes :id, :name
|
120
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
|
+
|
121
155
|
```
|
122
156
|
```ruby
|
123
157
|
{
|
@@ -3,12 +3,13 @@
|
|
3
3
|
module Highlighter
|
4
4
|
# Holds info on attribute
|
5
5
|
class Attribute
|
6
|
-
attr_reader :field, :serializer, :rename_to, :block
|
6
|
+
attr_reader :field, :serializer, :rename_to, :block, :show_if
|
7
7
|
|
8
|
-
def initialize(field:, serializer: nil, rename_to: nil, &block)
|
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
|
12
13
|
@block = block
|
13
14
|
end
|
14
15
|
|
@@ -19,5 +20,11 @@ module Highlighter
|
|
19
20
|
def block?
|
20
21
|
!block.nil?
|
21
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
|
22
29
|
end
|
23
30
|
end
|
@@ -14,7 +14,8 @@ 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)
|
18
19
|
return attribute.block.call(object) if attribute.block?
|
19
20
|
|
20
21
|
value = object.send(attribute.field)
|
@@ -23,12 +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 <<
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
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)
|
32
32
|
end
|
33
33
|
|
34
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:
|