Class: Eskimo::HTML::Component

Inherits:
Object
  • Object
show all
Defined in:
gems/eskimo-html/lib/eskimo/html/component.rb

Overview

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.

Constant Summary collapse

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.

{
  'className' => 'class'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}, &children) ⇒ Component

Returns a new instance of Component



25
26
27
28
# File 'gems/eskimo-html/lib/eskimo/html/component.rb', line 25

def initialize(attributes = {}, &children)
  @attributes = attributes
  @children = children
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes



21
22
23
# File 'gems/eskimo-html/lib/eskimo/html/component.rb', line 21

def attributes
  @attributes
end

#childrenObject (readonly)

Returns the value of attribute children



22
23
24
# File 'gems/eskimo-html/lib/eskimo/html/component.rb', line 22

def children
  @children
end

#tag_nameObject (readonly)

Returns the value of attribute tag_name



23
24
25
# File 'gems/eskimo-html/lib/eskimo/html/component.rb', line 23

def tag_name
  @tag_name
end

Instance Method Details

#render(render:) ⇒ Object



30
31
32
33
34
# File 'gems/eskimo-html/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