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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6e2a2a1f14c436d95569da59c07b36ea56ca1ef5f0a69546cb445423243d69ab
4
- data.tar.gz: 2a5f69f7f214a7a00d3dfd47d7b627968960c6525ee4c65ec91a4590cdbab140
3
+ metadata.gz: c947925081a723a10b0cb16c68efa3895556df3f87246bb0b9795d8b942e01f3
4
+ data.tar.gz: c8b215bf6c0a2f8ee726055350c78c07efbe9575f22defb6ab483a01df23eb5d
5
5
  SHA512:
6
- metadata.gz: 2eee9711c6e3cf2163ef43aa45ea15c50fa247c1dada13d21e225e0cab19dbc5e52cb3843168d23cd33434d8e496e4a6da12247232cc881af3ec5f98a1adf0ff
7
- data.tar.gz: cb281c5639b6381795d7d7f975ef5cf558ac0f9065ad38ac25d9c62e7257748b84df3d07a9f72d161709ae436c4712196aa26805bffc359522b1db0719305fcc
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
- ## [1.2.0] - 2023-03-01
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
- => {"class"=>"flex"}
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
- => {"class"=>"flex mt-1"}
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
- => {"class"=>"flex mt-1 mx-2"}
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
- => {"class"=>"flex flex-col"}
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
- => {"class"=>"flex", "data"=>{"controller"=>"dropdown toggle"}}
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
- => {"class"=>"flex", "data"=>{"controller"=>"dropdown"}}
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
- => {"class"=>"block"}
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
- => {"class"=>"flex", "role"=>"alert"}
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
- => {"class"=>"flex mt-1 mx-auto"}
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
- => {"style"=>"display: flex; margin-left: 0; margin-right: 0;"}
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
@@ -0,0 +1,7 @@
1
+ module TagOptions
2
+ module ConvertKey
3
+ def convert_key(key)
4
+ key&.to_s&.to_sym
5
+ end
6
+ end
7
+ end
@@ -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.size.zero? ? self : super
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
@@ -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
 
@@ -1,3 +1,3 @@
1
1
  module TagOptions
2
- VERSION = "1.2.0"
2
+ VERSION = "1.2.1"
3
3
  end
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.0
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