Class: Phlex::Stimulus::Components::Base Abstract

Inherits:
HTML
  • Object
show all
Defined in:
lib/phlex/stimulus/components/base.rb

Overview

This class is abstract.

Abstract class for Phlex components that render HTML with better sorbet and stimulus support.

Direct Known Subclasses

Controller

Instance Method Summary collapse

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.

: (bool) -> String?



33
34
35
# File 'lib/phlex/stimulus/components/base.rb', line 33

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.

: (*String?) -> String



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/phlex/stimulus/components/base.rb', line 53

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!

: (Array) -> String



66
67
68
# File 'lib/phlex/stimulus/components/base.rb', line 66

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"

: (String, String) -> String



23
24
25
# File 'lib/phlex/stimulus/components/base.rb', line 23

def event(event_name, full_action_name)
  "#{event_name}->#{full_action_name}"
end

#hObject



10
# File 'lib/phlex/stimulus/components/base.rb', line 10

def h = view_context

#image_exists?(path) ⇒ Boolean

: (String) -> bool

Returns:

  • (Boolean)


74
75
76
# File 'lib/phlex/stimulus/components/base.rb', line 74

def image_exists?(path)
  Boolean(h.resolve_asset_path(path))
end

#inspectObject

: -> String



13
14
15
# File 'lib/phlex/stimulus/components/base.rb', line 13

def inspect
  "#<#{self.class.name}>"
end

#newline(&block) ⇒ Object

Output a single newline character. If a block is given, a space will be output before and after the block.

: ?{ -> void } -> void



41
42
43
44
45
46
47
# File 'lib/phlex/stimulus/components/base.rb', line 41

def newline(&block)
  plain "\n"
  return unless block

  block.call
  plain "\n"
end