Class: Vident::StimulusBuilder
- Inherits:
-
Object
- Object
- Vident::StimulusBuilder
- Defined in:
- lib/vident/stimulus_builder.rb
Instance Method Summary collapse
- #actions(*action_names) ⇒ Object
- #classes(**class_mappings) ⇒ Object
-
#initialize ⇒ StimulusBuilder
constructor
A new instance of StimulusBuilder.
- #merge_with(other_builder) ⇒ Object
- #outlets(positional = nil, **outlet_mappings) ⇒ Object
- #targets(*target_names) ⇒ Object
- #to_attributes(component_instance) ⇒ Object
- #to_hash(component_instance) ⇒ Object (also: #to_h)
- #values(**value_hash) ⇒ Object
- #values_from_props(*prop_names) ⇒ Object
Constructor Details
#initialize ⇒ StimulusBuilder
Returns a new instance of StimulusBuilder.
5 6 7 8 9 10 11 12 |
# File 'lib/vident/stimulus_builder.rb', line 5 def initialize @actions = [] @targets = [] @values = {} @values_from_props = [] @classes = {} @outlets = {} end |
Instance Method Details
#actions(*action_names) ⇒ Object
24 25 26 27 |
# File 'lib/vident/stimulus_builder.rb', line 24 def actions(*action_names) @actions.concat(action_names) self end |
#classes(**class_mappings) ⇒ Object
44 45 46 47 |
# File 'lib/vident/stimulus_builder.rb', line 44 def classes(**class_mappings) @classes.merge!(class_mappings) self end |
#merge_with(other_builder) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/vident/stimulus_builder.rb', line 14 def merge_with(other_builder) @actions.concat(other_builder.actions_list) @targets.concat(other_builder.targets_list) @values.merge!(other_builder.values_hash) @values_from_props.concat(other_builder.values_from_props_list) @classes.merge!(other_builder.classes_hash) @outlets.merge!(other_builder.outlets_hash) self end |
#outlets(positional = nil, **outlet_mappings) ⇒ Object
49 50 51 52 53 |
# File 'lib/vident/stimulus_builder.rb', line 49 def outlets(positional = nil, **outlet_mappings) @outlets.merge!(positional) if positional.is_a?(Hash) @outlets.merge!(outlet_mappings) unless outlet_mappings.empty? self end |
#targets(*target_names) ⇒ Object
29 30 31 32 |
# File 'lib/vident/stimulus_builder.rb', line 29 def targets(*target_names) @targets.concat(target_names) self end |
#to_attributes(component_instance) ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/vident/stimulus_builder.rb', line 55 def to_attributes(component_instance) attrs = {} attrs[:stimulus_actions] = resolve_attributes_filtering_nil(@actions, component_instance) unless @actions.empty? attrs[:stimulus_targets] = resolve_attributes_filtering_nil(@targets, component_instance) unless @targets.empty? attrs[:stimulus_values] = resolve_hash_values_allowing_nil(@values, component_instance) unless @values.empty? attrs[:stimulus_values_from_props] = @values_from_props.dup unless @values_from_props.empty? attrs[:stimulus_classes] = resolve_hash_classes_filtering_nil(@classes, component_instance) unless @classes.empty? attrs[:stimulus_outlets] = @outlets.dup unless @outlets.empty? attrs end |
#to_hash(component_instance) ⇒ Object Also known as: to_h
66 67 68 |
# File 'lib/vident/stimulus_builder.rb', line 66 def to_hash(component_instance) to_attributes(component_instance) end |
#values(**value_hash) ⇒ Object
34 35 36 37 |
# File 'lib/vident/stimulus_builder.rb', line 34 def values(**value_hash) @values.merge!(value_hash) unless value_hash.empty? self end |
#values_from_props(*prop_names) ⇒ Object
39 40 41 42 |
# File 'lib/vident/stimulus_builder.rb', line 39 def values_from_props(*prop_names) @values_from_props.concat(prop_names) self end |