Class: Vident::Stimulus::Param

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

Overview

‘data-<ctrl>-<name>-param` fragment — same shape as Value, distinct semantics on the JS side (read via `event.params.<camel>`).

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

to_data_hash

Methods included from Combinable

#deconstruct, #deconstruct_keys, #with

Class Method Details

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



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/vident/stimulus/param.rb', line 19

def self.parse(*args, implied:, component_id: nil)
  case args
  in [Symbol => name_sym, raw]
    new(
      controller: implied,
      name: name_sym.to_s.dasherize,
      serialized: serialize(raw)
    )
  in [String => ctrl_path, Symbol => name_sym, raw]
    new(
      controller: Controller.parse(ctrl_path, implied: implied),
      name: name_sym.to_s.dasherize,
      serialized: serialize(raw)
    )
  else
    raise ::Vident::ParseError, "Param.parse: invalid arguments #{args.inspect}"
  end
end

.serialize(raw) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/vident/stimulus/param.rb', line 38

def self.serialize(raw)
  raise ::Vident::ParseError, "Param.serialize: nil is not serializable — filter nil upstream" if raw.nil?
  case raw
  when Array, Hash then raw.to_json
  else raw.to_s
  end
end

Instance Method Details

#data_attribute_keyObject



48
# File 'lib/vident/stimulus/param.rb', line 48

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

#to_data_pairObject



50
# File 'lib/vident/stimulus/param.rb', line 50

def to_data_pair = [data_attribute_key, serialized]

#to_hObject Also known as: to_hash



52
# File 'lib/vident/stimulus/param.rb', line 52

def to_h = {data_attribute_key => serialized}

#to_sObject



46
# File 'lib/vident/stimulus/param.rb', line 46

def to_s = serialized