Sha256: a77c1c5e81c45e39b01b3ff2e812f1463ad184990b67d8af18830152b39a7978

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

module Rack::App::FrontEnd::Helpers::Table
  module Builder
    extend(self)

    def from_array_of_hash(array_of_hash)
      headers = array_of_hash.each_with_object([]) do |hash, trs|
        trs.push(*hash.keys)
        trs.uniq!
      end

      o = Object.new
      o.extend(Rack::App::FrontEnd::Helpers::HtmlDsl)

      table_html = o.__send__ :table_tag do
        tr_tag do
          headers.each do |header|
            td_tag String(header)
          end
        end

        array_of_hash.each do |hash|
          tr_tag do
            headers.each do |header|
              td_tag String(hash[header])
            end
          end
        end
      end

      table_html
    end

    def from_hash(hash)
      o = Object.new
      o.extend(Rack::App::FrontEnd::Helpers::HtmlDsl)

      table_html = o.__send__ :table_tag do
        hash.each do |key, value|
          tr_tag do
            td_tag String(key)
            td_tag String(value)
          end
        end
      end

      table_html
    end
  end

  def table_by(object)
    if object.is_a?(Array) && object.all? { |e| e.is_a?(Hash) }
      Builder.from_array_of_hash(object)
    elsif object.is_a?(Hash)
      Builder.from_hash(object)
    else
      raise("don't know how to build table from this: #{object}")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rack-app-front_end-0.21.0 lib/rack/app/front_end/helpers/table.rb