Class: FoxTail::StimulusController

Inherits:
Object
  • Object
show all
Defined in:
lib/fox_tail/stimulus_controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier, config: nil) ⇒ StimulusController

Returns a new instance of StimulusController.



9
10
11
12
# File 'lib/fox_tail/stimulus_controller.rb', line 9

def initialize(identifier, config: nil)
  @identifier = format_identifier identifier
  @config = config
end

Instance Attribute Details

#identifierObject (readonly)

Returns the value of attribute identifier.



7
8
9
# File 'lib/fox_tail/stimulus_controller.rb', line 7

def identifier
  @identifier
end

Instance Method Details

#action(method_name, event: nil) ⇒ Object



48
49
50
51
# File 'lib/fox_tail/stimulus_controller.rb', line 48

def action(method_name, event: nil)
  action = "#{identifier}##{method_name}"
  (event && event.to_sym != :click) ? "#{event}->#{action}" : action
end

#action_param_key(name, raw: false) ⇒ Object



53
54
55
# File 'lib/fox_tail/stimulus_controller.rb', line 53

def action_param_key(name, raw: false)
  key name, :param, raw: raw
end

#attributes(_options = {}) ⇒ Object



18
19
20
21
22
# File 'lib/fox_tail/stimulus_controller.rb', line 18

def attributes(_options = {})
  {
    data: {controller: identifier}
  }
end

#build_actions(actions) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/fox_tail/stimulus_controller.rb', line 61

def build_actions(actions)
  return nil if actions.blank?

  formatted_actions = actions.each_with_object([]) do |(method_name, events), results|
    Array(events).each { |event| results.push(action(method_name, event: event)) }
  end

  stimulus_merger.merge_actions(*formatted_actions)
end

#classes_key(name, raw: false) ⇒ Object



36
37
38
# File 'lib/fox_tail/stimulus_controller.rb', line 36

def classes_key(name, raw: false)
  key name, :class, raw: raw
end

#configObject



14
15
16
# File 'lib/fox_tail/stimulus_controller.rb', line 14

def config
  @config ||= FoxTail::Config.current
end

#event(event_name) ⇒ Object



57
58
59
# File 'lib/fox_tail/stimulus_controller.rb', line 57

def event(event_name)
  "#{identifier}:#{event_name}"
end

#merge(attributes, options = {}) ⇒ Object



40
41
42
# File 'lib/fox_tail/stimulus_controller.rb', line 40

def merge(attributes, options = {})
  merge! attributes.deep_dup, options
end

#merge!(attributes, options = {}) ⇒ Object



44
45
46
# File 'lib/fox_tail/stimulus_controller.rb', line 44

def merge!(attributes, options = {})
  stimulus_merger.merge_attributes! attributes, self.attributes(options)
end

#outlet_key(name, raw: false) ⇒ Object



28
29
30
# File 'lib/fox_tail/stimulus_controller.rb', line 28

def outlet_key(name, raw: false)
  key name, :outlet, raw: raw
end

#target_key(raw: false) ⇒ Object



24
25
26
# File 'lib/fox_tail/stimulus_controller.rb', line 24

def target_key(raw: false)
  key :target, raw: raw
end

#to_sObject



75
76
77
# File 'lib/fox_tail/stimulus_controller.rb', line 75

def to_s
  identifier
end

#to_symObject



71
72
73
# File 'lib/fox_tail/stimulus_controller.rb', line 71

def to_sym
  identifier.gsub("-", "_").to_sym
end

#value_key(name, raw: false) ⇒ Object



32
33
34
# File 'lib/fox_tail/stimulus_controller.rb', line 32

def value_key(name, raw: false)
  key name, :value, raw: raw
end