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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fc57165cc3381a2749507abc7746f95d2ede9786820b7ff3e8d2f9624981fd66
4
- data.tar.gz: 593a5b19b6d616a407dd7b214186c43d3d478514f2607b318957485e717dcb21
3
+ metadata.gz: da9405d1bba458ff552240bdeed67c2abcd06eeb584db494a9bbb33c92527573
4
+ data.tar.gz: d01563fa024c95fe7ae3ba9f7e6b14a2db3fb5d829bcfe32f7bd08ddec22db01
5
5
  SHA512:
6
- metadata.gz: 70557187e4553b5049f2bf15da03c5f40710b6cc5daf133c15190d51d01193272f147d91e14b7c43a632c832abcfef2325cd2ae5b20608a3f95080c2525dc45d
7
- data.tar.gz: 9601811f3dc47ef0396432d4fa949d1098a95dee414e7d3e921f59ee8662ce3a884d1a453e0434698c8763b2127d4b3b77630f3cf6e34ace048d944528eafbd3
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.4.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
@@ -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
 
@@ -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
 
@@ -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,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, serializer: nil, rename_to: nil)
27
- attribute_list << Attribute.new(field:, serializer:, rename_to:)
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)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Highlighter
4
- VERSION = "0.4.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.4.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-04 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: