Class: StimulusTagHelper::StimulusAction

Inherits:
Object
  • Object
show all
Defined in:
lib/stimulus_tag_helper/stimulus_action.rb

Constant Summary collapse

STANDARD_PARSER =
/^(?<void>(?<event>.+?)(?<void>@(?<target>window|document))?->)?(?<identifier>.+?)(?<void>#(?<method>[^:]+?))(?<void>:(?<options>.+))?$/
NO_CONTROLLER_PARSER =
/^(?<void>(?<event>.+?)?(?<void>@(?<target>window|document))?->)?(?<method>[^#:]+?)(?<void>:(?<options>.+))?$/
PARTS =
i[identifier method event target options]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier:, method:, event: nil, target: nil, options: nil) ⇒ StimulusAction

event is nil to let stimulus.js decide default event for the element



12
13
14
15
16
# File 'lib/stimulus_tag_helper/stimulus_action.rb', line 12

def initialize(identifier:, method:, event: nil, target: nil, options: nil)
  PARTS.each do |part|
    instance_variable_set(:"@#{part}", binding.local_variable_get(part))
  end
end

Class Method Details

.parse(string, **defaults) ⇒ Object

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
# File 'lib/stimulus_tag_helper/stimulus_action.rb', line 39

def parse(string, **defaults)
  to_parse = string.to_s
  parsed =
    to_parse.include?("#") ? STANDARD_PARSER.match(to_parse) : NO_CONTROLLER_PARSER.match(to_parse)
  raise(ArgumentError, "Can't parse stimulus action #{string.inspect}") unless parsed

  new(**defaults.merge(parsed.named_captures.except("void").transform_keys(&:to_sym)))
end

Instance Method Details

#==(other) ⇒ Object



22
23
24
# File 'lib/stimulus_tag_helper/stimulus_action.rb', line 22

def ==(other)
  other.is_a?(StimulusAction) && other.to_s == to_s
end

#to_sObject



18
19
20
# File 'lib/stimulus_tag_helper/stimulus_action.rb', line 18

def to_s
  "#{event_part.presence&.+("->")}#{handler_part}"
end