Class: Vident2::Stimulus::Controller
- Inherits:
-
Literal::Data
- Object
- Literal::Data
- Vident2::Stimulus::Controller
- Defined in:
- lib/vident2/stimulus/controller.rb
Overview
‘data-controller` fragment. Fields:
-
‘path` : raw underscored path (e.g. `“admin/user_card_component”`)
-
‘name` : dasherized/joined form (e.g. `“admin–user-card-component”`)
-
‘alias_name`: optional Symbol the DSL uses to refer back to this controller from other entries (`action(:save, on: :admin)`).
Class Method Summary collapse
-
.parse(*args, as: nil, implied:, component_id: nil) ⇒ Object
‘.parse(path = nil, as: nil, implied:)`.
-
.to_data_hash(controllers) ⇒ Object
Space-joined.
Instance Method Summary collapse
- #identifier ⇒ Object
- #to_data_pair ⇒ Object
- #to_h ⇒ Object (also: #to_hash)
- #to_s ⇒ Object
Class Method Details
.parse(*args, as: nil, implied:, component_id: nil) ⇒ Object
‘.parse(path = nil, as: nil, implied:)`
No positional arg -> clone the implied controller (for unambiguous “refer to my own controller” use).
One positional arg (String | Symbol) -> explicit controller path. ‘implied:` is unused in that branch but is accepted for uniformity with the other kinds’ ‘.parse` signatures.
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/vident2/stimulus/controller.rb', line 26 def self.parse(*args, as: nil, implied:, component_id: nil) case args.size when 0 new(path: implied.path, name: implied.name, alias_name: as) when 1 raw = args[0] path = raw.to_s new(path: path, name: Naming.stimulize_path(path), alias_name: as) else raise ::Vident2::ParseError, "Controller.parse: expected 0 or 1 positional args, got #{args.size}" end end |
.to_data_hash(controllers) ⇒ Object
Space-joined. Order preserved, duplicates kept (caller dedups).
51 52 53 54 55 56 |
# File 'lib/vident2/stimulus/controller.rb', line 51 def self.to_data_hash(controllers) return {} if controllers.empty? joined = controllers.map(&:name).reject(&:empty?).join(" ") return {} if joined.empty? {controller: joined} end |
Instance Method Details
#identifier ⇒ Object
39 |
# File 'lib/vident2/stimulus/controller.rb', line 39 def identifier = name |
#to_data_pair ⇒ Object
43 |
# File 'lib/vident2/stimulus/controller.rb', line 43 def to_data_pair = [:controller, name] |
#to_h ⇒ Object Also known as: to_hash
45 46 47 |
# File 'lib/vident2/stimulus/controller.rb', line 45 def to_h = {controller: name} # Ruby's `{**x}` splat calls #to_hash; alias so users can splat the # data-attr pair directly into a tag's `data:` option. |
#to_s ⇒ Object
41 |
# File 'lib/vident2/stimulus/controller.rb', line 41 def to_s = name |