Class: Vident::StimulusBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/vident/stimulus_builder.rb

Constant Summary collapse

DSL_PRIMITIVES =

Primitives the DSL block tracks. Controllers are set via the component’s ‘stimulus_controllers:` prop, not the DSL, so they’re skipped here. Storage shape per primitive is an Array for positional kinds (actions, targets) and a Hash for keyed kinds (values, params, classes, outlets).

Stimulus::PRIMITIVES.reject { |primitive| primitive.name == :controllers }.freeze

Instance Method Summary collapse

Constructor Details

#initializeStimulusBuilder

Returns a new instance of StimulusBuilder.



11
12
13
14
# File 'lib/vident/stimulus_builder.rb', line 11

def initialize
  @entries = DSL_PRIMITIVES.to_h { |primitive| [primitive.name, primitive.keyed? ? {} : []] }
  @values_from_props = []
end

Instance Method Details

#actions(*names) ⇒ Object



26
27
28
29
# File 'lib/vident/stimulus_builder.rb', line 26

def actions(*names)
  @entries[:actions].concat(names)
  self
end

#classes(**hash) ⇒ Object



46
47
48
49
# File 'lib/vident/stimulus_builder.rb', line 46

def classes(**hash)
  @entries[:classes].merge!(hash) unless hash.empty?
  self
end

#merge_with(other) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/vident/stimulus_builder.rb', line 16

def merge_with(other)
  DSL_PRIMITIVES.each do |primitive|
    mine = @entries[primitive.name]
    theirs = other.entries_for(primitive.name)
    primitive.keyed? ? mine.merge!(theirs) : mine.concat(theirs)
  end
  @values_from_props.concat(other.values_from_props_list)
  self
end

#outlets(positional = nil, **hash) ⇒ Object

‘outlets(=> “.sel”)` accepts a positional Hash for identifiers that can’t be Ruby kwarg keys (contain ‘–`).



58
59
60
61
62
63
# File 'lib/vident/stimulus_builder.rb', line 58

def outlets(positional = nil, **hash)
  bucket = @entries[:outlets]
  bucket.merge!(positional) if positional.is_a?(Hash)
  bucket.merge!(hash) unless hash.empty?
  self
end

#params(**hash) ⇒ Object



41
42
43
44
# File 'lib/vident/stimulus_builder.rb', line 41

def params(**hash)
  @entries[:params].merge!(hash) unless hash.empty?
  self
end

#targets(*names) ⇒ Object



31
32
33
34
# File 'lib/vident/stimulus_builder.rb', line 31

def targets(*names)
  @entries[:targets].concat(names)
  self
end

#to_attributes(component_instance, phase: :all) ⇒ Object

‘phase:` controls which entries are resolved.

  • ‘:static` — resolve non-proc entries only; procs are skipped entirely (no placeholder). Used at init time, before helpers/view_context exist.

  • ‘:procs` — resolve proc entries only; non-procs are skipped. Used at render time so procs can reach `helpers` / `view_context`.

  • ‘:all` — resolve everything (legacy path, retained for safety).



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/vident/stimulus_builder.rb', line 71

def to_attributes(component_instance, phase: :all)
  attrs = {}
  DSL_PRIMITIVES.each do |primitive|
    entries = @entries[primitive.name]
    next if entries.empty?
    resolved = resolve_entries(primitive, entries, component_instance, phase:)
    attrs[primitive.key] = resolved unless resolved.nil? || resolved.empty?
  end
  if phase != :procs && !@values_from_props.empty?
    attrs[:stimulus_values_from_props] = @values_from_props.dup
  end
  attrs
end

#to_hash(component_instance) ⇒ Object Also known as: to_h



85
# File 'lib/vident/stimulus_builder.rb', line 85

def to_hash(component_instance) = to_attributes(component_instance)

#values(**hash) ⇒ Object



36
37
38
39
# File 'lib/vident/stimulus_builder.rb', line 36

def values(**hash)
  @entries[:values].merge!(hash) unless hash.empty?
  self
end

#values_from_props(*names) ⇒ Object



51
52
53
54
# File 'lib/vident/stimulus_builder.rb', line 51

def values_from_props(*names)
  @values_from_props.concat(names)
  self
end