Sha256: 05dcd2ebda0b125a199ef4d9e137130c31a1022e7b0f485c203447270d1f3a63

Contents?: true

Size: 655 Bytes

Versions: 2

Compression:

Stored size: 655 Bytes

Contents

require 'kosi'

module Slacken
  class TableElement
    def initialize(children)
      if children.first.type.name == :thead
        thead, tbody = children.slice(0, 2)
        @header = thead.child # tr tag
        @columns = tbody.children # tr tags
      else
        @header = nil
        @columns = children
      end
    end

    def render
      Kosi::Table.new(table_head).render(table_body).to_s.chomp
    end

    def to_s
      render
    end

    private

    def table_head
      @header ? { header: @header.children.map(&:to_s) } : {}
    end

    def table_body
      @columns.map { |column| column.children.map(&:to_s) }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
slacken-0.1.4 lib/slacken/table_element.rb
slacken-0.1.3 lib/slacken/table_element.rb