Class: ViewComponentContrib::StyleVariants::StyleSet
- Inherits:
-
Object
- Object
- ViewComponentContrib::StyleVariants::StyleSet
- Defined in:
- lib/view_component_contrib/style_variants.rb
Instance Method Summary collapse
- #base(&block) ⇒ Object
- #compile(**variants) ⇒ Object
- #compound(**variants, &block) ⇒ Object
- #defaults(&block) ⇒ Object
- #dup ⇒ Object
-
#initialize(&init_block) ⇒ StyleSet
constructor
A new instance of StyleSet.
- #variants(&block) ⇒ Object
Constructor Details
#initialize(&init_block) ⇒ StyleSet
Returns a new instance of StyleSet.
74 75 76 77 78 79 80 81 |
# File 'lib/view_component_contrib/style_variants.rb', line 74 def initialize(&init_block) @base_block = nil @defaults = {} @variants = {} @compounds = {} instance_eval(&init_block) if init_block end |
Instance Method Details
#base(&block) ⇒ Object
83 84 85 |
# File 'lib/view_component_contrib/style_variants.rb', line 83 def base(&block) @base_block = block end |
#compile(**variants) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/view_component_contrib/style_variants.rb', line 99 def compile(**variants) acc = Array(@base_block&.call || []) config = @defaults.merge(variants.compact) config.each do |variant, value| value = cast_value(value) variant = @variants.dig(variant, value) || next styles = variant.is_a?(::Proc) ? variant.call(**config) : variant acc.concat(Array(styles)) end @compounds.each do |compound, value| next unless compound.all? { |k, v| config[k] == v } styles = value.is_a?(::Proc) ? value.call(**config) : value acc.concat(Array(styles)) end acc end |
#compound(**variants, &block) ⇒ Object
95 96 97 |
# File 'lib/view_component_contrib/style_variants.rb', line 95 def compound(**variants, &block) @compounds[variants] = block end |
#defaults(&block) ⇒ Object
87 88 89 |
# File 'lib/view_component_contrib/style_variants.rb', line 87 def defaults(&block) @defaults = block.call.freeze end |
#dup ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/view_component_contrib/style_variants.rb', line 121 def dup copy = super copy.instance_variable_set(:@defaults, @defaults.dup) copy.instance_variable_set(:@variants, @variants.dup) copy.instance_variable_set(:@compounds, @compounds.dup) copy end |
#variants(&block) ⇒ Object
91 92 93 |
# File 'lib/view_component_contrib/style_variants.rb', line 91 def variants(&block) @variants = VariantBuilder.new(true).build(&block) end |