Class: Vident::Stimulus::Action

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

Overview

‘data-action` fragment: single action descriptor like `“click->admin–users#handleClick”`.

Constant Summary collapse

VALID_OPTIONS =
%i[once prevent stop passive !passive capture self].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Combinable

#deconstruct, #deconstruct_keys, #with

Class Method Details

.from_descriptor(h, implied:) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/vident/stimulus/action.rb', line 90

def self.from_descriptor(h, implied:)
  invalid_options = Array(h[:options]) - VALID_OPTIONS
  unless invalid_options.empty?
    raise ::Vident::ParseError,
      "Action.parse: invalid option(s) #{invalid_options.inspect}. Valid: #{VALID_OPTIONS.inspect}"
  end

  method_raw = h.fetch(:method)
  method_name = method_raw.is_a?(Symbol) ? Naming.js_name(method_raw) : method_raw.to_s
  controller = h[:controller] ? Controller.parse(h[:controller], implied: implied) : implied
  new(
    controller: controller,
    method_name: method_name,
    event: h[:event]&.to_s,
    modifiers: Array(h[:options]),
    keyboard: h[:keyboard],
    window: h.fetch(:window, false)
  )
end

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



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
# File 'lib/vident/stimulus/action.rb', line 23

def self.parse(*args, implied:, component_id: nil)
  case args
  in [Action => a]
    a
  in [Symbol => event, Action => a]
    a.with(event: event.to_s)
  in [Hash => h]
    from_descriptor(h, implied: implied)
  in [Symbol => method_sym]
    new(
      controller: implied,
      method_name: Naming.js_name(method_sym),
      event: nil
    )
  in [Symbol => event, Symbol => method_sym]
    new(
      controller: implied,
      method_name: Naming.js_name(method_sym),
      event: event.to_s
    )
  in [String => ctrl_path, Symbol => method_sym]
    new(
      controller: Controller.parse(ctrl_path, implied: implied),
      method_name: Naming.js_name(method_sym),
      event: nil
    )
  in [Symbol => event, String => ctrl_path, Symbol => method_sym]
    new(
      controller: Controller.parse(ctrl_path, implied: implied),
      method_name: Naming.js_name(method_sym),
      event: event.to_s
    )
  in [String => s]
    raise ::Vident::ParseError,
      "Action.parse: a bare String is a controller path, not a fully-qualified action descriptor. " \
      "For event/controller/method use structured args like `:click, \"path/ctrl\", :method` " \
      "or the Hash descriptor form. To parse an existing wire-format string like " \
      "\"click->ctrl#m\", call `Vident::Stimulus::Action.parse_descriptor(#{s.inspect})`."
  else
    raise ::Vident::ParseError, "Action.parse: invalid arguments #{args.inspect}"
  end
end

.parse_descriptor(s) ⇒ Object

Parses a wire-format Stimulus action descriptor (‘“event->ctrl#method”` or `“ctrl#method”`). The controller segment is taken verbatim — not re-stimulized.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/vident/stimulus/action.rb', line 112

def self.parse_descriptor(s)
  if s.include?("->")
    event_part, ctrl_method = s.split("->", 2)
    ctrl, method = ctrl_method.split("#", 2)
    new(
      controller: Controller.new(path: ctrl, name: ctrl),
      method_name: method,
      event: event_part
    )
  else
    ctrl, method = s.split("#", 2)
    new(
      controller: Controller.new(path: ctrl, name: ctrl),
      method_name: method,
      event: nil
    )
  end
end

.to_data_hash(actions) ⇒ Object



85
86
87
88
# File 'lib/vident/stimulus/action.rb', line 85

def self.to_data_hash(actions)
  return {} if actions.empty?
  {action: actions.map(&:to_s).join(" ")}
end

Instance Method Details

#to_data_pairObject



80
# File 'lib/vident/stimulus/action.rb', line 80

def to_data_pair = [:action, to_s]

#to_hObject Also known as: to_hash



82
# File 'lib/vident/stimulus/action.rb', line 82

def to_h = {action: to_s}

#to_sObject



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/vident/stimulus/action.rb', line 66

def to_s
  head =
    if event
      ev = event.to_s
      ev = "#{ev}.#{keyboard}" if keyboard
      ev = "#{ev}#{modifiers.map { |o| ":#{o}" }.join}" if modifiers.any?
      ev = "#{ev}@window" if window
      "#{ev}->"
    else
      ""
    end
  "#{head}#{controller.name}##{method_name}"
end