Class: Tailmix::HTML::StimulusBuilder
- Inherits:
-
Object
- Object
- Tailmix::HTML::StimulusBuilder
- Defined in:
- lib/tailmix/html/stimulus_builder.rb
Overview
A fluent DSL (builder) for constructing Stimulus data attributes. It acts as a proxy, modifying a DataMap instance directly.
Instance Method Summary collapse
-
#action(action_string) ⇒ self
Adds an action.
-
#context(controller_name) ⇒ Object
Sets the controller context for subsequent calls.
-
#controller(controller_name) ⇒ self
Defines a controller and sets it as the current context.
-
#initialize(data_map) ⇒ StimulusBuilder
constructor
A new instance of StimulusBuilder.
-
#target(target_name) ⇒ self
Adds a target, scoped to the current controller context.
-
#value(value_name, value) ⇒ self
Adds a value, scoped to the current controller context.
Constructor Details
#initialize(data_map) ⇒ StimulusBuilder
Returns a new instance of StimulusBuilder.
8 9 10 11 |
# File 'lib/tailmix/html/stimulus_builder.rb', line 8 def initialize(data_map) @data_map = data_map @context = nil # For context-aware attributes like targets and values end |
Instance Method Details
#action(action_string) ⇒ self
Adds an action.
31 32 33 34 |
# File 'lib/tailmix/html/stimulus_builder.rb', line 31 def action(action_string) @data_map.add_to_set(:action, action_string) self end |
#context(controller_name) ⇒ Object
Sets the controller context for subsequent calls.
22 23 24 25 |
# File 'lib/tailmix/html/stimulus_builder.rb', line 22 def context(controller_name) @context = controller_name.to_s self end |
#controller(controller_name) ⇒ self
Defines a controller and sets it as the current context.
15 16 17 18 19 |
# File 'lib/tailmix/html/stimulus_builder.rb', line 15 def controller(controller_name) @data_map.add_to_set(:controller, controller_name) @context = controller_name.to_s self end |
#target(target_name) ⇒ self
Adds a target, scoped to the current controller context.
38 39 40 41 42 43 44 |
# File 'lib/tailmix/html/stimulus_builder.rb', line 38 def target(target_name) ensure_context! # `target` is a shared attribute, but names are scoped to a controller. # So we add to the common `target` set. @data_map.add_to_set(:"#{@context}-target", target_name) self end |
#value(value_name, value) ⇒ self
Adds a value, scoped to the current controller context.
48 49 50 51 52 |
# File 'lib/tailmix/html/stimulus_builder.rb', line 48 def value(value_name, value) ensure_context! @data_map.merge!("#{context_key(value_name)}_value" => value) self end |