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
30
31
32
# 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 => ctrl_path, Symbol => sym]
    new(
      controller: Controller.parse(ctrl_path, implied: implied),
      name: Naming.js_name(sym)
    )
  in [String => s]
    raise ::Vident::ParseError,
      "Target.parse: a bare String is a controller path; target names must be Symbols " \
      "(got #{s.inspect}). Use `target :name` for a local target, or " \
      "`target \"path/to/ctrl\", :name` for cross-controller."
  else
    raise ::Vident::ParseError, "Target.parse: invalid arguments #{args.inspect}"
  end
end

.to_data_hash(targets) ⇒ Object



43
44
45
46
47
48
# File 'lib/vident/stimulus/target.rb', line 43

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



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

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

#to_data_pairObject



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

def to_data_pair = [data_attribute_key, name]

#to_hObject Also known as: to_hash



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

def to_h = {data_attribute_key => name}

#to_sObject



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

def to_s = name