tag_options 1.1.0 → 1.2.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 +5 -0
- data/README.md +26 -9
- data/lib/tag_options/hash.rb +2 -2
- data/lib/tag_options/hash_at.rb +11 -0
- data/lib/tag_options/resolver.rb +1 -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: 6e2a2a1f14c436d95569da59c07b36ea56ca1ef5f0a69546cb445423243d69ab
|
4
|
+
data.tar.gz: 2a5f69f7f214a7a00d3dfd47d7b627968960c6525ee4c65ec91a4590cdbab140
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2eee9711c6e3cf2163ef43aa45ea15c50fa247c1dada13d21e225e0cab19dbc5e52cb3843168d23cd33434d8e496e4a6da12247232cc881af3ec5f98a1adf0ff
|
7
|
+
data.tar.gz: cb281c5639b6381795d7d7f975ef5cf558ac0f9065ad38ac25d9c62e7257748b84df3d07a9f72d161709ae436c4712196aa26805bffc359522b1db0719305fcc
|
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,11 @@
|
|
2
2
|
|
3
3
|
## [Unreleased]
|
4
4
|
|
5
|
+
## [1.2.0] - 2023-03-01
|
6
|
+
|
7
|
+
- Added `at().default!` option for setting values that are not already present.
|
8
|
+
- Fix for passing an array of values to `combine1` or `set!`
|
9
|
+
|
5
10
|
## [1.1.0] - 2023-03-01
|
6
11
|
|
7
12
|
- Switched to inheriting from ActiveSupport::HashWithIndifferentAccess.
|
data/README.md
CHANGED
@@ -41,6 +41,7 @@ Would render:
|
|
41
41
|
- [General Usage](#general-usage)
|
42
42
|
- [combine!](#combine)
|
43
43
|
- [set!](#set)
|
44
|
+
- [default!](#default)
|
44
45
|
- [Conditional Usage](#conditional-usage)
|
45
46
|
- [Custom Property Resolvers](#custom-property-resolvers)
|
46
47
|
- [Development](#development)
|
@@ -71,9 +72,12 @@ TagOptions::Hash.new
|
|
71
72
|
|
72
73
|
hash = {class: "flex"}
|
73
74
|
TagOptions::Hash.new(hash)
|
74
|
-
=> {
|
75
|
+
=> {"class"=>"flex"}
|
75
76
|
```
|
76
77
|
|
78
|
+
`TagOptions::Hash` inherits from `ActiveSupport::HashWithIndifferentAccess`,
|
79
|
+
implementing a hash where keys `:foo` and `"foo"` are considered to be the same.
|
80
|
+
|
77
81
|
### combine!
|
78
82
|
|
79
83
|
Combine HTML attributes with an existing `TagOptions::Hash` by chaining `at` and
|
@@ -82,7 +86,7 @@ Combine HTML attributes with an existing `TagOptions::Hash` by chaining `at` and
|
|
82
86
|
```ruby
|
83
87
|
options = TagOptions::Hash.new(class: "flex")
|
84
88
|
options.at(:class).combine!("mt-1")
|
85
|
-
=> {
|
89
|
+
=> {"class"=>"flex mt-1"}
|
86
90
|
```
|
87
91
|
|
88
92
|
Values can also be specified as arrays.
|
@@ -90,7 +94,7 @@ Values can also be specified as arrays.
|
|
90
94
|
```ruby
|
91
95
|
options = TagOptions::Hash.new(class: "flex")
|
92
96
|
options.at(:class).combine!(["mt-1", "mx-2"])
|
93
|
-
=> {
|
97
|
+
=> {"class"=>"flex mt-1 mx-2"}
|
94
98
|
```
|
95
99
|
|
96
100
|
HTML attributes are only added if they don't already exist.
|
@@ -98,7 +102,7 @@ HTML attributes are only added if they don't already exist.
|
|
98
102
|
```ruby
|
99
103
|
options = TagOptions::Hash.new(class: "flex")
|
100
104
|
options.at(:class).combine!("flex flex-col")
|
101
|
-
=> {
|
105
|
+
=> {"class"=>"flex flex-col"}
|
102
106
|
```
|
103
107
|
|
104
108
|
You can also combine values on nested hashes.
|
@@ -106,7 +110,7 @@ You can also combine values on nested hashes.
|
|
106
110
|
```ruby
|
107
111
|
options = TagOptions::Hash.new(class: "flex", data: {controller: "dropdown"})
|
108
112
|
options.at(:data, :controller).combine!("toggle")
|
109
|
-
=> {
|
113
|
+
=> {"class"=>"flex", "data"=>{"controller"=>"dropdown toggle"}}
|
110
114
|
```
|
111
115
|
|
112
116
|
If a nested hash doesn't already exist it will be automatically added.
|
@@ -114,7 +118,7 @@ If a nested hash doesn't already exist it will be automatically added.
|
|
114
118
|
```ruby
|
115
119
|
options = TagOptions::Hash.new(class: "flex")
|
116
120
|
options.at(:data, :controller).combine!("dropdown")
|
117
|
-
=> {
|
121
|
+
=> {"class"=>"flex", "data"=>{"controller"=>"dropdown"}}
|
118
122
|
```
|
119
123
|
|
120
124
|
### set!
|
@@ -126,7 +130,20 @@ existing values.
|
|
126
130
|
```ruby
|
127
131
|
options = TagOptions::Hash.new(class: "flex")
|
128
132
|
options.at(:class).set!("block")
|
129
|
-
=> {
|
133
|
+
=> {"class"=>"block"}
|
134
|
+
```
|
135
|
+
|
136
|
+
### default!
|
137
|
+
|
138
|
+
Chaining `at` and `default!` functions nearly the same as `set!` with all the
|
139
|
+
same usage patterns. The only difference is that the default method will only
|
140
|
+
set the specified values if the value is not already specified.
|
141
|
+
|
142
|
+
```ruby
|
143
|
+
options = TagOptions::Hash.new(class: "flex")
|
144
|
+
options.at(:class).default!("block")
|
145
|
+
options.at(:role).default!("alert")
|
146
|
+
=> {"class"=>"flex", "role"=>"alert"}
|
130
147
|
```
|
131
148
|
|
132
149
|
## Conditional Usage
|
@@ -139,7 +156,7 @@ unconditionally and key/value pairs have their key added _IF_ the value is true.
|
|
139
156
|
# assuming `centered?` returns `true`
|
140
157
|
options = TagOptions::Hash.new(class: "flex")
|
141
158
|
options.at(:class).combine!("mt-1", "mx-auto": centered?, "mx-2": !centered?)
|
142
|
-
=> {
|
159
|
+
=> {"class"=>"flex mt-1 mx-auto"}
|
143
160
|
```
|
144
161
|
|
145
162
|
## Custom Property Resolvers
|
@@ -164,7 +181,7 @@ pass `as: :style` to `at`.
|
|
164
181
|
```ruby
|
165
182
|
options = TagOptions::Hash.new(style: "display: block; margin-left: 0;")
|
166
183
|
options.at(:style, as: :style).combine!("display: flex; margin-right: 0;")
|
167
|
-
=> {
|
184
|
+
=> {"style"=>"display: flex; margin-left: 0; margin-right: 0;"}
|
168
185
|
```
|
169
186
|
|
170
187
|
A `TagOptions::Resolver` class is available if you wish to implement your own
|
data/lib/tag_options/hash.rb
CHANGED
@@ -28,9 +28,9 @@ module TagOptions
|
|
28
28
|
populated_keys = []
|
29
29
|
data = self
|
30
30
|
keys.each do |key|
|
31
|
-
data[key] ||=
|
31
|
+
data[key] ||= self.class.new
|
32
32
|
data = data[key]
|
33
|
-
unless data.is_a?(
|
33
|
+
unless data.is_a?(self.class)
|
34
34
|
raise TagOptions::Errors::NotHashError.new(populated_keys, type: data.class)
|
35
35
|
end
|
36
36
|
end
|
data/lib/tag_options/hash_at.rb
CHANGED
@@ -15,6 +15,11 @@ module TagOptions
|
|
15
15
|
set_value! @resolver.call(current_value, *values, **conditions)
|
16
16
|
end
|
17
17
|
|
18
|
+
def default!(*values, **conditions)
|
19
|
+
@opt_hash.populate!(*@keys)
|
20
|
+
set_default! @resolver.call(*values, **conditions)
|
21
|
+
end
|
22
|
+
|
18
23
|
def set!(*values, **conditions)
|
19
24
|
@opt_hash.populate!(*@keys)
|
20
25
|
set_value! @resolver.call(*values, **conditions)
|
@@ -22,6 +27,12 @@ module TagOptions
|
|
22
27
|
|
23
28
|
private
|
24
29
|
|
30
|
+
def set_default!(value)
|
31
|
+
root = @opt_hash.dig(*@keys)
|
32
|
+
root[@value_key] = value unless root.key?(@value_key)
|
33
|
+
@opt_hash
|
34
|
+
end
|
35
|
+
|
25
36
|
def set_value!(value)
|
26
37
|
@opt_hash.dig(*@keys)[@value_key] = value
|
27
38
|
@opt_hash
|
data/lib/tag_options/resolver.rb
CHANGED
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.2.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-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|