Class: Vident2::Stimulus::Value
- Inherits:
-
Literal::Data
- Object
- Literal::Data
- Vident2::Stimulus::Value
- Defined in:
- lib/vident2/stimulus/value.rb
Overview
‘data-<ctrl>-<name>-value` fragment. Holds the serialised form (always a String post-parse); Array/Hash inputs go through `to_json`, other non-nil inputs through `to_s`. The `Null` sentinel’s ‘to_s` produces `“null”` naturally. Only `nil` drops at the caller —`false`, blank strings, and empty collections emit their serialised form.
Class Method Summary collapse
-
.parse(*args, implied:, component_id: nil) ⇒ Object
‘.parse(*args, implied:)` grammar: (Symbol, raw) -> implied controller, value named `Symbol` (String, Symbol, raw) -> explicit (controller_path, name, raw).
-
.serialize(raw) ⇒ Object
Raw -> String.
-
.to_data_hash(values) ⇒ Object
One entry per Value instance.
Instance Method Summary collapse
- #data_attribute_key ⇒ Object
- #to_data_pair ⇒ Object
- #to_h ⇒ Object (also: #to_hash)
- #to_s ⇒ Object
Class Method Details
.parse(*args, implied:, component_id: nil) ⇒ Object
‘.parse(*args, implied:)` grammar:
(Symbol, raw) -> implied controller, value named `Symbol`
(String, Symbol, raw) -> explicit (controller_path, name, raw)
The caller (Resolver / mutator) is responsible for filtering out ‘nil` before reaching here — see `serialize` for the check.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/vident2/stimulus/value.rb', line 28 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 ::Vident2::ParseError, "Value.parse: invalid arguments #{args.inspect}" end end |
.serialize(raw) ⇒ Object
Raw -> String. ‘Null` sentinel serialises to `“null”` via its own `to_s`. `nil` should have been filtered upstream; raising here catches misrouted callers early.
50 51 52 53 54 55 56 |
# File 'lib/vident2/stimulus/value.rb', line 50 def self.serialize(raw) raise ::Vident2::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 |
.to_data_hash(values) ⇒ Object
One entry per Value instance. Later-instance same-key wins on collision (Hash#merge semantics).
69 70 71 72 73 74 |
# File 'lib/vident2/stimulus/value.rb', line 69 def self.to_data_hash(values) values.each_with_object({}) do |v, acc| key, str = v.to_data_pair acc[key] = str end end |
Instance Method Details
#data_attribute_key ⇒ Object
60 |
# File 'lib/vident2/stimulus/value.rb', line 60 def data_attribute_key = :"#{controller.name}-#{name}-value" |
#to_data_pair ⇒ Object
62 |
# File 'lib/vident2/stimulus/value.rb', line 62 def to_data_pair = [data_attribute_key, serialized] |
#to_h ⇒ Object Also known as: to_hash
64 |
# File 'lib/vident2/stimulus/value.rb', line 64 def to_h = {data_attribute_key => serialized} |
#to_s ⇒ Object
58 |
# File 'lib/vident2/stimulus/value.rb', line 58 def to_s = serialized |