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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.event(event_name, full_action_name) ⇒ Object

Constructs a Stimulus event hook string eg.

event('click', 'summary#redirect') #=> "click->summary#redirect"

Signature:

  • (String, String) -> String



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.

Signature:

  • (bool) -> String?



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.

Signature:

  • (*String?) -> String



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!

Signature:

  • (Array[String?]) -> String



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"

Signature:

  • (String, String) -> String



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

#hObject



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

def h = view_context

#image_exists?(path) ⇒ Boolean

Signature:

  • (String) -> bool

Returns:

  • (Boolean)


96
97
98
# File 'lib/phlex/stimulus/components/base.rb', line 96

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

#inspectObject

Signature:

  • -> String



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.

Signature:

  • [K, V] (*Hash[K, V]?) -> Hash[K, V]



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.

Signature:

  • ?{ -> void } -> void



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