Class: LocoMotion::ComponentConfig
- Inherits:
-
Object
- Object
- LocoMotion::ComponentConfig
- Defined in:
- lib/loco_motion/component_config.rb
Instance Attribute Summary collapse
-
#component ⇒ Object
readonly
Returns the value of attribute component.
-
#modifiers ⇒ Object
readonly
Returns the value of attribute modifiers.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parts ⇒ Object
readonly
Returns the value of attribute parts.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
-
#add_css(part_name, css) ⇒ Object
Adds default CSS to the requested component part.
-
#add_html(part_name, html) ⇒ Object
Adds default HTML to the requested component part.
-
#add_stimulus_controller(part_name, controller_name) ⇒ Object
Add a default Stimulus (Javascript) controller to the requested component part.
- #build ⇒ Object
-
#get_part(part_name) ⇒ Object
Returns the part for the reqeust part name or an empty hash if none was found.
-
#initialize(component, **kws, &block) ⇒ ComponentConfig
constructor
A new instance of ComponentConfig.
-
#inspect ⇒ Object
For now, just return the Hash version for inspect.
-
#merge_user_options!(**kws) ⇒ Object
Add specific component user options if they pass shortened attributes.
-
#set_tag_name(part_name, tag_name) ⇒ Object
Sets the default tag name for the requested component part.
-
#smart_merge!(**kws) ⇒ Object
Merge additional options into the defaults config by combining the new options with the existing options, rather than overwriting (where possible).
-
#to_h ⇒ Object
Render a Hash version of the config.
-
#valid_parts ⇒ Object
Return a list of valid parts for the component.
-
#validate ⇒ Object
Validate the component config and throw errors if there are issues.
-
#validate_modifiers ⇒ Object
Validate that all of the modifiers are correct.
-
#validate_part(part_name) ⇒ Object
Validates that the requested part is valid for the component.
Constructor Details
#initialize(component, **kws, &block) ⇒ ComponentConfig
4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/loco_motion/component_config.rb', line 4 def initialize(component, **kws, &block) @component = component = kws @parts = {} @modifiers = (kws[:modifiers] || [kws[:modifier]]).compact @size = kws[:size] build validate end |
Instance Attribute Details
#component ⇒ Object (readonly)
Returns the value of attribute component.
2 3 4 |
# File 'lib/loco_motion/component_config.rb', line 2 def component @component end |
#modifiers ⇒ Object (readonly)
Returns the value of attribute modifiers.
2 3 4 |
# File 'lib/loco_motion/component_config.rb', line 2 def modifiers @modifiers end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
2 3 4 |
# File 'lib/loco_motion/component_config.rb', line 2 def end |
#parts ⇒ Object (readonly)
Returns the value of attribute parts.
2 3 4 |
# File 'lib/loco_motion/component_config.rb', line 2 def parts @parts end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
2 3 4 |
# File 'lib/loco_motion/component_config.rb', line 2 def size @size end |
Instance Method Details
#add_css(part_name, css) ⇒ Object
Adds default CSS to the requested component part.
89 90 91 |
# File 'lib/loco_motion/component_config.rb', line 89 def add_css(part_name, css) @parts[part_name][:default_css] << css if css end |
#add_html(part_name, html) ⇒ Object
Adds default HTML to the requested component part.
96 97 98 |
# File 'lib/loco_motion/component_config.rb', line 96 def add_html(part_name, html) @parts[part_name][:default_html] = @parts[part_name][:default_html].deep_merge(html) if html end |
#add_stimulus_controller(part_name, controller_name) ⇒ Object
Add a default Stimulus (Javascript) controller to the requested component part.
103 104 105 106 107 |
# File 'lib/loco_motion/component_config.rb', line 103 def add_stimulus_controller(part_name, controller_name) @parts[part_name] ||= {} @parts[part_name][:default_stimulus_controllers] ||= [] @parts[part_name][:default_stimulus_controllers] << controller_name end |
#build ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/loco_motion/component_config.rb', line 16 def build # Allow users to pass css/html for a specific part (i.e. modal_dialog) @component.component_parts.each do |part, defaults| @parts[part] = { default_css: [], default_html: {}, default_tag_name: defaults[:tag_name] || :div, default_stimulus_controllers: [], user_css: ["#{part}_css".to_sym] || [], user_html: ["#{part}_html".to_sym] || {}, user_tag_name: ["#{part}_tag_name".to_sym], user_stimulus_controllers: ["#{part}_controllers".to_sym] || [], } end # Allow useres to pass some shortened attributes for the component part (**) end |
#get_part(part_name) ⇒ Object
Returns the part for the reqeust part name or an empty hash if none was found.
75 76 77 |
# File 'lib/loco_motion/component_config.rb', line 75 def get_part(part_name) @parts[part_name] || {} end |
#inspect ⇒ Object
For now, just return the Hash version for inspect.
157 158 159 160 161 162 163 164 165 |
# File 'lib/loco_motion/component_config.rb', line 157 def inspect [ "#<#{self.class.name}", "@options=#{@options.inspect}", "@parts=#{@parts.inspect}", "@modifiers=#{@modifiers.inspect}", "@size=#{@size.inspect}", ].join(" ") + ">" end |
#merge_user_options!(**kws) ⇒ Object
Add specific component user options if they pass shortened attributes.
39 40 41 42 43 44 45 |
# File 'lib/loco_motion/component_config.rb', line 39 def (**kws) @parts[:component][:user_tag_name] = kws[:tag_name] if kws[:tag_name] @parts[:component][:user_css].push(kws[:css]) if kws[:css] @parts[:component][:user_html].deep_merge!(kws[:html]) if kws[:html] @parts[:component][:user_stimulus_controllers].push(kws[:controller]) if kws[:controller] @parts[:component][:user_stimulus_controllers].push(kws[:controllers]) if kws[:controllers] end |
#set_tag_name(part_name, tag_name) ⇒ Object
Sets the default tag name for the requested component part.
82 83 84 |
# File 'lib/loco_motion/component_config.rb', line 82 def set_tag_name(part_name, tag_name) @parts[part_name][:default_tag_name] = tag_name if tag_name end |
#smart_merge!(**kws) ⇒ Object
Merge additional options into the defaults config by combining the new options with the existing options, rather than overwriting (where possible).
HTML will be deep merged, CSS will be appended, and tag_name will be overwritten.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/loco_motion/component_config.rb', line 54 def smart_merge!(**kws) @component.component_parts.each do |part, defaults| set_tag_name(part, kws["#{part}_tag_name".to_sym]) add_css(part, kws["#{part}_css".to_sym]) add_html(part, kws["#{part}_html".to_sym]) controllers = kws["#{part}_controllers".to_sym] || [] controllers.each do |controller_name| add_stimulus_controller(part, controller_name) end end # Make sure to merge any user-provided options as well (**kws) end |
#to_h ⇒ Object
Render a Hash version of the config.
145 146 147 148 149 150 151 152 |
# File 'lib/loco_motion/component_config.rb', line 145 def to_h { options: , parts: @parts, modifiers: @modifiers, size: @size } end |
#valid_parts ⇒ Object
Return a list of valid parts for the component.
138 139 140 |
# File 'lib/loco_motion/component_config.rb', line 138 def valid_parts @parts.keys end |
#validate ⇒ Object
Validate the component config and throw errors if there are issues.
112 113 114 |
# File 'lib/loco_motion/component_config.rb', line 112 def validate validate_modifiers end |
#validate_modifiers ⇒ Object
Validate that all of the modifiers are correct.
119 120 121 122 123 124 125 126 |
# File 'lib/loco_motion/component_config.rb', line 119 def validate_modifiers # Check to make sure they have passed a valid / defined modifier (@modifiers || []).each do |modifier| if modifier.present? && !@component.valid_modifiers.include?(modifier) raise LocoMotion::InvalidModifierError.new(modifier, @component) end end end |
#validate_part(part_name) ⇒ Object
Validates that the requested part is valid for the component.
131 132 133 |
# File 'lib/loco_motion/component_config.rb', line 131 def validate_part(part_name) raise LocoMotion::UnknownPartError.new(part_name, @component) unless valid_parts.include?(part_name) end |