Class: Vident2::Stimulus::Outlet

Inherits:
Literal::Data
  • Object
show all
Defined in:
lib/vident2/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.

Auto-selector: ‘“#<component_id> [data-controller~=<outlet>]”`. If `component_id` is nil (host id not yet known) the `#<id>` prefix is omitted — caller must backfill if needed.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.auto_selector(outlet_identifier, component_id:) ⇒ Object



82
83
84
85
# File 'lib/vident2/stimulus/outlet.rb', line 82

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.



92
93
94
# File 'lib/vident2/stimulus/outlet.rb', line 92

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

‘.parse(*args, implied:, component_id:)` grammar mirrors v1:

(Symbol)              -> outlet name on implied, auto-selector
(String)              -> outlet identifier, auto-selector
(Array[name, sel])    -> explicit [name, selector] pair
(Symbol|String, String) -> (name, explicit selector)
(String, Symbol, String) -> (ctrl_path, name, selector)
(<component instance>) -> grab stimulus_identifier and build auto-selector


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
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/vident2/stimulus/outlet.rb', line 27

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(str, 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
    )
  else
    component_like = args.size == 1 ? args[0] : nil
    if component_like && component_like.respond_to?(:stimulus_identifier)
      ident = component_like.stimulus_identifier
      new(
        controller: implied,
        name: ident,
        selector: auto_selector(ident, component_id: component_id)
      )
    else
      raise ::Vident2::ParseError, "Outlet.parse: invalid arguments #{args.inspect}"
    end
  end
end

.to_data_hash(outlets) ⇒ Object



105
106
107
108
109
110
# File 'lib/vident2/stimulus/outlet.rb', line 105

def self.to_data_hash(outlets)
  outlets.each_with_object({}) do |o, acc|
    key, sel = o.to_data_pair
    acc[key] = sel
  end
end

Instance Method Details

#data_attribute_keyObject



98
# File 'lib/vident2/stimulus/outlet.rb', line 98

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

#to_data_pairObject



100
# File 'lib/vident2/stimulus/outlet.rb', line 100

def to_data_pair = [data_attribute_key, selector]

#to_hObject Also known as: to_hash



102
# File 'lib/vident2/stimulus/outlet.rb', line 102

def to_h = {data_attribute_key => selector}

#to_sObject



96
# File 'lib/vident2/stimulus/outlet.rb', line 96

def to_s = selector