Skip to content

Commit 9c2d1b1

Browse files
committed
Add global definitions of class variants. Close #36.
1 parent d9b81df commit 9c2d1b1

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

lib/class_variants.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,17 @@ def configure(&block)
2020
def build(...)
2121
Instance.new(...)
2222
end
23+
24+
def definitions
25+
@definitions ||= {}
26+
end
27+
28+
def define(name, ...)
29+
definitions[name.to_sym] = Instance.new(...)
30+
end
31+
32+
def for(name)
33+
definitions[name.to_sym]
34+
end
2335
end
2436
end

test/definitions_test.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require "test_helper"
2+
3+
class DefinitionsTest < Minitest::Test
4+
def setup
5+
ClassVariants.define :button do
6+
variant color: :primary, class: "bg-orange-500"
7+
end
8+
9+
ClassVariants.define :link do
10+
variant color: :primary, class: "bg-blue-500"
11+
end
12+
end
13+
14+
def teardown
15+
ClassVariants.instance_variable_set(:@definitions, nil)
16+
end
17+
18+
def test_definitions
19+
assert_equal "bg-orange-500", ClassVariants.for(:button).render(color: :primary)
20+
assert_equal "bg-blue-500", ClassVariants.for(:link).render(color: :primary)
21+
end
22+
end

0 commit comments

Comments
 (0)