attrify 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.standard.yml +6 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +14 -0
- data/Gemfile.lock +133 -0
- data/LICENSE.txt +21 -0
- data/README.md +212 -0
- data/Rakefile +10 -0
- data/lib/attrify/dsl/base.rb +28 -0
- data/lib/attrify/dsl/compound.rb +30 -0
- data/lib/attrify/dsl/engine.rb +47 -0
- data/lib/attrify/dsl/nested_variant.rb +36 -0
- data/lib/attrify/dsl/variant.rb +34 -0
- data/lib/attrify/helpers.rb +70 -0
- data/lib/attrify/operation_set.rb +220 -0
- data/lib/attrify/parser.rb +202 -0
- data/lib/attrify/variant.rb +67 -0
- data/lib/attrify/variant_registry.rb +108 -0
- data/lib/attrify/version.rb +5 -0
- data/lib/attrify.rb +53 -0
- data/sig/attribute_variants.rbs +4 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c15da6170447306522cb53c74db61937e5fc59d3e5a8619d40142c253be2ab7a
|
4
|
+
data.tar.gz: 70c850497d0d5ceec7a8e5693e806494477303bde85030bd3da862e98eead7c8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 512a4466eae20886eff9b71a615c7a08f251a3691fad2af0f8ec46ddec6fe9c039bdba4d27c56c61da909e95e5c8dfb4ecbde22f559aca151ce3c0a0fbbcfc7a
|
7
|
+
data.tar.gz: 7f2726c8394496baa5e7e4e7f5a2b4a70d3ec81495e725e44291db36a77b439c6993626c08800a4697a4c5223dd522bed3027820053088050c9107b43d54aafd
|
data/.rspec
ADDED
data/.standard.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at TODO: Write your email address. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in attrify.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem "rake", "~> 13.0"
|
9
|
+
|
10
|
+
gem "rspec", "~> 3.0"
|
11
|
+
|
12
|
+
gem "benchmark-ips"
|
13
|
+
gem "standard", "~> 1.3"
|
14
|
+
gem "standard-rails"
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
attrify (0.4.0)
|
5
|
+
actionview (> 7.0)
|
6
|
+
activesupport (> 7.0)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
actionview (7.1.4)
|
12
|
+
activesupport (= 7.1.4)
|
13
|
+
builder (~> 3.1)
|
14
|
+
erubi (~> 1.11)
|
15
|
+
rails-dom-testing (~> 2.2)
|
16
|
+
rails-html-sanitizer (~> 1.6)
|
17
|
+
activesupport (7.1.4)
|
18
|
+
base64
|
19
|
+
bigdecimal
|
20
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
21
|
+
connection_pool (>= 2.2.5)
|
22
|
+
drb
|
23
|
+
i18n (>= 1.6, < 2)
|
24
|
+
minitest (>= 5.1)
|
25
|
+
mutex_m
|
26
|
+
tzinfo (~> 2.0)
|
27
|
+
ast (2.4.2)
|
28
|
+
base64 (0.2.0)
|
29
|
+
benchmark-ips (2.14.0)
|
30
|
+
bigdecimal (3.1.8)
|
31
|
+
builder (3.3.0)
|
32
|
+
concurrent-ruby (1.3.4)
|
33
|
+
connection_pool (2.4.1)
|
34
|
+
crass (1.0.6)
|
35
|
+
diff-lcs (1.5.1)
|
36
|
+
drb (2.2.1)
|
37
|
+
erubi (1.13.0)
|
38
|
+
i18n (1.14.6)
|
39
|
+
concurrent-ruby (~> 1.0)
|
40
|
+
json (2.7.2)
|
41
|
+
language_server-protocol (3.17.0.3)
|
42
|
+
lint_roller (1.1.0)
|
43
|
+
loofah (2.22.0)
|
44
|
+
crass (~> 1.0.2)
|
45
|
+
nokogiri (>= 1.12.0)
|
46
|
+
minitest (5.25.1)
|
47
|
+
mutex_m (0.2.0)
|
48
|
+
nokogiri (1.15.6-x86_64-linux)
|
49
|
+
racc (~> 1.4)
|
50
|
+
parallel (1.26.3)
|
51
|
+
parser (3.3.5.0)
|
52
|
+
ast (~> 2.4.1)
|
53
|
+
racc
|
54
|
+
racc (1.8.1)
|
55
|
+
rack (3.1.7)
|
56
|
+
rails-dom-testing (2.2.0)
|
57
|
+
activesupport (>= 5.0.0)
|
58
|
+
minitest
|
59
|
+
nokogiri (>= 1.6)
|
60
|
+
rails-html-sanitizer (1.6.0)
|
61
|
+
loofah (~> 2.21)
|
62
|
+
nokogiri (~> 1.14)
|
63
|
+
rainbow (3.1.1)
|
64
|
+
rake (13.2.1)
|
65
|
+
regexp_parser (2.9.2)
|
66
|
+
rexml (3.3.7)
|
67
|
+
rspec (3.13.0)
|
68
|
+
rspec-core (~> 3.13.0)
|
69
|
+
rspec-expectations (~> 3.13.0)
|
70
|
+
rspec-mocks (~> 3.13.0)
|
71
|
+
rspec-core (3.13.1)
|
72
|
+
rspec-support (~> 3.13.0)
|
73
|
+
rspec-expectations (3.13.3)
|
74
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
75
|
+
rspec-support (~> 3.13.0)
|
76
|
+
rspec-mocks (3.13.1)
|
77
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
78
|
+
rspec-support (~> 3.13.0)
|
79
|
+
rspec-support (3.13.1)
|
80
|
+
rubocop (1.64.1)
|
81
|
+
json (~> 2.3)
|
82
|
+
language_server-protocol (>= 3.17.0)
|
83
|
+
parallel (~> 1.10)
|
84
|
+
parser (>= 3.3.0.2)
|
85
|
+
rainbow (>= 2.2.2, < 4.0)
|
86
|
+
regexp_parser (>= 1.8, < 3.0)
|
87
|
+
rexml (>= 3.2.5, < 4.0)
|
88
|
+
rubocop-ast (>= 1.31.1, < 2.0)
|
89
|
+
ruby-progressbar (~> 1.7)
|
90
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
91
|
+
rubocop-ast (1.32.3)
|
92
|
+
parser (>= 3.3.1.0)
|
93
|
+
rubocop-performance (1.21.1)
|
94
|
+
rubocop (>= 1.48.1, < 2.0)
|
95
|
+
rubocop-ast (>= 1.31.1, < 2.0)
|
96
|
+
rubocop-rails (2.23.1)
|
97
|
+
activesupport (>= 4.2.0)
|
98
|
+
rack (>= 1.1)
|
99
|
+
rubocop (>= 1.33.0, < 2.0)
|
100
|
+
rubocop-ast (>= 1.30.0, < 2.0)
|
101
|
+
ruby-progressbar (1.13.0)
|
102
|
+
standard (1.37.0)
|
103
|
+
language_server-protocol (~> 3.17.0.2)
|
104
|
+
lint_roller (~> 1.0)
|
105
|
+
rubocop (~> 1.64.0)
|
106
|
+
standard-custom (~> 1.0.0)
|
107
|
+
standard-performance (~> 1.4)
|
108
|
+
standard-custom (1.0.2)
|
109
|
+
lint_roller (~> 1.0)
|
110
|
+
rubocop (~> 1.50)
|
111
|
+
standard-performance (1.4.0)
|
112
|
+
lint_roller (~> 1.1)
|
113
|
+
rubocop-performance (~> 1.21.0)
|
114
|
+
standard-rails (1.0.2)
|
115
|
+
lint_roller (~> 1.0)
|
116
|
+
rubocop-rails (~> 2.23.1)
|
117
|
+
tzinfo (2.0.6)
|
118
|
+
concurrent-ruby (~> 1.0)
|
119
|
+
unicode-display_width (2.6.0)
|
120
|
+
|
121
|
+
PLATFORMS
|
122
|
+
x86_64-linux
|
123
|
+
|
124
|
+
DEPENDENCIES
|
125
|
+
attrify!
|
126
|
+
benchmark-ips
|
127
|
+
rake (~> 13.0)
|
128
|
+
rspec (~> 3.0)
|
129
|
+
standard (~> 1.3)
|
130
|
+
standard-rails
|
131
|
+
|
132
|
+
BUNDLED WITH
|
133
|
+
2.3.8
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 TODO: Write your name
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,212 @@
|
|
1
|
+
# Attrify
|
2
|
+
> [!WARNING]
|
3
|
+
> This a pre-release version of the gem. The API may change
|
4
|
+
|
5
|
+
|
6
|
+
:muscle: A powerful variant API for ruby components
|
7
|
+
|
8
|
+
- Define component variants directly in your ruby
|
9
|
+
- Framework agnostic
|
10
|
+
- Seamlessly handle complex UI components
|
11
|
+
|
12
|
+
[![Build Status](https://github.com/hectormf/attrify/actions/workflows/main.yml/badge.svg)](https://github.com/hectormf/attrify/actions)
|
13
|
+
[![License: MIT](https://cdn.prod.website-files.com/5e0f1144930a8bc8aace526c/65dd9eb5aaca434fac4f1c34_License-MIT-blue.svg)](/LICENSE.txt)
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's gemfile
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
gem "attrify"
|
21
|
+
```
|
22
|
+
|
23
|
+
## Getting Started
|
24
|
+
|
25
|
+
Add it to your model
|
26
|
+
```ruby
|
27
|
+
class BaseComponent
|
28
|
+
include Attrify
|
29
|
+
|
30
|
+
# Optional: pass keyword arguments from the initializer directly to the attributes API
|
31
|
+
def initialize(**args)
|
32
|
+
with_attributes(**args)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
Define attributes for your class:
|
38
|
+
```ruby
|
39
|
+
class Button < BaseComponent
|
40
|
+
attributes {
|
41
|
+
base id: ->{ "button_#{SecureRandom.uuid}}"},
|
42
|
+
class: %w[inline-flex items-center rounded],
|
43
|
+
data: { controller: "button_controller" }
|
44
|
+
|
45
|
+
variant(:color) {
|
46
|
+
primary class: { append: %w[bg-blue-500 text-white] }
|
47
|
+
secondary class: { append: %w[bg-gray-500 text-white shadow-sm] }
|
48
|
+
danger class: { append: %w[bg-red-500 text-white] }
|
49
|
+
}
|
50
|
+
|
51
|
+
variant(:size) {
|
52
|
+
xs class: { append: %w[text-xs h-7 px-3 py-1] }
|
53
|
+
sm class: { append: %w[text-sm h-8 px-3.5 py-1.5] }
|
54
|
+
md class: { append: %w[text-sm h-9 px-4 py-2] }
|
55
|
+
lg class: { append: %w[text-base h-10 px-5 py-2.5] }
|
56
|
+
xl class: { append: %w[text-lg h-11 px-6 py-3] }
|
57
|
+
}
|
58
|
+
|
59
|
+
default color: :primary, size: :md
|
60
|
+
}
|
61
|
+
end
|
62
|
+
```
|
63
|
+
|
64
|
+
Add the attributes to your component's html
|
65
|
+
```erb
|
66
|
+
<button <%= attribs() %>> <%= content %> </button>
|
67
|
+
```
|
68
|
+
|
69
|
+
Try it out!
|
70
|
+
```ruby
|
71
|
+
# Use the default attributes
|
72
|
+
Button.new() { "Click Me" }
|
73
|
+
|
74
|
+
# Use a predefined color and size
|
75
|
+
Button.new(color: :primary, size: :sm) { "Click Me" }
|
76
|
+
|
77
|
+
# Override the attributes as needed
|
78
|
+
Button.new(color: :primary,
|
79
|
+
id: "special_button",
|
80
|
+
class: { remove: "border" },
|
81
|
+
style: "width: 300px;",
|
82
|
+
href: "https://www.google.com") { "Click Me" }
|
83
|
+
```
|
84
|
+
|
85
|
+
## Operations
|
86
|
+
|
87
|
+
To best leverage the variants API it is important to understand it's workings.
|
88
|
+
Under the hood, attributes are parsed into operations.
|
89
|
+
|
90
|
+
List of operations:
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
{ set: value }
|
94
|
+
{ remove: value }
|
95
|
+
{ append: value }
|
96
|
+
{ prepend: value }
|
97
|
+
```
|
98
|
+
|
99
|
+
Let's take a look at some examples
|
100
|
+
```ruby
|
101
|
+
# The default operation is SET so the following buttons are equivelant
|
102
|
+
Button.new(color: :primary, class: "bg-purple-500")
|
103
|
+
Button.new(color: :primary, class: { set: "bg-purple-500" })
|
104
|
+
|
105
|
+
# We can define multiple operations like this:
|
106
|
+
Button.new(color: :primary, class: [{ remove: "bg-blue-500" },
|
107
|
+
{ prepend: "text-black" }])
|
108
|
+
```
|
109
|
+
|
110
|
+
## Slots
|
111
|
+
Slots allow you to define different attributes for different parts of a component.
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
class Card < BaseComponent
|
115
|
+
attributes {
|
116
|
+
base {
|
117
|
+
slot :card, id: ->{"card_#{id}"}, class: %w[rounded-md border]
|
118
|
+
slot :header, class: %w[bg-gray-100 border-b]
|
119
|
+
slot :body, class: %w[]
|
120
|
+
slot :footer, class: %w[border-t]
|
121
|
+
}
|
122
|
+
|
123
|
+
variant(:color) {
|
124
|
+
primary {
|
125
|
+
slot :header, class: { append: %w[bg-blue-500] }
|
126
|
+
slot :footer, class: { append: %w[bg-blue-500] }
|
127
|
+
}
|
128
|
+
danger {
|
129
|
+
slot :header, class: { append: %w[bg-red-500] }
|
130
|
+
slot :footer, class: { append: %w[bg-red-500] }
|
131
|
+
}
|
132
|
+
}
|
133
|
+
|
134
|
+
# Wildcard applies to all slots
|
135
|
+
variant(:padding) {
|
136
|
+
default {
|
137
|
+
slot *, class: { append: %w[p-4] }
|
138
|
+
}
|
139
|
+
condensed {
|
140
|
+
slot *, class: { append: %w[px-4 py-2] }
|
141
|
+
}
|
142
|
+
spacious {
|
143
|
+
slot *, class: { append: %w[p-6] }
|
144
|
+
}
|
145
|
+
}
|
146
|
+
|
147
|
+
default padding: :default
|
148
|
+
}
|
149
|
+
end
|
150
|
+
```
|
151
|
+
|
152
|
+
Use it in your html
|
153
|
+
```erb
|
154
|
+
<div <%= attribs(slot: :card) %>>
|
155
|
+
<div <%= attribs(slot: :header) %>> Header </div>
|
156
|
+
<div <%= attribs(slot: :body) %>> Body </div>
|
157
|
+
<div <%= attribs(slot: :footer) %>> Footer </div>
|
158
|
+
</div>
|
159
|
+
```
|
160
|
+
|
161
|
+
Override attributes like this:
|
162
|
+
```ruby
|
163
|
+
Card.new(color: :primary, footer: { id: "footer_id" },
|
164
|
+
card: { class: { append: "border-3" }})
|
165
|
+
```
|
166
|
+
|
167
|
+
## Child components
|
168
|
+
We can define variants of a child component inside a parent
|
169
|
+
|
170
|
+
```ruby
|
171
|
+
class Alert < BaseComponent
|
172
|
+
attributes {
|
173
|
+
base {
|
174
|
+
slot :main, class: %w[rounded-md border]
|
175
|
+
slot :button, size: :md
|
176
|
+
}
|
177
|
+
variant(:color) {
|
178
|
+
primary {
|
179
|
+
slot :main, class: { append: %w[bg-blue-100] }
|
180
|
+
slot :button, color: :primary
|
181
|
+
}
|
182
|
+
danger {
|
183
|
+
slot :main, class: { append: %w[bg-red-100] }
|
184
|
+
slot :button, color: :danger, size: :lg, id: "alert_button_1"
|
185
|
+
}
|
186
|
+
}
|
187
|
+
}
|
188
|
+
```
|
189
|
+
|
190
|
+
|
191
|
+
```erb
|
192
|
+
<div <%= attribs(slot: :main) %>>
|
193
|
+
Hello World
|
194
|
+
<%= render Button.new(**attribs(slot: :button)) %>
|
195
|
+
</div>
|
196
|
+
```
|
197
|
+
**Note:** You can override like this as well
|
198
|
+
```erb
|
199
|
+
<div <%= attribs(slot: :main, class: { append: "h-10" }) %>>
|
200
|
+
Hello World
|
201
|
+
<%= render Button.new(**attribs(slot: :button, size: :xl)) %>
|
202
|
+
</div>
|
203
|
+
```
|
204
|
+
|
205
|
+
|
206
|
+
## Acknowledgements
|
207
|
+
Greatly inspired by Tailwind Variants, ViewComponentContrib::StyleVariants, and what GitHub is doing with Primer::Classify.
|
208
|
+
|
209
|
+
## License
|
210
|
+
|
211
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
212
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Attrify
|
4
|
+
module DSL
|
5
|
+
class Base
|
6
|
+
def initialize
|
7
|
+
@base = {}
|
8
|
+
end
|
9
|
+
|
10
|
+
def build(&)
|
11
|
+
instance_eval(&)
|
12
|
+
@base
|
13
|
+
end
|
14
|
+
|
15
|
+
def slot(name, attributes = nil, &block)
|
16
|
+
if block
|
17
|
+
# If a block is provided, we need to create a nested structure
|
18
|
+
@base[name] = self.class.new.build(&block) # Recursively build nested slots
|
19
|
+
elsif attributes.is_a?(Hash)
|
20
|
+
# If no block, but attributes are provided, add them directly
|
21
|
+
@base[name] = attributes
|
22
|
+
else
|
23
|
+
raise ArgumentError, "Either attributes hash or a block is required"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "nested_variant"
|
4
|
+
|
5
|
+
module Attrify
|
6
|
+
module DSL
|
7
|
+
class Compound
|
8
|
+
def initialize
|
9
|
+
@compounds = {}
|
10
|
+
end
|
11
|
+
|
12
|
+
def build(&)
|
13
|
+
# Capture the result of the block execution
|
14
|
+
result = instance_eval(&)
|
15
|
+
|
16
|
+
# If the result is a hash and no slots were explicitly set, treat it as compound attributes
|
17
|
+
if result.is_a?(Hash) && @compounds.empty?
|
18
|
+
@compounds.merge!(result)
|
19
|
+
end
|
20
|
+
|
21
|
+
@compounds
|
22
|
+
end
|
23
|
+
|
24
|
+
# Explicit slot handling
|
25
|
+
def slot(name, attributes)
|
26
|
+
@compounds[name] = attributes
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "base"
|
4
|
+
require_relative "variant"
|
5
|
+
require_relative "compound"
|
6
|
+
|
7
|
+
module Attrify
|
8
|
+
module DSL
|
9
|
+
class Engine
|
10
|
+
attr_reader :base_attr, :variants, :defaults, :compounds
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
@base_attr = {}
|
14
|
+
@variants = {}
|
15
|
+
@defaults = {}
|
16
|
+
@compounds = []
|
17
|
+
end
|
18
|
+
|
19
|
+
def build(&)
|
20
|
+
instance_eval(&)
|
21
|
+
self
|
22
|
+
end
|
23
|
+
|
24
|
+
def base(input = nil, &block)
|
25
|
+
if block
|
26
|
+
@base_attr = Base.new.build(&block)
|
27
|
+
elsif input.is_a?(Hash)
|
28
|
+
@base_attr = input
|
29
|
+
else
|
30
|
+
raise ArgumentError, "Expected a block or a hash"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def variant(name, &)
|
35
|
+
@variants[name] = Variant.new.build(&)
|
36
|
+
end
|
37
|
+
|
38
|
+
def compound(variants, &)
|
39
|
+
@compounds.concat([{variants: variants, attributes: Compound.new.build(&)}])
|
40
|
+
end
|
41
|
+
|
42
|
+
def default(defaults)
|
43
|
+
@defaults = defaults
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Attrify
|
4
|
+
module DSL
|
5
|
+
class NestedVariant
|
6
|
+
def initialize
|
7
|
+
@variants = {}
|
8
|
+
end
|
9
|
+
|
10
|
+
def build(&)
|
11
|
+
instance_eval(&)
|
12
|
+
@variants
|
13
|
+
end
|
14
|
+
|
15
|
+
def slot(name, attributes)
|
16
|
+
@variants[name] = attributes
|
17
|
+
end
|
18
|
+
|
19
|
+
def respond_to_missing?(name, include_private = false)
|
20
|
+
true
|
21
|
+
end
|
22
|
+
|
23
|
+
def method_missing(name, *args, &block)
|
24
|
+
# Handle the case where the slot is provided as a block
|
25
|
+
if block
|
26
|
+
@variants[name] = instance_eval(&block)
|
27
|
+
# Handle the case where the slot is provided directly as a hash
|
28
|
+
elsif args.length == 1 && args[0].is_a?(Hash)
|
29
|
+
@variants[name] = args[0]
|
30
|
+
else
|
31
|
+
super
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "nested_variant"
|
4
|
+
|
5
|
+
module Attrify
|
6
|
+
module DSL
|
7
|
+
class Variant
|
8
|
+
def initialize
|
9
|
+
@variants = {}
|
10
|
+
end
|
11
|
+
|
12
|
+
def build(&)
|
13
|
+
instance_eval(&)
|
14
|
+
@variants
|
15
|
+
end
|
16
|
+
|
17
|
+
def respond_to_missing?(name, include_private = false)
|
18
|
+
true
|
19
|
+
end
|
20
|
+
|
21
|
+
def method_missing(name, *args, &block)
|
22
|
+
# Handle the case where the variant is provided as a block
|
23
|
+
if block
|
24
|
+
@variants[name] = NestedVariant.new.build(&block)
|
25
|
+
# Handle the case where the variant is provided directly as a hash
|
26
|
+
elsif args.length == 1 && args[0].is_a?(Hash)
|
27
|
+
@variants[name] = args[0]
|
28
|
+
else
|
29
|
+
super
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|