Class: Phlex::Stimulus::Components::Base Abstract
- Inherits:
-
HTML
- Object
- HTML
- Phlex::Stimulus::Components::Base
- Defined in:
- lib/phlex/stimulus/components/base.rb
Overview
Abstract class for Phlex components that render HTML with better sorbet and stimulus support.
Direct Known Subclasses
Class Method Summary collapse
-
.event(event_name, full_action_name) ⇒ Object
Constructs a Stimulus event hook string eg.
Instance Method Summary collapse
-
#attrbool(val) ⇒ Object
Converts a Ruby boolean to a boolean value in HTML attributes.
-
#class_list(*args) ⇒ Object
(also: #strlist)
Merges the given list of class names (with optional nils) into a single class string separated by spaces.
- #class_list!(args) ⇒ Object (also: #strlist!)
-
#event(event_name, full_action_name) ⇒ Object
Constructs a Stimulus event hook string eg.
- #h ⇒ Object
- #image_exists?(path) ⇒ Boolean
- #inspect ⇒ Object
-
#merge(*hashes) ⇒ Object
Merges the given list of hashes (with optional nils) into a single hash.
-
#newline(&block) ⇒ Object
Output a single newline character.
Class Method Details
.event(event_name, full_action_name) ⇒ Object
Constructs a Stimulus event hook string eg.
event('click', 'summary#redirect') #=> "click->summary#redirect"
17 18 19 |
# File 'lib/phlex/stimulus/components/base.rb', line 17 def event(event_name, full_action_name) "#{event_name}->#{full_action_name}" end |
Instance Method Details
#attrbool(val) ⇒ Object
Converts a Ruby boolean to a boolean value in HTML attributes.
Returns "true" for a truthy value, otherwise nil.
45 46 47 |
# File 'lib/phlex/stimulus/components/base.rb', line 45 def attrbool(val) 'true' if val end |
#class_list(*args) ⇒ Object Also known as: strlist
Merges the given list of class names (with optional nils) into a single class string separated by spaces.
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/phlex/stimulus/components/base.rb', line 65 def class_list(*args) buff = String.new args.each do |arg| next unless arg buff << arg << ' ' end buff.strip end |
#class_list!(args) ⇒ Object Also known as: strlist!
78 79 80 |
# File 'lib/phlex/stimulus/components/base.rb', line 78 def class_list!(args) class_list(*T.unsafe(args)) end |
#event(event_name, full_action_name) ⇒ Object
Constructs a Stimulus event hook string eg.
event('click', 'summary#redirect') #=> "click->summary#redirect"
35 36 37 |
# File 'lib/phlex/stimulus/components/base.rb', line 35 def event(event_name, full_action_name) "#{event_name}->#{full_action_name}" end |
#h ⇒ Object
22 |
# File 'lib/phlex/stimulus/components/base.rb', line 22 def h = view_context |
#image_exists?(path) ⇒ Boolean
96 97 98 |
# File 'lib/phlex/stimulus/components/base.rb', line 96 def image_exists?(path) Boolean(h.resolve_asset_path(path)) end |
#inspect ⇒ Object
25 26 27 |
# File 'lib/phlex/stimulus/components/base.rb', line 25 def inspect "#<#{self.class.name}>" end |
#merge(*hashes) ⇒ Object
Merges the given list of hashes (with optional nils) into a single hash.
89 90 91 92 93 |
# File 'lib/phlex/stimulus/components/base.rb', line 89 def merge(*hashes) hashes.each_with_object({}) do |elem, acc| acc.merge!(elem) if elem end end |
#newline(&block) ⇒ Object
Output a single newline character. If a block is given, a space will be output before and after the block.
53 54 55 56 57 58 59 |
# File 'lib/phlex/stimulus/components/base.rb', line 53 def newline(&block) plain "\n" return unless block block.call plain "\n" end |