class Eskimo::HTML::Component

Base HTMLElement component which takes a {tag_name}, a set of {attributes}, and optionally a set of {children} and turns them into a beautiful – believe it or not – HTML tag:

<x-foo <!-- attributes -->>
  <!-- children -->
</x-foo>

See {ASCII::Component} for how this works under the hood since it's similar.

Constants

ATTRIBUTE_REWRITES

Mapping of Ruby attribute name to HTML attribute name; some words like “class” are reserved and are problematic when passed as attributes so this hash supports a method to rewrite those.

Attributes

attributes[R]
children[R]
tag_name[R]

Public Class Methods

new(attributes = {}, &children) click to toggle source
# File lib/eskimo/html/component.rb, line 25
def initialize(attributes = {}, &children)
  @attributes = attributes
  @children = children
end

Public Instance Methods

render(render:, **) click to toggle source
# File lib/eskimo/html/component.rb, line 30
def render(render:, **)
  tag_with_attributes = "#{tag_name} #{serialize_attributes}"

  "<#{tag_with_attributes.strip}>#{render[@children]}</#{tag_name}>"
end

Private Instance Methods

serialize_attributes() click to toggle source
# File lib/eskimo/html/component.rb, line 38
def serialize_attributes
  attributes.map do |(k, v)|
    k = ATTRIBUTE_REWRITES.fetch(k.to_s, k.to_s)

    case v
    when true
      "#{k}=\"#{k}\""
    when false
    else
      "#{k}=\"#{v}\""
    end
  end.join(' ')
end