Class: StimulusAttrs

Inherits:
HtmlAttrs
  • Object
show all
Defined in:
lib/stimulus_attrs.rb

Overview

rubocop:disable Style/Documentation

Defined Under Namespace

Modules: DefaultIdentifier, Helpers Classes: IdentifierScope

Constant Summary collapse

VERSION =
'0.1.0'

Class Method Summary collapse

Class Method Details

.actions(descriptor = nil, identifier: nil, params: nil, **descriptors) ⇒ Object

‘descriptor` is what stimulus calls things that go inside data-action: stimulus.hotwired.dev/reference/actions#descriptors



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/stimulus_attrs.rb', line 46

def self.actions(descriptor = nil, identifier: nil, params: nil, **descriptors)
  if descriptor
    # Keep the default descriptors near the start of the action list
    descriptors = { nil => [descriptor] + Array(descriptors.delete(nil)) }.merge(descriptors)
  end

  action = descriptors.map do |event, methods|
    Array(methods).map do |method|
      method = method.to_s
      prepend_identifier = !method.include?('#')
      raise ArgumentError, 'identifier is required' if prepend_identifier && !identifier

      method = method.to_s.underscore.camelcase(:lower) if prepend_identifier
      method = "#{identifier}##{method}" if prepend_identifier
      event ? "#{event}->#{method}" : method
    end
  end.flatten.join(' ')

  result = new(data: { action: action })
  result = result.merge(self.params(**params, identifier: identifier)) if params.present?
  result
end

.classes(identifier:, **classes) ⇒ Object



41
42
43
# File 'lib/stimulus_attrs.rb', line 41

def self.classes(identifier:, **classes)
  new(data: classes.transform_keys { |k| "#{identifier}-#{k.to_s.underscore.dasherize}-class" })
end

.controller(identifier_as_arg = nil, values: nil, classes: nil, actions: nil, params: nil, outlets: nil, identifier: nil) ⇒ Object

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/stimulus_attrs.rb', line 18

def self.controller(identifier_as_arg = nil, values: nil, classes: nil, actions: nil, params: nil, outlets: nil, identifier: nil)
  identifier ||= identifier_as_arg
  raise ArgumentError, 'identifier is required' unless identifier
  controller_referenced(identifier)

  result = new(data: { controller: identifier })
  result = result.merge(self.values(**values, identifier: identifier)) if values
  result = result.merge(self.classes(**classes, identifier: identifier)) if classes
  result = result.merge(self.actions(**actions, identifier: identifier)) if actions
  result = result.merge(self.params(**params, identifier: identifier)) if params
  result = result.merge(self.outlets(**outlets, identifier: identifier)) if outlets

  result
end

.controller_referenced(identifier) ⇒ Object



83
84
85
86
# File 'lib/stimulus_attrs.rb', line 83

def self.controller_referenced(identifier)
  return unless instrumentation_enabled?
  ActiveSupport::Notifications.instrument('controller_referenced.stimulus_attrs', identifier: identifier)
end

.instrumentation_enabled=(value) ⇒ Object



92
93
94
# File 'lib/stimulus_attrs.rb', line 92

def self.instrumentation_enabled=(value)
  @instrumentation_enabled = value
end

.instrumentation_enabled?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/stimulus_attrs.rb', line 88

def self.instrumentation_enabled?
  !!@instrumentation_enabled
end

.outlets(identifier:, **outlets) ⇒ Object



73
74
75
# File 'lib/stimulus_attrs.rb', line 73

def self.outlets(identifier:, **outlets)
  new(data: outlets.transform_keys { |k| "#{identifier}-#{k.to_s.underscore.dasherize}-outlet" })
end

.params(identifier:, **params) ⇒ Object



69
70
71
# File 'lib/stimulus_attrs.rb', line 69

def self.params(identifier:, **params)
  new(data: params.transform_keys { |k| "#{identifier}-#{k.to_s.underscore.dasherize}-param" })
end

.target(target_name, identifier:) ⇒ Object



33
34
35
# File 'lib/stimulus_attrs.rb', line 33

def self.target(target_name, identifier:)
  new(data: { "#{identifier}-target" => target_name.to_s.underscore.camelcase(:lower) })
end

.values(identifier:, **values) ⇒ Object



37
38
39
# File 'lib/stimulus_attrs.rb', line 37

def self.values(identifier:, **values)
  new(data: values.transform_keys { |k| "#{identifier}-#{k.to_s.underscore.dasherize}-value" })
end

.with_identifier(identifier) {|scope| ... } ⇒ Object

Yields:

  • (scope)


77
78
79
80
81
# File 'lib/stimulus_attrs.rb', line 77

def self.with_identifier(identifier, &block)
  scope = IdentifierScope.new(identifier)
  yield scope if block
  scope
end