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
27
28
29
30
31
32
# 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]
    if raw.respond_to?(:stimulus_identifier)
      name = raw.stimulus_identifier
      path = raw.respond_to?(:stimulus_identifier_path) ? raw.stimulus_identifier_path : name
      new(path: path, name: name, alias_name: as)
    else
      path = raw.to_s
      new(path: path, name: Naming.stimulize_path(path), alias_name: as)
    end
  else
    raise ::Vident::ParseError, "Controller.parse: expected 0 or 1 positional args, got #{args.size}"
  end
end

.path_arg?(value) ⇒ Boolean

Whether ‘value` is accepted in a cross-controller path slot: a path String, or a Vident component (class or instance) that names itself.

Returns:

  • (Boolean)


36
37
38
# File 'lib/vident/stimulus/controller.rb', line 36

def self.path_arg?(value)
  value.is_a?(String) || value.respond_to?(:stimulus_identifier)
end

.to_data_hash(controllers) ⇒ Object



49
50
51
52
53
54
# File 'lib/vident/stimulus/controller.rb', line 49

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



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

def identifier = name

#to_data_pairObject



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

def to_data_pair = [:controller, name]

#to_hObject Also known as: to_hash



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

def to_h = {controller: name}

#to_sObject



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

def to_s = name