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 +4 -4
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +1 -1
- data/README.md +31 -3
- data/lib/highlighter/attribute.rb +13 -3
- data/lib/highlighter/commands/attribute_serializer.rb +3 -1
- data/lib/highlighter/serializer.rb +17 -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: fed8b6305a5a4231734f7658d2577617ad4c32df1ec070871371c70225ae5296
|
4
|
+
data.tar.gz: f2c18df64f442916800fc6011849cce93e1256e4bf8a3ec93830e87bf3eefd0b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 79a2cb71a0e784547f3f10e006f22a96f516b8428b3a7e66892bdf067dfa8c9ddd97ce3dc3b338fe88c22a88a3d28ca3050b0ccc4f7fc359cda9f815b682e193
|
7
|
+
data.tar.gz: 85244d986e0ff0c5545e74f6df9d740f8f1f97af0d9ad2e890a80ca5b0d9b8cf3384b872abdacc74d4f1f593974da9d7c8f171a5a2986eed2008702b0a4cb7db
|
data/CHANGELOG.md
CHANGED
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
|
|
@@ -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 :
|
6
|
+
attr_reader :field, :serializer, :rename_to, :block
|
7
7
|
|
8
|
-
def initialize(
|
9
|
-
@
|
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
|
-
|
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.
|
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(
|
27
|
-
|
28
|
-
|
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
|
-
|
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
|
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.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-
|
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:
|