Class: Vident::Stimulus::Outlet

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

Overview

‘data-<ctrl>-<name>-outlet` fragment. `selector` is the CSS selector the Stimulus runtime uses to resolve the outlet on the page.

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

.auto_selector(outlet_identifier, component_id:) ⇒ Object



69
70
71
72
# File 'lib/vident/stimulus/outlet.rb', line 69

def self.auto_selector(outlet_identifier, component_id:)
  prefix = component_id ? "##{css_escape_ident(component_id)} " : ""
  "#{prefix}[data-controller~=#{outlet_identifier}]"
end

.css_escape_ident(id) ⇒ Object

CSS-escapes anything outside the bare identifier alphabet (‘A-Za-z0-9_-`) using the `HH ` hex form (with trailing space delimiter). Bare `<char>` works for many punctuation cases but not for whitespace, parens, or non-ASCII — the hex form is always valid per CSS Syntax §4.3.7.



79
80
81
# File 'lib/vident/stimulus/outlet.rb', line 79

def self.css_escape_ident(id)
  id.to_s.gsub(/[^A-Za-z0-9_-]/) { |c| "\\#{c.ord.to_s(16)} " }
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/vident/stimulus/outlet.rb', line 17

def self.parse(*args, implied:, component_id: nil)
  case args
  in [Symbol => sym]
    name = sym.to_s.dasherize
    new(
      controller: implied,
      name: name,
      selector: auto_selector(name, component_id: component_id)
    )
  in [String => str]
    name = str.dasherize
    new(
      controller: implied,
      name: name,
      selector: auto_selector(name, component_id: component_id)
    )
  in [[identifier, selector]]
    new(
      controller: implied,
      name: identifier.to_s.dasherize,
      selector: selector
    )
  in [Symbol => sym, String => sel]
    new(
      controller: implied,
      name: sym.to_s.dasherize,
      selector: sel
    )
  in [String => id_or_name, String => sel]
    new(
      controller: implied,
      name: id_or_name.dasherize,
      selector: sel
    )
  in [String => ctrl_path, Symbol => sym, String => sel]
    new(
      controller: Controller.parse(ctrl_path, implied: implied),
      name: sym.to_s.dasherize,
      selector: sel
    )
  in [obj] if obj.respond_to?(:stimulus_identifier)
    ident = obj.stimulus_identifier
    new(
      controller: implied,
      name: ident,
      selector: auto_selector(ident, component_id: component_id)
    )
  else
    raise ::Vident::ParseError, "Outlet.parse: invalid arguments #{args.inspect}"
  end
end

Instance Method Details

#data_attribute_keyObject



85
# File 'lib/vident/stimulus/outlet.rb', line 85

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

#to_data_pairObject



87
# File 'lib/vident/stimulus/outlet.rb', line 87

def to_data_pair = [data_attribute_key, selector]

#to_hObject Also known as: to_hash



89
# File 'lib/vident/stimulus/outlet.rb', line 89

def to_h = {data_attribute_key => selector}

#to_sObject



83
# File 'lib/vident/stimulus/outlet.rb', line 83

def to_s = selector