Class: OrderedListComponent

Inherits:
Object
  • Object
show all
Includes:
HTMLComponent
Defined in:
lib/html-native/collections.rb

Overview

OrderedListComponent represents an HTML ordered list based on an Enumerable collection.

Attributes in an OrderedListComponent are separated into multiple groups since there are multiple kinds of tags. These groups are:

  • list - The attributes associated with the <ol> element

  • item - The attributes associated with <li> elements

For example, to have a list with 20px padding and the class of “list-item” given to each item, you could write: “‘ OrderedListComponent.new(data, attributes:

{list: {style: "padding: 20px"}, item: {class: "list-item"}})

“‘ which is equivalent to “` <ol style=“padding: 20px”>

<li class="list-item">...</li>
<li class="list-item">...</li>
...

</ol> “‘

Constant Summary

Constants included from HTMLComponent

HTMLComponent::FORBIDDEN_ATTRIBUTES, HTMLComponent::LIMITED_ATTRIBUTES, HTMLComponent::TAG_LIST

Instance Method Summary collapse

Methods included from HTMLComponent

#_if, #_label, #_unless, #doctype, singleton, #valid_attribute?

Constructor Details

#initialize(data, attributes: {}, &block) ⇒ OrderedListComponent

Creates a new instance of OrderedListComponent from the values of data.

If a block is given, each item in data is passed to it to render the list items. If no block is given, data are used directly.



67
68
69
70
71
72
# File 'lib/html-native/collections.rb', line 67

def initialize(data, attributes: {}, &block)
  @list_data = data
  @list_attributes = attributes[:list] || {}
  @item_attributes = attributes[:item] || {}
  @block = block
end

Instance Method Details

#renderObject

Converts the OrderedListComponent instance to the equivalent HTML.

render can be called directly, but that usually isn’t necessary. HTMLComponent::Builder handles this automatically, so it only needs to be done if there is no prior instance of one.



79
80
81
82
83
84
85
86
87
# File 'lib/html-native/collections.rb', line 79

def render
  ol(@list_attributes) do
    @list_data.component_map do |l|
      li(@item_attributes) do
        @block ? @block.call(l) : l
      end
    end
  end
end