class_variants 0.0.5 → 0.0.7

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: ff3460025f84ed33be2203bd2340bfa5769342778ff2d8b8656b28ff05dd858f
4
- data.tar.gz: 05d3075c1c174ba4140f40a5755b4972160708ca9afc5866207a7d222802efc2
3
+ metadata.gz: 765533378fe25d4d376f7b79d3071f056438f7c94ddddcdd45043d54669c1859
4
+ data.tar.gz: d05e57bd9f860e806687b52ee20faeffb59b2774904ec6fb84f3403bdff7d692
5
5
  SHA512:
6
- metadata.gz: 7719a590c8afc043a1e3538c4e0c91d38f0d968951f555ed782a0254a7b539573ba0ba4034d91f50507cfba331d37989f8dee3d47aa97fbab2f7508337b64c3d
7
- data.tar.gz: 553a22135fb34a7b853da679fb0cc8db3e68c2394ad13a0e8fd8a4c8a60fcabfef4aad6d62aa52e3e1d3f98da6bd2772b240be8fc8dd5fcdbc71c73f14488cfe
6
+ metadata.gz: e7517aed893faec0040d34907290f335135631fd12268359687dbcf411b4cbe1cc206ed9a1bffd34ad667bd3e43166300efa9fae7f81ae44c5bf129d6587805e
7
+ data.tar.gz: c1bb443cc535bd4eec83ebcc6880a851ab8c3bd89342406f904c83136006a082f60092111a20d05273b014639342ca8fb3faa9e67cb6a326e351411e18e57522
@@ -3,37 +3,51 @@ class ClassVariants::Instance
3
3
  attr_reader :variants
4
4
  attr_reader :defaults
5
5
 
6
- def initialize(classes = "", variants: {}, defaults: {})
6
+ def initialize(classes = "", variants: {}, compoundVariants: [], defaults: {})
7
7
  @classes = classes
8
- @variants = variants
8
+ @variants = expand_boolean_variants(variants)
9
+ @compoundVariants = compoundVariants
9
10
  @defaults = defaults
10
11
  end
11
12
 
12
- def render(**settings)
13
- result = []
13
+ def render(**overrides)
14
+ # Start with our default classes
15
+ result = [@classes]
14
16
 
15
- # Add the default classes if any provided
16
- result << classes if classes
17
+ # Then merge the passed in overrides on top of the defaults
18
+ selected = @defaults.merge(overrides)
17
19
 
18
- # Keep the applied variants so we can later apply the defaults
19
- applied_options = []
20
-
21
- # Go through each keys provided
22
- settings.each do |key, value|
23
- if variants.keys.include? key
24
- applied_options << key
25
- result << variants[key][value]
26
- end
20
+ selected.each do |variant_type, variant|
21
+ # dig the classes out and add them to the result
22
+ result << @variants.dig(variant_type, variant)
27
23
  end
28
24
 
29
- if defaults.present?
30
- defaults.each do |key, key_to_use|
31
- unless applied_options.include? key
32
- result << @variants[key][key_to_use] if @variants[key].present?
33
- end
25
+ @compoundVariants.each do |compound_variant|
26
+ if (compound_variant.keys - [:class]).all? { |key| selected[key] == compound_variant[key] }
27
+ result << compound_variant[:class]
34
28
  end
35
29
  end
36
30
 
31
+ # Compact out any nil values we may have dug up
32
+ result.compact!
33
+
34
+ # Return the final token list
37
35
  result.join " "
38
36
  end
39
- end
37
+
38
+ private
39
+
40
+ def expand_boolean_variants(variants)
41
+ variants.each.map { |key, value|
42
+ case value
43
+ when String
44
+ s_key = key.to_s
45
+ { s_key.delete_prefix("!").to_sym => { !s_key.start_with?("!") => value } }
46
+ else
47
+ { key => value }
48
+ end
49
+ }.reduce do |variants, more_variants|
50
+ variants.merge!(more_variants) { |_key, v1, v2| v1.merge!(v2) }
51
+ end
52
+ end
53
+ end
@@ -1,3 +1,3 @@
1
1
  module ClassVariants
2
- VERSION = "0.0.5" unless const_defined?(:VERSION)
2
+ VERSION = "0.0.7" unless const_defined?(:VERSION)
3
3
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: class_variants
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adrian Marin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-10-27 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2023-12-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: benchmark-ips
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  description: Easily configure styles and apply them as classes.
14
28
  email: adrian@adrianthedev.com
15
29
  executables: []
@@ -43,7 +57,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
43
57
  - !ruby/object:Gem::Version
44
58
  version: '0'
45
59
  requirements: []
46
- rubygems_version: 3.3.3
60
+ rubygems_version: 3.4.6
47
61
  signing_key:
48
62
  specification_version: 4
49
63
  summary: Easily configure styles and apply them as classes.