Class: Vident::Stimulus::Controller

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

Overview

‘data-controller` fragment.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Combinable

#deconstruct, #deconstruct_keys, #with

Class Method Details

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



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/vident/stimulus/controller.rb', line 15

def self.parse(*args, implied:, as: nil, component_id: nil)
  case args.size
  when 0
    new(path: implied.path, name: implied.name, alias_name: as)
  when 1
    raw = args[0]
    path = raw.to_s
    new(path: path, name: Naming.stimulize_path(path), alias_name: as)
  else
    raise ::Vident::ParseError, "Controller.parse: expected 0 or 1 positional args, got #{args.size}"
  end
end

.to_data_hash(controllers) ⇒ Object



37
38
39
40
41
42
# File 'lib/vident/stimulus/controller.rb', line 37

def self.to_data_hash(controllers)
  return {} if controllers.empty?
  joined = controllers.map(&:name).reject(&:empty?).join(" ")
  return {} if joined.empty?
  {controller: joined}
end

Instance Method Details

#identifierObject



28
# File 'lib/vident/stimulus/controller.rb', line 28

def identifier = name

#to_data_pairObject



32
# File 'lib/vident/stimulus/controller.rb', line 32

def to_data_pair = [:controller, name]

#to_hObject Also known as: to_hash



34
# File 'lib/vident/stimulus/controller.rb', line 34

def to_h = {controller: name}

#to_sObject



30
# File 'lib/vident/stimulus/controller.rb', line 30

def to_s = name