tag_options 1.2.0 → 1.2.1
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 +10 -2
- data/README.md +13 -10
- data/lib/tag_options/convert_key.rb +7 -0
- data/lib/tag_options/hash.rb +8 -4
- data/lib/tag_options/hash_at.rb +5 -2
- data/lib/tag_options/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c947925081a723a10b0cb16c68efa3895556df3f87246bb0b9795d8b942e01f3
|
4
|
+
data.tar.gz: c8b215bf6c0a2f8ee726055350c78c07efbe9575f22defb6ab483a01df23eb5d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8764d6f0ebd02bebdc40bf449a58a21b5eea8ac8746fabb5c45980403f121efde6f210011b396f788ca8d6ca8cb5c553c032cdffff71c53a4558af3f1406d07c
|
7
|
+
data.tar.gz: 0caa39cf680e022b64609c224e359188e1025d9c52bddb2c80ae5e1d60e9a1cdadf3b45581e927119971d552b1c4014f8ed4e0a809be6c4199229bcea30849b5
|
data/CHANGELOG.md
CHANGED
@@ -2,14 +2,22 @@
|
|
2
2
|
|
3
3
|
## [Unreleased]
|
4
4
|
|
5
|
-
|
5
|
+
|
6
|
+
## [1.2.1] - 2023-03-02
|
7
|
+
|
8
|
+
- Fixed bug introduced when switching to
|
9
|
+
`ActiveSupport::HashWithIndifferentAccess` that prevented a `TagOptions::Hash`
|
10
|
+
from being passed to a method using double splat, e.g. `some_method
|
11
|
+
**options`.
|
12
|
+
|
13
|
+
## [1.2.0] - 2023-03-02
|
6
14
|
|
7
15
|
- Added `at().default!` option for setting values that are not already present.
|
8
16
|
- Fix for passing an array of values to `combine1` or `set!`
|
9
17
|
|
10
18
|
## [1.1.0] - 2023-03-01
|
11
19
|
|
12
|
-
- Switched to inheriting from ActiveSupport::HashWithIndifferentAccess
|
20
|
+
- Switched to inheriting from `ActiveSupport::HashWithIndifferentAccess`.
|
13
21
|
- Added before/after/around initialize callback support.
|
14
22
|
|
15
23
|
## [1.0.0] - 2022-06-14
|
data/README.md
CHANGED
@@ -72,11 +72,14 @@ TagOptions::Hash.new
|
|
72
72
|
|
73
73
|
hash = {class: "flex"}
|
74
74
|
TagOptions::Hash.new(hash)
|
75
|
-
=> {
|
75
|
+
=> {:class=>"flex"}
|
76
76
|
```
|
77
77
|
|
78
78
|
`TagOptions::Hash` inherits from `ActiveSupport::HashWithIndifferentAccess`,
|
79
79
|
implementing a hash where keys `:foo` and `"foo"` are considered to be the same.
|
80
|
+
It differs from `ActiveSupport::HashWithIndifferentAccess`, however, by storing
|
81
|
+
the keys as symbols instead of strings to make it easier to pass the hash as
|
82
|
+
an method argument using double splat, e.g. `some_method **options`.
|
80
83
|
|
81
84
|
### combine!
|
82
85
|
|
@@ -86,7 +89,7 @@ Combine HTML attributes with an existing `TagOptions::Hash` by chaining `at` and
|
|
86
89
|
```ruby
|
87
90
|
options = TagOptions::Hash.new(class: "flex")
|
88
91
|
options.at(:class).combine!("mt-1")
|
89
|
-
=> {
|
92
|
+
=> {:class=>"flex mt-1"}
|
90
93
|
```
|
91
94
|
|
92
95
|
Values can also be specified as arrays.
|
@@ -94,7 +97,7 @@ Values can also be specified as arrays.
|
|
94
97
|
```ruby
|
95
98
|
options = TagOptions::Hash.new(class: "flex")
|
96
99
|
options.at(:class).combine!(["mt-1", "mx-2"])
|
97
|
-
=> {
|
100
|
+
=> {:class=>"flex mt-1 mx-2"}
|
98
101
|
```
|
99
102
|
|
100
103
|
HTML attributes are only added if they don't already exist.
|
@@ -102,7 +105,7 @@ HTML attributes are only added if they don't already exist.
|
|
102
105
|
```ruby
|
103
106
|
options = TagOptions::Hash.new(class: "flex")
|
104
107
|
options.at(:class).combine!("flex flex-col")
|
105
|
-
=> {
|
108
|
+
=> {:class=>"flex flex-col"}
|
106
109
|
```
|
107
110
|
|
108
111
|
You can also combine values on nested hashes.
|
@@ -110,7 +113,7 @@ You can also combine values on nested hashes.
|
|
110
113
|
```ruby
|
111
114
|
options = TagOptions::Hash.new(class: "flex", data: {controller: "dropdown"})
|
112
115
|
options.at(:data, :controller).combine!("toggle")
|
113
|
-
=> {
|
116
|
+
=> {:class=>"flex", :data=>{:controller=>"dropdown toggle"}
|
114
117
|
```
|
115
118
|
|
116
119
|
If a nested hash doesn't already exist it will be automatically added.
|
@@ -118,7 +121,7 @@ If a nested hash doesn't already exist it will be automatically added.
|
|
118
121
|
```ruby
|
119
122
|
options = TagOptions::Hash.new(class: "flex")
|
120
123
|
options.at(:data, :controller).combine!("dropdown")
|
121
|
-
=> {
|
124
|
+
=> {:class=>"flex", :data=>{:controller=>"dropdown"}
|
122
125
|
```
|
123
126
|
|
124
127
|
### set!
|
@@ -130,7 +133,7 @@ existing values.
|
|
130
133
|
```ruby
|
131
134
|
options = TagOptions::Hash.new(class: "flex")
|
132
135
|
options.at(:class).set!("block")
|
133
|
-
=> {
|
136
|
+
=> {:class=>"block"}
|
134
137
|
```
|
135
138
|
|
136
139
|
### default!
|
@@ -143,7 +146,7 @@ set the specified values if the value is not already specified.
|
|
143
146
|
options = TagOptions::Hash.new(class: "flex")
|
144
147
|
options.at(:class).default!("block")
|
145
148
|
options.at(:role).default!("alert")
|
146
|
-
=> {
|
149
|
+
=> {:class=>"flex", :role=>"alert"}
|
147
150
|
```
|
148
151
|
|
149
152
|
## Conditional Usage
|
@@ -156,7 +159,7 @@ unconditionally and key/value pairs have their key added _IF_ the value is true.
|
|
156
159
|
# assuming `centered?` returns `true`
|
157
160
|
options = TagOptions::Hash.new(class: "flex")
|
158
161
|
options.at(:class).combine!("mt-1", "mx-auto": centered?, "mx-2": !centered?)
|
159
|
-
=> {
|
162
|
+
=> {:class=>"flex mt-1 mx-auto"}
|
160
163
|
```
|
161
164
|
|
162
165
|
## Custom Property Resolvers
|
@@ -181,7 +184,7 @@ pass `as: :style` to `at`.
|
|
181
184
|
```ruby
|
182
185
|
options = TagOptions::Hash.new(style: "display: block; margin-left: 0;")
|
183
186
|
options.at(:style, as: :style).combine!("display: flex; margin-right: 0;")
|
184
|
-
=> {
|
187
|
+
=> {:style=>"display: flex; margin-left: 0; margin-right: 0;"}
|
185
188
|
```
|
186
189
|
|
187
190
|
A `TagOptions::Resolver` class is available if you wish to implement your own
|
data/lib/tag_options/hash.rb
CHANGED
@@ -1,17 +1,20 @@
|
|
1
1
|
require "active_support/callbacks"
|
2
2
|
require "active_support/core_ext/hash/indifferent_access"
|
3
|
+
require "tag_options/convert_key"
|
3
4
|
require "tag_options/hash_at"
|
4
5
|
require "tag_options/errors/not_hash_error"
|
5
6
|
|
6
7
|
module TagOptions
|
7
8
|
class Hash < ActiveSupport::HashWithIndifferentAccess
|
8
9
|
include ActiveSupport::Callbacks
|
10
|
+
include ConvertKey
|
11
|
+
|
9
12
|
define_callbacks :initialize
|
10
13
|
|
11
14
|
def initialize(hash = {})
|
12
15
|
run_callbacks :initialize do
|
13
16
|
hash.each do |key, value|
|
14
|
-
self[key] = value.is_a?(::Hash) ? self.class.new(value) : value
|
17
|
+
self[convert_key(key)] = value.is_a?(::Hash) ? self.class.new(value) : value
|
15
18
|
end
|
16
19
|
end
|
17
20
|
end
|
@@ -21,15 +24,16 @@ module TagOptions
|
|
21
24
|
end
|
22
25
|
|
23
26
|
def dig(*keys)
|
24
|
-
keys.
|
27
|
+
keys = keys.map { |key| convert_key(key) }
|
28
|
+
keys.size.zero? ? self : super(*keys)
|
25
29
|
end
|
26
30
|
|
27
31
|
def populate!(*keys)
|
28
32
|
populated_keys = []
|
29
33
|
data = self
|
30
34
|
keys.each do |key|
|
31
|
-
data[key] ||= self.class.new
|
32
|
-
data = data[key]
|
35
|
+
data[convert_key(key)] ||= self.class.new
|
36
|
+
data = data[convert_key(key)]
|
33
37
|
unless data.is_a?(self.class)
|
34
38
|
raise TagOptions::Errors::NotHashError.new(populated_keys, type: data.class)
|
35
39
|
end
|
data/lib/tag_options/hash_at.rb
CHANGED
@@ -1,11 +1,14 @@
|
|
1
1
|
require "tag_options/configuration"
|
2
|
+
require "tag_options/convert_key"
|
2
3
|
|
3
4
|
module TagOptions
|
4
5
|
class HashAt
|
6
|
+
include ConvertKey
|
7
|
+
|
5
8
|
def initialize(opt_hash:, keys:, as:)
|
6
9
|
@opt_hash = opt_hash
|
7
|
-
@keys = keys[..-2]
|
8
|
-
@value_key = keys[-1]
|
10
|
+
@keys = keys[..-2].map { |key| convert_key(key) }
|
11
|
+
@value_key = convert_key(keys[-1])
|
9
12
|
@resolver = TagOptions.configuration.resolver(as)
|
10
13
|
end
|
11
14
|
|
data/lib/tag_options/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tag_options
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Monroe
|
@@ -51,6 +51,7 @@ files:
|
|
51
51
|
- Rakefile
|
52
52
|
- lib/tag_options.rb
|
53
53
|
- lib/tag_options/configuration.rb
|
54
|
+
- lib/tag_options/convert_key.rb
|
54
55
|
- lib/tag_options/error.rb
|
55
56
|
- lib/tag_options/errors/not_hash_error.rb
|
56
57
|
- lib/tag_options/errors/resolver_error.rb
|