classy-yaml 1.5.0 → 1.6.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/README.md +106 -94
- data/lib/classy/yaml/helpers.rb +29 -1
- data/lib/classy/yaml/tag_helper.rb +19 -0
- data/lib/classy/yaml/version.rb +1 -1
- data/lib/classy/yaml.rb +4 -19
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9290a4e662dd8d535fcaa8027679d3f9429fbadc848d22122be32e2816c60179
|
4
|
+
data.tar.gz: d17e3d9b702997ee5bc75d1bc8d7bc0d8819f3892176c6b26a0a2346487e991e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7b5971bbb9f71d9bc5f8a99791a89180c95ac752dac26a11d2560255d32c1a0f57ed2347c4df6caefccadd310ed1db937445a0c2e7edfa734603d51889ed814c
|
7
|
+
data.tar.gz: 854882375942f68865a146d84c1365a137307b4973bd42358491149b783959407e32aa07700e28f6321a0e9250273346932764320a72f136ab69240d4cb941f7
|
data/README.md
CHANGED
@@ -1,72 +1,119 @@
|
|
1
1
|
# Classy::Yaml
|
2
|
-
This was created to provide convenient utility class grouping for environments without a bundler (or any situation where you might not want to add custom css classes).
|
2
|
+
This was created to provide convenient utility class grouping for environments without a bundler (or any situation where you might not want to add custom css classes).
|
3
3
|
|
4
4
|
## Usage
|
5
5
|
After installing the gem, the helper method `yass` will be available from anywhere. It looks for the definitions in the YAML file `config/utility_classes.yml` (note: you must add this yourself).
|
6
6
|
|
7
|
-
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
8
10
|
|
11
|
+
```ruby
|
12
|
+
gem "classy-yaml"
|
9
13
|
```
|
10
|
-
|
11
|
-
|
12
|
-
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
```bash
|
17
|
+
$ bundle install
|
13
18
|
```
|
14
19
|
|
15
|
-
|
20
|
+
Or install it yourself as:
|
21
|
+
```bash
|
22
|
+
$ gem install classy-yaml
|
23
|
+
```
|
16
24
|
|
17
|
-
|
25
|
+
## Usage
|
18
26
|
|
27
|
+
### Default Settings
|
28
|
+
|
29
|
+
You can override any of the following settings in an initializer such as `config/initializers/classy_yaml.rb`:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
Classy::Yaml.setup do |config|
|
33
|
+
config.default_file = "config/utility_classes.yml"
|
34
|
+
config.extra_files = []
|
35
|
+
config.override_tag_helpers = false # Process all ERB tag helper class: attribute through `yass` when it is a non-string
|
36
|
+
end
|
19
37
|
```
|
20
|
-
btn:
|
21
|
-
base: "px-3 py-2"
|
22
|
-
blue: "text-blue-200 bg-blue-500"
|
23
|
-
yellow: "text-yellow-200 bg-blue-500"
|
24
|
-
```
|
25
38
|
|
26
|
-
|
39
|
+
### YAML Configuration
|
40
|
+
|
41
|
+
Create your utility classes YAML file (`config/utility_classes.yml`):
|
27
42
|
|
28
|
-
|
43
|
+
```yaml
|
44
|
+
btn:
|
45
|
+
base: "px-4 py-2 rounded font-medium" # Base class is automatically applied when referencing a sibling of base
|
46
|
+
primary: "bg-blue-500 text-white hover:bg-blue-600"
|
47
|
+
secondary: "bg-gray-200 text-gray-800 hover:bg-gray-300"
|
48
|
+
danger: "bg-red-500 text-white hover:bg-red-600"
|
29
49
|
|
30
|
-
|
31
|
-
|
50
|
+
card:
|
51
|
+
compact: "m-4"
|
32
52
|
```
|
53
|
+
|
54
|
+
You can also define classes as YAML arrays
|
55
|
+
|
56
|
+
```yaml
|
33
57
|
btn:
|
34
|
-
base: "px-
|
35
|
-
|
36
|
-
|
58
|
+
base: "px-4 py-2 rounded font-medium" # Base class is automatically applied when referencing a sibling of base
|
59
|
+
primary: "bg-blue-500 text-white hover:bg-blue-600"
|
60
|
+
secondary: "bg-gray-200 text-gray-800 hover:bg-gray-300"
|
61
|
+
danger: "bg-red-500 text-white hover:bg-red-600"
|
62
|
+
warning: # Hideous example
|
63
|
+
- "bg-yellow-200"
|
64
|
+
- "text-gray-100"
|
37
65
|
```
|
38
66
|
|
39
|
-
|
40
|
-
|
67
|
+
> [!WARNING]
|
68
|
+
> If using tailwind, be sure to point the tailwind configuration to your yaml files for parsing
|
41
69
|
|
42
|
-
###
|
43
|
-
There is a special helper built for ViewComponent and sidecar assets. In your `example_component.rb`, add the line `include Classy::Yaml::ComponentHelpers`. This helper will tell `yass` to check if there is a `example_component.yml` file, and first use that for definitions. If the definitions aren't found in the `example_component.yml`, then it will fallback to `config/utility_classes.yml`.
|
70
|
+
### Using the Helper
|
44
71
|
|
45
|
-
|
72
|
+
In your views or components:
|
46
73
|
|
47
|
-
```
|
48
|
-
|
49
|
-
btn:
|
50
|
-
|
51
|
-
|
52
|
-
yellow: "text-yellow-200 bg-blue-500"
|
53
|
-
|
54
|
-
|
55
|
-
# app/components/example_component/example_component.yml
|
56
|
-
btn:
|
57
|
-
purple: "text-purple-200 bg-blue-500"
|
58
|
-
|
74
|
+
```erb
|
75
|
+
<!-- Basic usage -->
|
76
|
+
<button class="<%= yass(btn: :primary) %>">
|
77
|
+
Click me
|
78
|
+
</button>
|
59
79
|
|
60
|
-
|
80
|
+
<!-- <button class="px-4 py-2 rounded font-medium bg-blue-500 text-white hover:bg-blue-600">Click me</button> -->
|
81
|
+
|
82
|
+
<!-- Multiple classes -->
|
83
|
+
<button class="<%= yass(btn: :primary, card: :compact) %>">
|
84
|
+
Button in card
|
85
|
+
</button>
|
86
|
+
|
87
|
+
<!-- <button class="px-4 py-2 rounded font-medium bg-blue-500 text-white hover:bg-blue-600 m-4">Click me</button> -->
|
88
|
+
|
89
|
+
<!-- Skip base classes -->
|
90
|
+
<button class="<%= yass(btn: :primary, skip_base: true) %>">
|
91
|
+
Button without base
|
92
|
+
</button>
|
61
93
|
|
62
|
-
<button class="
|
94
|
+
<!-- <button class="bg-blue-500 text-white hover:bg-blue-600">Click me</button> -->
|
63
95
|
|
64
|
-
|
96
|
+
<!-- With config.override_tag_helpers = true -->
|
97
|
+
<%= tag.div class: { btn: :primary, card: :compact } do %>
|
98
|
+
Automatic class processing
|
99
|
+
<% end %>
|
100
|
+
|
101
|
+
<div class="px-4 py-2 rounded font-medium bg-blue-500 text-white hover:bg-blue-600 m-4"></div>
|
65
102
|
```
|
66
103
|
|
67
|
-
|
104
|
+
### ViewComponent Integration
|
105
|
+
|
106
|
+
Include the component helpers in your ViewComponent:
|
68
107
|
|
108
|
+
```ruby
|
109
|
+
class ExampleComponent < ViewComponent::Base
|
110
|
+
include Classy::Yaml::ComponentHelpers
|
111
|
+
end
|
69
112
|
```
|
113
|
+
|
114
|
+
This will automatically look for a sidecar file `example_component.yml` file alongside your component. For example:
|
115
|
+
|
116
|
+
```yaml
|
70
117
|
# config/utility_classes.yml
|
71
118
|
btn:
|
72
119
|
base: "px-3 py-2"
|
@@ -76,73 +123,38 @@ btn:
|
|
76
123
|
|
77
124
|
# app/components/example_component/example_component.yml
|
78
125
|
btn:
|
79
|
-
|
80
|
-
|
126
|
+
purple: "text-purple-200 bg-blue-500"
|
127
|
+
|
81
128
|
|
82
129
|
# app/components/example_component/example_component.html.erb
|
83
130
|
|
84
|
-
<button class="<%=yass(btn: :purple)%>">Classy Button</button>
|
131
|
+
<button class="<%= yass(btn: :purple) %>">Classy Button</button>
|
85
132
|
|
86
|
-
#
|
133
|
+
# <button class="px-3 py-2 text-purple-200 bg-blue-500">Click me</button>
|
87
134
|
```
|
88
135
|
|
89
|
-
|
90
|
-
yass(:button, :secondary) # => "btn btn-secondary"
|
91
|
-
yass(:button, :large) # => "btn btn-lg"
|
92
|
-
yass(:button, :primary, :large) # => "btn btn-primary btn-lg"
|
93
|
-
|
94
|
-
### Array style definition
|
136
|
+
## Tailwind Merge Integration
|
95
137
|
|
96
|
-
|
97
|
-
array:
|
98
|
-
- "px-3 py-2"
|
99
|
-
- "bg-gray"
|
100
|
-
- "text-purple"
|
101
|
-
```
|
138
|
+
Classy YAML detects if [tailwind_merge](https://github.com/gjtorikian/tailwind_merge) is installed and will leverage it when fetching classes with `yass`. Please read their documentation for installation and benefits.
|
102
139
|
|
103
|
-
yass(:array) # => "px-3 py-2 bg-gray text-purple"
|
104
140
|
|
105
|
-
## Configuration
|
106
|
-
You can configure the gem by creating an initializer in your Rails app. The following options are available:
|
141
|
+
## Configuration Options
|
107
142
|
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
config.extra_files = [
|
115
|
-
"app/yamls/extra_styles.yml"
|
116
|
-
]
|
117
|
-
end
|
118
|
-
```
|
143
|
+
| Option | Default | Description |
|
144
|
+
|--------|---------|-------------|
|
145
|
+
| `default_file` | `"config/utility_classes.yml"` | Path to the main YAML file |
|
146
|
+
| `extra_files` | `[]` | Array of additional YAML files (highest priority) |
|
147
|
+
| `engine_files` | `[]` | Array of engine YAML files (lowest priority) |
|
148
|
+
| `override_tag_helpers` | `false` | Automatically process class symbols/hashes in Rails tag helpers |
|
119
149
|
|
120
|
-
##
|
121
|
-
Add this line to your application's Gemfile:
|
150
|
+
## Contributing
|
122
151
|
|
123
|
-
|
124
|
-
gem 'classy-yaml'
|
125
|
-
```
|
152
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Tonksthebear/classy-yaml. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](CODE_OF_CONDUCT.md).
|
126
153
|
|
127
|
-
|
128
|
-
```bash
|
129
|
-
$ bundle
|
130
|
-
```
|
154
|
+
## License
|
131
155
|
|
132
|
-
|
133
|
-
```bash
|
134
|
-
$ gem install classy-yaml
|
135
|
-
```
|
156
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
136
157
|
|
137
|
-
|
138
|
-
```
|
139
|
-
purge: [
|
140
|
-
"./config/utility_classes.yml",
|
141
|
-
"./app/components/**/*.yml" // If using view component sidecar assets
|
142
|
-
]
|
143
|
-
```
|
144
|
-
## Contributing
|
145
|
-
This is my first attempt at an actual gem usable for all. Any and all suggestions are absolutely welcome. I'm not the cleanest programmer, so code quality suggestions are welcome too 👍 I extracted this logic from my private work and I felt it was useful enough to be worth publishing.
|
158
|
+
## Code of Conduct
|
146
159
|
|
147
|
-
|
148
|
-
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
160
|
+
Everyone interacting in the Classy::Yaml project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](CODE_OF_CONDUCT.md).
|
data/lib/classy/yaml/helpers.rb
CHANGED
@@ -34,11 +34,39 @@ module Classy
|
|
34
34
|
keys, classes = flatten_args(values: args)
|
35
35
|
classes += fetch_classes(keys, classy_yamls: classy_yamls, skip_base: skip_base_hash[:skip_base])
|
36
36
|
|
37
|
-
|
37
|
+
# Use tailwind_merge if available, otherwise fall back to simple join
|
38
|
+
merge_classes(classes.flatten.uniq)
|
38
39
|
end
|
39
40
|
|
40
41
|
private
|
41
42
|
|
43
|
+
# Merges CSS classes using tailwind_merge if available, otherwise uses simple join
|
44
|
+
# @param classes [Array] Array of CSS class strings
|
45
|
+
# @return [String] Merged CSS classes
|
46
|
+
def merge_classes(classes)
|
47
|
+
return "" if classes.blank?
|
48
|
+
|
49
|
+
if tailwind_merge_available?
|
50
|
+
# Use tailwind_merge for intelligent class merging
|
51
|
+
TailwindMerge::Merger.new.merge(classes)
|
52
|
+
else
|
53
|
+
# Fall back to simple space-joined classes
|
54
|
+
classes.join(" ")
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# Checks if tailwind_merge gem is available
|
59
|
+
# @return [Boolean] True if tailwind_merge is available
|
60
|
+
def tailwind_merge_available?
|
61
|
+
return @tailwind_merge_available if defined?(@tailwind_merge_available)
|
62
|
+
@tailwind_merge_available = begin
|
63
|
+
require "tailwind_merge"
|
64
|
+
true
|
65
|
+
rescue LoadError
|
66
|
+
false
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
42
70
|
# Loads a YAML file and adds its contents to the classy_yamls array
|
43
71
|
# @param file_path [String, Pathname] Path to the YAML file
|
44
72
|
# @param classy_yamls [Array] Array to add the parsed YAML to
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Classy
|
2
|
+
module Yaml
|
3
|
+
module TagHelper
|
4
|
+
include Classy::Yaml::Helpers
|
5
|
+
|
6
|
+
def tag_options(options, escape = true)
|
7
|
+
if options
|
8
|
+
class_key = options.key?(:class) ? :class : "class"
|
9
|
+
options = options.dup
|
10
|
+
val = options[class_key]
|
11
|
+
if val.is_a?(Symbol) || val.is_a?(Hash) || val.is_a?(Array)
|
12
|
+
options[:class] = yass(val)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
super(options, escape)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/classy/yaml/version.rb
CHANGED
data/lib/classy/yaml.rb
CHANGED
@@ -88,25 +88,10 @@ module Classy
|
|
88
88
|
private
|
89
89
|
|
90
90
|
def self.apply_tag_helper_override
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
unless method_defined?(:classy_yaml_original_tag_options)
|
96
|
-
alias_method :classy_yaml_original_tag_options, :tag_options
|
97
|
-
|
98
|
-
def tag_options(options, escape = true)
|
99
|
-
if options
|
100
|
-
class_key = options.key?(:class) ? :class : "class"
|
101
|
-
options = options.dup
|
102
|
-
val = options[class_key]
|
103
|
-
if val.is_a?(Symbol) || val.is_a?(Hash) || val.is_a?(Array)
|
104
|
-
options[:class] = yass(val)
|
105
|
-
end
|
106
|
-
end
|
107
|
-
classy_yaml_original_tag_options(options, escape)
|
108
|
-
end
|
109
|
-
end
|
91
|
+
require "classy/yaml/tag_helper"
|
92
|
+
|
93
|
+
ActiveSupport.on_load(:action_view) do
|
94
|
+
ActionView::Helpers::TagHelper::TagBuilder.prepend(Classy::Yaml::TagHelper)
|
110
95
|
end
|
111
96
|
end
|
112
97
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: classy-yaml
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.6.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tonksthebear
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-08-
|
11
|
+
date: 2025-08-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '6'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: tailwind_merge
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.0'
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: view_component
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -137,6 +151,7 @@ files:
|
|
137
151
|
- lib/classy/yaml/engine.rb
|
138
152
|
- lib/classy/yaml/helpers.rb
|
139
153
|
- lib/classy/yaml/invalid_key_error.rb
|
154
|
+
- lib/classy/yaml/tag_helper.rb
|
140
155
|
- lib/classy/yaml/version.rb
|
141
156
|
- lib/tasks/classy/yaml_tasks.rake
|
142
157
|
homepage: https://github.com/Tonksthebear/classy-yaml
|