Class: Vident::Stimulus::Value

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

Overview

‘data-<ctrl>-<name>-value` fragment. Holds the serialised form (always a String). Only `nil` is rejected — `false`, blank strings, and empty collections emit their serialised form.

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



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

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

.serialize(raw) ⇒ Object



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

def self.serialize(raw)
  raise ::Vident::ParseError, "Value.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



49
# File 'lib/vident/stimulus/value.rb', line 49

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

#to_data_pairObject



51
# File 'lib/vident/stimulus/value.rb', line 51

def to_data_pair = [data_attribute_key, serialized]

#to_hObject Also known as: to_hash



53
# File 'lib/vident/stimulus/value.rb', line 53

def to_h = {data_attribute_key => serialized}

#to_sObject



47
# File 'lib/vident/stimulus/value.rb', line 47

def to_s = serialized