tag_options 1.2.1 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -1
- data/README.md +45 -3
- data/lib/tag_options/hash_at.rb +19 -0
- data/lib/tag_options/resolver.rb +4 -0
- data/lib/tag_options/resolvers/default.rb +5 -1
- data/lib/tag_options/resolvers/style.rb +5 -1
- data/lib/tag_options/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: 5cee8f74f051ffc41909e62f0a613904ce5f342a6be1fd8d961b8f7663d66087
|
4
|
+
data.tar.gz: 5e8c5f2b69fe32e13330a99f4d112a412bc4e6b3a78a5233d66f11a3ad032e7c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d6613c09fbf756a9a15e3a4001e478a0c225f643c1dd3f8b58be3e63fe8c3afb0dab00bed9d8984ca75ae58b37ae4fbb4e26d0410b7d36bb0cf59f52fecbd869
|
7
|
+
data.tar.gz: ed905332c293482cd3610c5374bb9a7b27328cd1c245bb55c6d69fc10b4c36e7c67f6a38ca3a32ef3a0f879344c9751b9a22ae18f91f1ed21d4bf5a6b7a8cac4
|
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,14 @@
|
|
2
2
|
|
3
3
|
## [Unreleased]
|
4
4
|
|
5
|
+
## [1.3.0] - 2023-03-03
|
6
|
+
|
7
|
+
- Added `at().remove!` option for removing values.
|
8
|
+
|
9
|
+
**NOTE**: If you have implemented custom resolvers, you will need to modify them
|
10
|
+
in order to support `remove!`. For examples, see the [built-in
|
11
|
+
handlers](https://github.com/wamonroe/tag_options/tree/main/lib/tag_options/resolvers)
|
12
|
+
for more information.
|
5
13
|
|
6
14
|
## [1.2.1] - 2023-03-02
|
7
15
|
|
@@ -23,7 +31,8 @@
|
|
23
31
|
## [1.0.0] - 2022-06-14
|
24
32
|
|
25
33
|
- Rewrote and simplified TagOptions::Hash and supporting classes.
|
26
|
-
|
34
|
+
|
35
|
+
**BREAKING CHANGES**: Read documentation for updated usage before updating.
|
27
36
|
|
28
37
|
## [0.9.3] - 2021-11-11
|
29
38
|
|
data/README.md
CHANGED
@@ -42,6 +42,7 @@ Would render:
|
|
42
42
|
- [combine!](#combine)
|
43
43
|
- [set!](#set)
|
44
44
|
- [default!](#default)
|
45
|
+
- [remove!](#remove)
|
45
46
|
- [Conditional Usage](#conditional-usage)
|
46
47
|
- [Custom Property Resolvers](#custom-property-resolvers)
|
47
48
|
- [Development](#development)
|
@@ -149,11 +150,31 @@ options.at(:role).default!("alert")
|
|
149
150
|
=> {:class=>"flex", :role=>"alert"}
|
150
151
|
```
|
151
152
|
|
153
|
+
### remove!
|
154
|
+
|
155
|
+
Remove HTML attributes from an existing `TagOptions::Hash` by chaining `at` and
|
156
|
+
`remove!`.
|
157
|
+
|
158
|
+
```ruby
|
159
|
+
options = TagOptions::Hash.new(class: "flex ml-1 mr-1")
|
160
|
+
options.at(:class).remove!("mr-1")
|
161
|
+
=> {:class=>"flex ml-1"}
|
162
|
+
```
|
163
|
+
|
164
|
+
In addition to string values, you can also pass regular expression to `remove!`.
|
165
|
+
|
166
|
+
```ruby
|
167
|
+
options = TagOptions::Hash.new(class: "flex ml-1 mr-2")
|
168
|
+
options.at(:class).remove!(/m.-\d/)
|
169
|
+
=> {:class=>"flex"}
|
170
|
+
```
|
171
|
+
|
152
172
|
## Conditional Usage
|
153
173
|
|
154
|
-
|
155
|
-
|
156
|
-
unconditionally and key/value pairs have their key
|
174
|
+
The `combine!`, `set!`, `default!`, and `remove!` methods allow for values to be
|
175
|
+
conditionally resolved using an argument array. Where the values are passed
|
176
|
+
unconditionally and key/value pairs have their key passed _IF_ the value is
|
177
|
+
true.
|
157
178
|
|
158
179
|
```ruby
|
159
180
|
# assuming `centered?` returns `true`
|
@@ -162,6 +183,27 @@ options.at(:class).combine!("mt-1", "mx-auto": centered?, "mx-2": !centered?)
|
|
162
183
|
=> {:class=>"flex mt-1 mx-auto"}
|
163
184
|
```
|
164
185
|
|
186
|
+
```ruby
|
187
|
+
# assuming `centered?` returns `true`
|
188
|
+
options = TagOptions::Hash.new(class: "flex")
|
189
|
+
options.at(:class).set!("block", "mx-auto": centered?, "mx-2": !centered?)
|
190
|
+
=> {:class=>"block mx-auto"}
|
191
|
+
```
|
192
|
+
|
193
|
+
```ruby
|
194
|
+
# assuming `centered?` returns `true`
|
195
|
+
options = TagOptions::Hash.new(role: "alert")
|
196
|
+
options.at(:class).default!("flex", "mx-auto": centered?, "mx-2": !centered?)
|
197
|
+
=> {:role=>"alert", :class=>"flex mx-auto"}
|
198
|
+
```
|
199
|
+
|
200
|
+
```ruby
|
201
|
+
# assuming `centered?` returns `true`
|
202
|
+
options = TagOptions::Hash.new(class: "flex mx-auto mx-2")
|
203
|
+
options.at(:class).remove!("mt-1", "mx-auto": centered?, "mx-2": !centered?)
|
204
|
+
=> {:class=>"flex mx-2"}
|
205
|
+
```
|
206
|
+
|
165
207
|
## Custom Property Resolvers
|
166
208
|
|
167
209
|
Chaining `at` to `combine!` or `set!` processes HTML properties similar to
|
data/lib/tag_options/hash_at.rb
CHANGED
@@ -28,8 +28,27 @@ module TagOptions
|
|
28
28
|
set_value! @resolver.call(*values, **conditions)
|
29
29
|
end
|
30
30
|
|
31
|
+
def remove!(*values, **conditions)
|
32
|
+
@opt_hash.populate!(*@keys)
|
33
|
+
regex_values, values = values.flatten.partition { |v| v.is_a?(Regexp) }
|
34
|
+
remove_values!(*regex_values, *@resolver.values(*values, **conditions))
|
35
|
+
end
|
36
|
+
|
31
37
|
private
|
32
38
|
|
39
|
+
def remove_values!(*values_to_remove)
|
40
|
+
values = @resolver.values(@opt_hash.dig(*@keys)[@value_key])
|
41
|
+
values_to_remove.each do |value|
|
42
|
+
if value.is_a?(Regexp)
|
43
|
+
values.reject! { |current_value| value.match?(current_value) }
|
44
|
+
else
|
45
|
+
values.reject! { |current_value| value == current_value }
|
46
|
+
end
|
47
|
+
end
|
48
|
+
@opt_hash.dig(*@keys)[@value_key] = @resolver.call(*values)
|
49
|
+
@opt_hash
|
50
|
+
end
|
51
|
+
|
33
52
|
def set_default!(value)
|
34
53
|
root = @opt_hash.dig(*@keys)
|
35
54
|
root[@value_key] = value unless root.key?(@value_key)
|
data/lib/tag_options/resolver.rb
CHANGED
@@ -4,7 +4,11 @@ module TagOptions
|
|
4
4
|
module Resolvers
|
5
5
|
class Default < Resolver
|
6
6
|
def call
|
7
|
-
|
7
|
+
values.join(" ")
|
8
|
+
end
|
9
|
+
|
10
|
+
def values
|
11
|
+
@values.map { |v| v.to_s.split }.flatten.compact.uniq
|
8
12
|
end
|
9
13
|
end
|
10
14
|
end
|
data/lib/tag_options/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tag_options
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Monroe
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-03-
|
11
|
+
date: 2023-03-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|