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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fed8b6305a5a4231734f7658d2577617ad4c32df1ec070871371c70225ae5296
4
- data.tar.gz: f2c18df64f442916800fc6011849cce93e1256e4bf8a3ec93830e87bf3eefd0b
3
+ metadata.gz: da9405d1bba458ff552240bdeed67c2abcd06eeb584db494a9bbb33c92527573
4
+ data.tar.gz: d01563fa024c95fe7ae3ba9f7e6b14a2db3fb5d829bcfe32f7bd08ddec22db01
5
5
  SHA512:
6
- metadata.gz: 79a2cb71a0e784547f3f10e006f22a96f516b8428b3a7e66892bdf067dfa8c9ddd97ce3dc3b338fe88c22a88a3d28ca3050b0ccc4f7fc359cda9f815b682e193
7
- data.tar.gz: 85244d986e0ff0c5545e74f6df9d740f8f1f97af0d9ad2e890a80ca5b0d9b8cf3384b872abdacc74d4f1f593974da9d7c8f171a5a2986eed2008702b0a4cb7db
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
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- highlighter (0.5.0.pre.alpha)
4
+ highlighter (0.6.0.pre.alpha)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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)
@@ -16,7 +16,7 @@ module Highlighter
16
16
 
17
17
  def call
18
18
  attributes.each_with_object({}) do |attr, hash|
19
- hash[attr.name] = serialize_attribute(attr)
19
+ hash[attr.name] = serialize_attribute(attr) if attr.show?(object, options)
20
20
  hash
21
21
  end
22
22
  end
@@ -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, 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
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)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Highlighter
4
- VERSION = "0.5.0-alpha"
4
+ VERSION = "0.6.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.5.0.pre.alpha
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-09-10 00:00:00.000000000 Z
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: