Sha256: a7c84f47e4e6390be9c08613765f863114e44cbbd07d77d0df496ed8fdac8b00

Contents?: true

Size: 1.36 KB

Versions: 3

Compression:

Stored size: 1.36 KB

Contents

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

    def from_array_of_hash(array_of_hash, attributes)
      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, attributes 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, attributes)
      o = Object.new
      o.extend(Rack::App::FrontEnd::Helpers::HtmlDsl)

      table_html = o.__send__ :table_tag, attributes 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, attributes={})
    if object.is_a?(Array) && object.all? { |e| e.is_a?(Hash) }
      Builder.from_array_of_hash(object, attributes)
    elsif object.is_a?(Hash)
      Builder.from_hash(object, attributes)
    else
      raise("don't know how to build table from this: #{object}")
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rack-app-front_end-0.22.2 lib/rack/app/front_end/helpers/table.rb
rack-app-front_end-0.22.1 lib/rack/app/front_end/helpers/table.rb
rack-app-front_end-0.22.0 lib/rack/app/front_end/helpers/table.rb