Class: Vident2::Stimulus::Target

Inherits:
Literal::Data
  • Object
show all
Defined in:
lib/vident2/stimulus/target.rb

Overview

‘data-<ctrl>-target` fragment. One per target reference; the Array aggregator groups by controller and space-joins same-key values.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

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

‘.parse(*args, implied:)`:

(Symbol)         -> target `:name` on implied controller
(String)         -> target name as-is on implied (already js-cased)
(String, Symbol) -> explicit (controller_path, target_name)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/vident2/stimulus/target.rb', line 19

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 ::Vident2::ParseError, "Target.parse: invalid arguments #{args.inspect}"
  end
end

.to_data_hash(targets) ⇒ Object

Same-key concat with space. Example:

Target(row)                       -> "foo-target" => "row"
Target(row) + Target(cell)        -> "foo-target" => "row cell"
Target(row) + Target(x, on: bar)  -> {"foo-target"=>"row", "bar-target"=>"x"}


49
50
51
52
53
54
# File 'lib/vident2/stimulus/target.rb', line 49

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



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

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

#to_data_pairObject



39
# File 'lib/vident2/stimulus/target.rb', line 39

def to_data_pair = [data_attribute_key, name]

#to_hObject Also known as: to_hash

Splat target for inline ‘data: Vident2::Stimulus::Target.**target**target.to_h` usage.



42
# File 'lib/vident2/stimulus/target.rb', line 42

def to_h = {data_attribute_key => name}

#to_sObject



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

def to_s = name