Class: Vident::Stimulus::Target

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

Overview

‘data-<ctrl>-target` fragment.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Combinable

#deconstruct, #deconstruct_keys, #with

Class Method Details

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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/vident/stimulus/target.rb', line 15

def self.parse(*args, implied:, component_id: nil)
  case args
  in [Symbol => sym]
    new(controller: implied, name: Naming.js_name(sym))
  in [String => str]
    new(controller: implied, name: str)
  in [String => ctrl_path, Symbol => sym]
    new(
      controller: Controller.parse(ctrl_path, implied: implied),
      name: Naming.js_name(sym)
    )
  else
    raise ::Vident::ParseError, "Target.parse: invalid arguments #{args.inspect}"
  end
end

.to_data_hash(targets) ⇒ Object



40
41
42
43
44
45
# File 'lib/vident/stimulus/target.rb', line 40

def self.to_data_hash(targets)
  targets.each_with_object({}) do |t, acc|
    key, value = t.to_data_pair
    acc[key] = acc.key?(key) ? "#{acc[key]} #{value}" : value
  end
end

Instance Method Details

#data_attribute_keyObject



33
# File 'lib/vident/stimulus/target.rb', line 33

def data_attribute_key = :"#{controller.name}-target"

#to_data_pairObject



35
# File 'lib/vident/stimulus/target.rb', line 35

def to_data_pair = [data_attribute_key, name]

#to_hObject Also known as: to_hash



37
# File 'lib/vident/stimulus/target.rb', line 37

def to_h = {data_attribute_key => name}

#to_sObject



31
# File 'lib/vident/stimulus/target.rb', line 31

def to_s = name