tag_options 1.0.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 900cd0b42ae1f34b69b7cf227cb4f6bc91cb560f3b04424cdf3e2fd41e5d3f61
4
- data.tar.gz: b4af6738e8d551d795bab1ef057bb47f49de8d73ba32068c839d04bf50e78a43
3
+ metadata.gz: 6e2a2a1f14c436d95569da59c07b36ea56ca1ef5f0a69546cb445423243d69ab
4
+ data.tar.gz: 2a5f69f7f214a7a00d3dfd47d7b627968960c6525ee4c65ec91a4590cdbab140
5
5
  SHA512:
6
- metadata.gz: 3322cbce916927b777b7e915e181102cdbc7497c42095edc0e11f009d9a6a3884e8649ce6826d0199ba0ac3e9fe1a8c039bc5bd8c490a6db027714f08e6d88c4
7
- data.tar.gz: 0d1ebc95b92d489d19c49b33acead9abe88a9fc5932865941454100e11bb877db6baacf7380a85f0495c3d47d3807d02e6d8bbfe08d654dbfb6b17a2940c8393
6
+ metadata.gz: 2eee9711c6e3cf2163ef43aa45ea15c50fa247c1dada13d21e225e0cab19dbc5e52cb3843168d23cd33434d8e496e4a6da12247232cc881af3ec5f98a1adf0ff
7
+ data.tar.gz: cb281c5639b6381795d7d7f975ef5cf558ac0f9065ad38ac25d9c62e7257748b84df3d07a9f72d161709ae436c4712196aa26805bffc359522b1db0719305fcc
data/CHANGELOG.md CHANGED
@@ -2,10 +2,20 @@
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
+
10
+ ## [1.1.0] - 2023-03-01
11
+
12
+ - Switched to inheriting from ActiveSupport::HashWithIndifferentAccess.
13
+ - Added before/after/around initialize callback support.
14
+
5
15
  ## [1.0.0] - 2022-06-14
6
16
 
7
17
  - Rewrote and simplified TagOptions::Hash and supporting classes.
8
- - BREAKING CHANGES, read documentation for updated usage before updating
18
+ - BREAKING CHANGES, read documentation for updated usage before updating.
9
19
 
10
20
  ## [0.9.3] - 2021-11-11
11
21
 
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Tag Options
2
2
 
