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
- #build_variants(&block) ⇒ Object
- #compile(**variants) ⇒ Object
- #compound(**variants, &block) ⇒ Object
- #defaults(&block) ⇒ Object
- #dup ⇒ Object
- #find_parent_variants ⇒ Object
- #handle_variants(variants, strategy) ⇒ Object
-
#initialize(&init_block) ⇒ StyleSet
constructor
A new instance of StyleSet.
- #variants(strategy: :override, &block) ⇒ Object
Constructor Details
#initialize(&init_block) ⇒ StyleSet
Returns a new instance of StyleSet.
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/view_component_contrib/style_variants.rb', line 74 def initialize(&init_block) @base_block = nil @defaults = {} @variants = {} @compounds = {} return unless init_block @init_block = init_block instance_eval(&init_block) end |
Instance Method Details
#base(&block) ⇒ Object
86 87 88 |
# File 'lib/view_component_contrib/style_variants.rb', line 86 def base(&block) @base_block = block end |
#build_variants(&block) ⇒ Object
99 100 101 |
# File 'lib/view_component_contrib/style_variants.rb', line 99 def build_variants(&block) VariantBuilder.new(true).build(&block) end |
#compile(**variants) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/view_component_contrib/style_variants.rb', line 128 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.concat(Array(config[:class])) acc.concat(Array(config[:class_name])) acc end |
#compound(**variants, &block) ⇒ Object
124 125 126 |
# File 'lib/view_component_contrib/style_variants.rb', line 124 def compound(**variants, &block) @compounds[variants] = block end |
#defaults(&block) ⇒ Object
90 91 92 |
# File 'lib/view_component_contrib/style_variants.rb', line 90 def defaults(&block) @defaults = block.call.freeze end |
#dup ⇒ Object
152 153 154 155 156 157 158 |
# File 'lib/view_component_contrib/style_variants.rb', line 152 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 |
#find_parent_variants ⇒ Object
114 115 116 117 118 119 120 121 122 |
# File 'lib/view_component_contrib/style_variants.rb', line 114 def find_parent_variants parent_component = @init_block.binding.receiver.superclass return unless parent_component.respond_to?(:style_config) parent_config = parent_component.style_config default_parent_style = parent_component.default_style_name parent_style_set = parent_config.instance_variable_get(:@styles)[default_parent_style.to_sym] parent_style_set.instance_variable_get(:@variants).deep_dup end |
#handle_variants(variants, strategy) ⇒ Object
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/view_component_contrib/style_variants.rb', line 103 def handle_variants(variants, strategy) return variants if strategy == :override parent_variants = find_parent_variants return variants unless parent_variants return parent_variants.deep_merge(variants) if strategy == :merge parent_variants.merge(variants) if strategy == :extend end |
#variants(strategy: :override, &block) ⇒ Object
94 95 96 97 |
# File 'lib/view_component_contrib/style_variants.rb', line 94 def variants(strategy: :override, &block) variants = build_variants(&block) @variants = handle_variants(variants, strategy) end |