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



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/vident/stimulus/action.rb', line 86

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
# 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 [String => s]
    parse_qualified_string(s)
  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
    )
  else
    raise ::Vident::ParseError, "Action.parse: invalid arguments #{args.inspect}"
  end
end

.parse_qualified_string(s) ⇒ Object

Pass-through: the controller segment is NOT re-stimulized.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/vident/stimulus/action.rb', line 107

def self.parse_qualified_string(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



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

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

Instance Method Details

#to_data_pairObject



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

def to_data_pair = [:action, to_s]

#to_hObject Also known as: to_hash



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

def to_h = {action: to_s}

#to_sObject



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/vident/stimulus/action.rb', line 62

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