tag_options 1.1.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 +14 -1
- data/README.md +20 -0
- data/lib/tag_options/convert_key.rb +7 -0
- data/lib/tag_options/hash.rb +9 -5
- data/lib/tag_options/hash_at.rb +16 -2
- data/lib/tag_options/resolver.rb +1 -1
- data/lib/tag_options/version.rb +1 -1
- metadata +3 -2
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,9 +2,22 @@
|
|
2
2
|
|
3
3
|
## [Unreleased]
|
4
4
|
|
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
|
14
|
+
|
15
|
+
- Added `at().default!` option for setting values that are not already present.
|
16
|
+
- Fix for passing an array of values to `combine1` or `set!`
|
17
|
+
|
5
18
|
## [1.1.0] - 2023-03-01
|
6
19
|
|
7
|
-
- Switched to inheriting from ActiveSupport::HashWithIndifferentAccess
|
20
|
+
- Switched to inheriting from `ActiveSupport::HashWithIndifferentAccess`.
|
8
21
|
- Added before/after/around initialize callback support.
|
9
22
|
|
10
23
|
## [1.0.0] - 2022-06-14
|
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)
|
@@ -74,6 +75,12 @@ 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
|
+
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`.
|
83
|
+
|
77
84
|
### combine!
|
78
85
|
|
79
86
|
Combine HTML attributes with an existing `TagOptions::Hash` by chaining `at` and
|
@@ -129,6 +136,19 @@ options.at(:class).set!("block")
|
|
129
136
|
=> {:class=>"block"}
|
130
137
|
```
|
131
138
|
|
139
|
+
### default!
|
140
|
+
|
141
|
+
Chaining `at` and `default!` functions nearly the same as `set!` with all the
|
142
|
+
same usage patterns. The only difference is that the default method will only
|
143
|
+
set the specified values if the value is not already specified.
|
144
|
+
|
145
|
+
```ruby
|
146
|
+
options = TagOptions::Hash.new(class: "flex")
|
147
|
+
options.at(:class).default!("block")
|
148
|
+
options.at(:role).default!("alert")
|
149
|
+
=> {:class=>"flex", :role=>"alert"}
|
150
|
+
```
|
151
|
+
|
132
152
|
## Conditional Usage
|
133
153
|
|
134
154
|
Both the `combine!` and `set!` allow for values to be conditionally added to
|
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,16 +24,17 @@ 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] ||=
|
32
|
-
data = data[key]
|
33
|
-
unless data.is_a?(
|
35
|
+
data[convert_key(key)] ||= self.class.new
|
36
|
+
data = data[convert_key(key)]
|
37
|
+
unless data.is_a?(self.class)
|
34
38
|
raise TagOptions::Errors::NotHashError.new(populated_keys, type: data.class)
|
35
39
|
end
|
36
40
|
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
|
|
@@ -15,6 +18,11 @@ module TagOptions
|
|
15
18
|
set_value! @resolver.call(current_value, *values, **conditions)
|
16
19
|
end
|
17
20
|
|
21
|
+
def default!(*values, **conditions)
|
22
|
+
@opt_hash.populate!(*@keys)
|
23
|
+
set_default! @resolver.call(*values, **conditions)
|
24
|
+
end
|
25
|
+
|
18
26
|
def set!(*values, **conditions)
|
19
27
|
@opt_hash.populate!(*@keys)
|
20
28
|
set_value! @resolver.call(*values, **conditions)
|
@@ -22,6 +30,12 @@ module TagOptions
|
|
22
30
|
|
23
31
|
private
|
24
32
|
|
33
|
+
def set_default!(value)
|
34
|
+
root = @opt_hash.dig(*@keys)
|
35
|
+
root[@value_key] = value unless root.key?(@value_key)
|
36
|
+
@opt_hash
|
37
|
+
end
|
38
|
+
|
25
39
|
def set_value!(value)
|
26
40
|
@opt_hash.dig(*@keys)[@value_key] = value
|
27
41
|
@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.1
|
4
|
+
version: 1.2.1
|
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
|
@@ -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
|