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) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/vident/stimulus_builder.rb', line 65

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

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



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

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