tag_options 1.1.0 → 1.2.0

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: 5802916aaa57253843bc9fa02aed005828d4ffa30a8ccf514b9949c5c0859d2d
4
- data.tar.gz: 80ee0b00682ee8d94595039cd1569c0152d0dedb225a78672986416118bec6fe
3
+ metadata.gz: 6e2a2a1f14c436d95569da59c07b36ea56ca1ef5f0a69546cb445423243d69ab
4
+ data.tar.gz: 2a5f69f7f214a7a00d3dfd47d7b627968960c6525ee4c65ec91a4590cdbab140
5
5
  SHA512:
6
- metadata.gz: f498f2aa015310a8b4d3800dcb435c5b946665f5f53bf54630a80666e3c56f05c54b75788ea84b2d57829d2369404408621a4b2b7adeec1c5668fad2e61ac37a
7
- data.tar.gz: 9e04caa1298cec89d34af81daa7c11b51c18020c3bb013aaf7b104de5f1e5c80d545aeaae334929d3c2796d5fa00f31aafb59028b588bf9aefd218278e56bb9c
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
- => {:class=>"flex"}
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
- => {:class=>"flex mt-1"}
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
- => {:class=>"flex mt-1 mx-2"}
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
- => {:class=>"flex flex-col"}
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
- => {:class=>"flex", :data=>{:controller=>"dropdown toggle"}
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
- => {:class=>"flex", :data=>{:controller=>"dropdown"}
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
- => {:class=>"block"}
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
- => {:class=>"flex mt-1 mx-auto"}
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
- => {:style=>"display: flex; margin-left: 0; margin-right: 0;"}
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
@@ -28,9 +28,9 @@ module TagOptions
28
28
  populated_keys = []
29
29
  data = self
30
30
  keys.each do |key|
31
- data[key] ||= TagOptions::Hash.new
31
+ data[key] ||= self.class.new
32
32
  data = data[key]
33
- unless data.is_a?(TagOptions::Hash)
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
@@ -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
@@ -1,7 +1,7 @@
1
1
  module TagOptions
2
2
  class Resolver
3
3
  def initialize(*values, **conditional_values)
4
- @values = [*values, *resolve_conditional_values(conditional_values)]
4
+ @values = [*values.flatten, *resolve_conditional_values(conditional_values)]
5
5
  end
6
6
 
7
7
  def self.call(...)
@@ -1,3 +1,3 @@
1
1
  module TagOptions
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
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.0
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-01 00:00:00.000000000 Z
11
+ date: 2023-03-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport