Sha256: bb9abf1b8022873c0da99fd42cfdd2aa52030d5e826e90915b3cd228006e7b07

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

# http://www.panic.com/statusboard/docs/table_tutorial.pdf
require 'csv'

module PanicBoardData

  class ProgressBar
    attr_accessor :value

    def to_s
      ['<td class="projectsBars">',
       (1..self.value).to_a
               .map { |x| "<div class=\"barSegment value#{x}\"></div>" }
               .join,
       '</td>'].join
    end
  end

  class Table

    attr_accessor :data, :widths, :base_image_url

    def initialize(data = [])
      @data = data
    end

    def build_image value
      "<img src=\"#{url_for(value)}\" />"
    end

    def progress_bar_to int
      progress_bar = ProgressBar.new
      progress_bar.value = int
      progress_bar
    end

    def to_html
      "<table>#{data_to_table_rows}</table>"
    end

    def to_csv
      self.class.to_csv self.data
    end

    def self.to_csv data
      ::CSV.generate do |csv|
        data.each { |row| csv << row }
      end.strip
    end

    private

    def url_for value
      [self.base_image_url, value]
        .select { |x| x.to_s != '' }
        .map    { |x| x.to_s.strip }
        .map    { |x| x.gsub('/', '') }
        .join('/')
        .gsub('http:', 'http://')
        .gsub('https:', 'https://')
    end

    def data_to_table_rows
      return '' unless data
      data.map { |r| build_row_for r }.join
    end

    def build_row_for record
      result = record.each_with_index.map { |v, i| build_cell_for v, i }.join
      "<tr>#{result}</tr>"
    end

    def build_cell_for value, index
      value = flatten_a_value_array_to_a_single_value value
      width = get_width_for index
      render_cell value, width
    end

    def flatten_a_value_array_to_a_single_value value
      value.is_a?(Array) ? value.join('') : value
    end

    def get_width_for index
      widths ? widths[index] : nil
    end

    def render_cell value, width
      return value.to_s if value.is_a? PanicBoardData::ProgressBar
      width ? "<td style=\"width: #{width}px\">#{value}</td>"
            : "<td>#{value}</td>"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
panic_board_data-0.0.10 lib/panic_board_data/table.rb