3
+ [![Test](https://github.com/wamonroe/tag_options/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/wamonroe/tag_options/actions/workflows/test.yml)
4
+
3
5
  Simple library for manipulating options passed to the Rails `tag`,
4
6
  `content_tag`, and other tag helpers.
5
7
 
@@ -33,16 +35,18 @@ Would render:
33
35
 
34
36
  ## Table of Contents
35
37
 
36
- - [Installation](#installation)
37
- - [General Usage](#general-usage)
38
- - [combine!](#combine)
39
- - [set!](#set)
40
- - [Conditional Usage](#conditional-usage)
41
- - [Property Resolvers](#property-resolvers)
42
- - [Development](#development)
43
- - [Contributing](#contributing)
44
- - [To Do](#to-do)
45
- - [License](#license)
38
+ - [Tag Options](#tag-options)
39
+ - [Table of Contents](#table-of-contents)
40
+ - [Installation](#installation)
41
+ - [General Usage](#general-usage)
42
+ - [combine!](#combine)
43
+ - [set!](#set)
44
+ - [default!](#default)
45
+ - [Conditional Usage](#conditional-usage)
46
+ - [Custom Property Resolvers](#custom-property-resolvers)
47
+ - [Development](#development)
48
+ - [Contributing](#contributing)
49
+ - [License](#license)
46
50
 
47
51
  ## Installation
48
52
 
@@ -68,9 +72,12 @@ TagOptions::Hash.new
68
72
 
69
73
  hash = {class: "flex"}
70
74
  TagOptions::Hash.new(hash)
71
- => {:class=>"flex"}
75
+ => {"class"=>"flex"}
72
76
  ```
73
77
 
78
+ `TagOptions::Hash` inherits from `ActiveSupport::HashWithIndifferentAccess`,
79
+ implementing a hash where keys `:foo` and `"foo"` are considered to be the same.
80
+
74
81
  ### combine!
75
82
 
76
83
  Combine HTML attributes with an existing `TagOptions::Hash` by chaining `at` and
@@ -79,7 +86,7 @@ Combine HTML attributes with an existing `TagOptions::Hash` by chaining `at` and
79
86
  ```ruby
80
87
  options = TagOptions::Hash.new(class: "flex")
81
88
  options.at(:class).combine!("mt-1")
82
- => {:class=>"flex mt-1"}
89
+ => {"class"=>"flex mt-1"}
83
90
  ```
84
91
 
85
92
  Values can also be specified as arrays.
@@ -87,7 +94,7 @@ Values can also be specified as arrays.
87
94
  ```ruby
88
95
  options = TagOptions::Hash.new(class: "flex")
89
96
  options.at(:class).combine!(["mt-1", "mx-2"])
90
- => {:class=>"flex mt-1 mx-2"}
97
+ => {"class"=>"flex mt-1 mx-2"}
91
98
  ```
92
99
 
93
100
  HTML attributes are only added if they don't already exist.
@@ -95,7 +102,7 @@ HTML attributes are only added if they don't already exist.
95
102
  ```ruby
96
103
  options = TagOptions::Hash.new(class: "flex")
97
104
  options.at(:class).combine!("flex flex-col")
98
- => {:class=>"flex flex-col"}
105
+ => {"class"=>"flex flex-col"}
99
106
  ```
100
107
 
101
108
  You can also combine values on nested hashes.
@@ -103,7 +110,7 @@ You can also combine values on nested hashes.
103
110
  ```ruby
104
111
  options = TagOptions::Hash.new(class: "flex", data: {controller: "dropdown"})
105
112
  options.at(:data, :controller).combine!("toggle")
106
- => {:class=>"flex", :data=>{:controller=>"dropdown toggle"}
113
+ => {"class"=>"flex", "data"=>{"controller"=>"dropdown toggle"}}
107
114
  ```
108
115
 
109
116
  If a nested hash doesn't already exist it will be automatically added.
@@ -111,7 +118,7 @@ If a nested hash doesn't already exist it will be automatically added.
111
118
  ```ruby
112
119
  options = TagOptions::Hash.new(class: "flex")
113
120
  options.at(:data, :controller).combine!("dropdown")
114
- => {:class=>"flex", :data=>{:controller=>"dropdown"}
121
+ => {"class"=>"flex", "data"=>{"controller"=>"dropdown"}}
115
122
  ```
116
123
 
117
124
  ### set!
@@ -123,7 +130,20 @@ existing values.
123
130
  ```ruby
124
131
  options = TagOptions::Hash.new(class: "flex")
125
132
  options.at(:class).set!("block")
126
- => {: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"}
127
147
  ```
128
148
 
129
149
  ## Conditional Usage
@@ -136,7 +156,7 @@ unconditionally and key/value pairs have their key added _IF_ the value is true.
136
156
  # assuming `centered?` returns `true`
137
157
  options = TagOptions::Hash.new(class: "flex")
138
158
  options.at(:class).combine!("mt-1", "mx-auto": centered?, "mx-2": !centered?)
139
- => {:class=>"flex mt-1 mx-auto"}
159
+ => {"class"=>"flex mt-1 mx-auto"}
140
160
  ```
141
161
 
142
162
  ## Custom Property Resolvers
@@ -161,7 +181,7 @@ pass `as: :style` to `at`.
161
181
  ```ruby
162
182
  options = TagOptions::Hash.new(style: "display: block; margin-left: 0;")
163
183
  options.at(:style, as: :style).combine!("display: flex; margin-right: 0;")
164
- => {:style=>"display: flex; margin-left: 0; margin-right: 0;"}
184
+ => {"style"=>"display: flex; margin-left: 0; margin-right: 0;"}
165
185
  ```
166
186
 
167
187
  A `TagOptions::Resolver` class is available if you wish to implement your own
@@ -185,7 +205,7 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
185
205
  - `bin/console` for an interactive prompt that will allow you to experiment
186
206
  - `bin/rubocop` to run RuboCop to check the code style and formatting
187
207
 
188
- To install this gem onto your local machine, run `bundle exec rake install`. To
208
+ To build this gem on your local machine, run `bundle exec rake build`. To
189
209
  release a new version, update the version number in `version.rb`, and then run
190
210
  `bundle exec rake release`, which will create a git tag for the version, push
191
211
  git commits and the created tag, and push the `.gem` file to
@@ -1,11 +1,18 @@
1
+ require "active_support/callbacks"
2
+ require "active_support/core_ext/hash/indifferent_access"
1
3
  require "tag_options/hash_at"
2
4
  require "tag_options/errors/not_hash_error"
3
5
 
4
6
  module TagOptions
5
- class Hash < ::Hash
7
+ class Hash < ActiveSupport::HashWithIndifferentAccess
8
+ include ActiveSupport::Callbacks
9
+ define_callbacks :initialize
10
+
6
11
  def initialize(hash = {})
7
- hash.each do |key, value|
8
- self[key] = value.is_a?(::Hash) ? TagOptions::Hash.new(value) : value
12
+ run_callbacks :initialize do
13
+ hash.each do |key, value|
14
+ self[key] = value.is_a?(::Hash) ? self.class.new(value) : value
15
+ end
9
16
  end
10
17
  end
11
18
 
@@ -21,9 +28,9 @@ module TagOptions
21
28
  populated_keys = []
22
29
  data = self
23
30
  keys.each do |key|
24
- data[key] ||= TagOptions::Hash.new
31
+ data[key] ||= self.class.new
25
32
  data = data[key]
26
- unless data.is_a?(TagOptions::Hash)
33
+ unless data.is_a?(self.class)
27
34
  raise TagOptions::Errors::NotHashError.new(populated_keys, type: data.class)
28
35
  end
29
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.0.0"
2
+ VERSION = "1.2.0"
3
3
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tag_options
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.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: 2022-06-15 00:00:00.000000000 Z
11
+ date: 2023-03-02 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: rspec
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -68,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
68
82
  - !ruby/object:Gem::Version
69
83
  version: '0'
70
84
  requirements: []
71
- rubygems_version: 3.3.7
85
+ rubygems_version: 3.4.6
72
86
  signing_key:
73
87
  specification_version: 4
74
88
  summary: Simple library for manipulating options passed to various Rails tag helpers.