class_variants 0.0.4 → 0.0.6

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: 4780d216a018db941e8e96886478a5aa7bd16f5248de9a676f015811928aa638
4
- data.tar.gz: 17a48e550a1e22ebf16e9841e2b10252f4c1026656454ed8cd785879105bc5c6
3
+ metadata.gz: 2012fb19a3b94a62a3278f1256167126ef85e203a967cc20f8cb67d6fc94bfcb
4
+ data.tar.gz: ec7bf120843ba12c34f7bfbef26f5170fac8bd4e7f40d7717c6a478bf0116cbf
5
5
  SHA512:
6
- metadata.gz: cfa942f88c875214541e95ac2e9ce454934641570aaefb96d179120b1feefc27eb6df61f89525802f9f72ab46864364088b843cac78dd48b1d4572f89aa02d60
7
- data.tar.gz: 427590f3b012493e159675cd96f22f7a206f171656fb22727dc0e44c93f831580c89c4ffe960a09ea37f8e75459831f39289d8cd79c4b57c63a5289d11eeb6f8
6
+ metadata.gz: fceccde2ac9e59dad730915436bd9d8fc2193316c8ce494e76227ac6f64de4bbffa448adc06f70f811a3c869c4b767d489ad750aff41e942ecc4afdc3545b4cd
7
+ data.tar.gz: b881d68c49c2e9cccea155e021aafcaad68ec2df3d48c9c78afac833b96e8c0cc325c174bf34660733ed64570501477351f8b8bf5672290fdd1ed10cf1ab41f3
@@ -5,35 +5,41 @@ class ClassVariants::Instance
5
5
 
6
6
  def initialize(classes = "", variants: {}, defaults: {})
7
7
  @classes = classes
8
- @variants = variants
8
+ @variants = expand_boolean_variants(variants)
9
9
  @defaults = defaults
10
10
  end
11
11
 
12
- def render(**settings)
13
- result = []
12
+ def render(**overrides)
13
+ # Start with our default classes
14
+ result = [@classes]
14
15
 
15
- # Add the default classes if any provided
16
- result << classes if classes
16
+ # Then merge the passed in overrides on top of the defaults
17
+ @defaults.merge(overrides)
18
+ .each do |variant_type, variant|
19
+ # dig the classes out and add them to the result
20
+ result << @variants.dig(variant_type, variant)
21
+ end
17
22
 
18
- # Keep the applied variants so we can later apply the defaults
19
- applied_options = []
23
+ # Compact out any nil values we may have dug up
24
+ result.compact!
20
25
 
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
27
- end
26
+ # Return the final token list
27
+ result.join " "
28
+ end
29
+
30
+ private
28
31
 
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
32
+ def expand_boolean_variants(variants)
33
+ variants.each.map { |key, value|
34
+ case value
35
+ when String
36
+ s_key = key.to_s
37
+ { s_key.delete_prefix("!").to_sym => { !s_key.start_with?("!") => value } }
38
+ else
39
+ { key => value }
34
40
  end
41
+ }.reduce do |variants, more_variants|
42
+ variants.merge!(more_variants) { |_key, v1, v2| v1.merge!(v2) }
35
43
  end
36
-
37
- result.join " "
38
44
  end
39
- end
45
+ end
@@ -1,3 +1,3 @@
1
1
  module ClassVariants
2
- VERSION = "0.0.4" unless const_defined?(:VERSION)
2
+ VERSION = "0.0.6" unless const_defined?(:VERSION)
3
3
  end
@@ -4,4 +4,9 @@ require "class_variants/instance"
4
4
  require "class_variants/railtie" if defined?(Rails)
5
5
 
6
6
  module ClassVariants
7
+ class << self
8
+ def build(classes, **args)
9
+ Instance.new classes, **args
10
+ end
11
+ end
7
12
  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.4
4
+ version: 0.0.6
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: 2022-12-21 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: []