Class: Vident::Stimulus::ClassMap

Inherits:
Base
  • Object
show all
Defined in:
lib/vident/stimulus/class_map.rb

Overview

‘data-<ctrl>-<name>-class` fragment — a named CSS class set readable on the JS side as `this.<name>Class`.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

to_data_hash

Methods included from Combinable

#deconstruct, #deconstruct_keys, #with

Class Method Details

.normalize_css(input) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/vident/stimulus/class_map.rb', line 36

def self.normalize_css(input)
  case input
  when String
    input.split(/\s+/).reject(&:empty?).join(" ")
  when Array
    input.map(&:to_s).reject(&:empty?).join(" ")
  else
    raise ::Vident::ParseError, "ClassMap.parse: css must be a String or Array, got #{input.class}"
  end
end

.parse(*args, implied:, component_id: nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/vident/stimulus/class_map.rb', line 17

def self.parse(*args, implied:, component_id: nil)
  case args
  in [Symbol => name_sym, css_input]
    new(
      controller: implied,
      name: name_sym.to_s.dasherize,
      css: normalize_css(css_input)
    )
  in [String => ctrl_path, Symbol => name_sym, css_input]
    new(
      controller: Controller.parse(ctrl_path, implied: implied),
      name: name_sym.to_s.dasherize,
      css: normalize_css(css_input)
    )
  else
    raise ::Vident::ParseError, "ClassMap.parse: invalid arguments #{args.inspect}"
  end
end

Instance Method Details

#data_attribute_keyObject



49
# File 'lib/vident/stimulus/class_map.rb', line 49

def data_attribute_key = :"#{controller.name}-#{name}-class"

#to_data_pairObject



51
# File 'lib/vident/stimulus/class_map.rb', line 51

def to_data_pair = [data_attribute_key, css]

#to_hObject Also known as: to_hash



53
# File 'lib/vident/stimulus/class_map.rb', line 53

def to_h = {data_attribute_key => css}

#to_sObject



47
# File 'lib/vident/stimulus/class_map.rb', line 47

def to_s = css