Class: ViewComponentContrib::StyleVariants::StyleConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/view_component_contrib/style_variants.rb

Overview

:nodoc:

Constant Summary collapse

DEFAULT_POST_PROCESSOR =
->(compiled) { compiled.join(" ") }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStyleConfig

Returns a new instance of StyleConfig.



146
147
148
149
# File 'lib/view_component_contrib/style_variants.rb', line 146

def initialize
  @styles = {}
  @postprocessor = DEFAULT_POST_PROCESSOR
end

Instance Attribute Details

#postprocessorObject (readonly)

Returns the value of attribute postprocessor.



144
145
146
# File 'lib/view_component_contrib/style_variants.rb', line 144

def postprocessor
  @postprocessor
end

Instance Method Details

#compile(name, **variants) ⇒ Object



155
156
157
158
159
160
161
# File 'lib/view_component_contrib/style_variants.rb', line 155

def compile(name, **variants)
  styles[name]&.compile(**variants).then do |compiled|
    next unless compiled

    postprocess(compiled)
  end
end

#define(name, &block) ⇒ Object



151
152
153
# File 'lib/view_component_contrib/style_variants.rb', line 151

def define(name, &block)
  styles[name] = StyleSet.new(&block)
end

#dupObject



168
169
170
171
172
# File 'lib/view_component_contrib/style_variants.rb', line 168

def dup
  copy = super
  copy.instance_variable_set(:@styles, @styles.dup)
  copy
end

#postprocess_with(callable = nil, &block) ⇒ Object

Allow defining a custom postprocessor



164
165
166
# File 'lib/view_component_contrib/style_variants.rb', line 164

def postprocess_with(callable = nil, &block)
  @postprocessor = callable || block
end