class Rubypivot::SpreadTableLine

Constants

LINE_TYPES

Attributes

attribut[RW]
line_data[RW]
line_type[R]
options[R]
title[RW]

Public Class Methods

new(line_type, line_data = [], options = {}) click to toggle source
# File lib/rubypivot/spread_table.rb, line 26
def initialize(line_type, line_data = [], options = {})
  @options = options
  @attribute = {}
  @attribs = []
  set_line_type(line_type, @options)
  # Desparate to have an array, convert it if not
  if line_data.is_a?(Array)
    @line_data = line_data
  elsif line_data.is_a?(Hash)
    @line_data = []
    line_data.each do |key, value|
      @line_data << value
    end
  elsif line_data.is_a?(String)
    @line_data = line_data.split(/[ \t]+/)
  else
    @line_data = [line_data]
  end
  if options[:title]
    if options[:title] == :first
      @title = @line_data.shift
    else
      @title = options[:title].to_s
    end
  end
end

Public Instance Methods

data_width() click to toggle source
# File lib/rubypivot/spread_table.rb, line 70
def data_width
  @line_data.size
end
line_data_formatted() click to toggle source
# File lib/rubypivot/spread_table.rb, line 109
def line_data_formatted
  return @line_data if @line_type == :header || @attribute[:data_format].nil?
  res = []
  @line_data.each do |data|
    res << @attribute[:data_format] % data
  end
  res
end
make_col_class(options = {}) click to toggle source
# File lib/rubypivot/spread_table.rb, line 149
def make_col_class(options = {})
  klass = "col"
  klass << "-#{@attribute[:line_class]}" if @attribute[:line_class]
  klass << "-#{options[:width]}" if options[:width]
  # klass << " #{options[:klass]}" if options[:klass]
  klass
end
set_cell_callback(callback) click to toggle source
# File lib/rubypivot/spread_table.rb, line 100
def set_cell_callback(callback)
  return if callback.nil? || !callback.is_a?(Method) 
  if @line_type == :header
    @attribute[:cell_callback] = callback
  else
    @attribute[:cell_callback] = callback
  end
end
set_cell_class(callback) click to toggle source
# File lib/rubypivot/spread_table.rb, line 86
def set_cell_class(callback)
  return unless callback
  if callback.is_a?(Method)
    @line_data.each_with_index do |cell_data, idx|
      klass = callback.call(cell_data, @title)
      @attribs[idx] = klass if klass
    end
  else
    @line_data.each_with_index do |cell_data, idx|
      @attribs[idx] = callback.to_s
    end
  end
end
set_line_class(klass) click to toggle source
# File lib/rubypivot/spread_table.rb, line 74
def set_line_class(klass)
  @attribute[:line_class] = klass
end
set_line_type(line_type, options = {}) click to toggle source
# File lib/rubypivot/spread_table.rb, line 53
def set_line_type(line_type, options = {})
  @line_type = line_type
  @line_type = :data unless LINE_TYPES[@line_type] # at least vaid must be set
  if @line_type == :data
    @attribute[:line_class] = options[:data_line_class] if options[:data_line_class]
    @attribute[:title_class] = options[:data_title_class] if options[:data_title_class]
  else
    @attribute[:line_class] = options[:header_line_class] if options[:header_line_class]
    @attribute[:title_class] = options[:header_title_class] if options[:header_title_class]
  end
  if @line_type == :header
    #
  else
    @attribute[:data_format] = options[:data_format] if options[:data_format]
  end
end
set_td_class(klass) click to toggle source
# File lib/rubypivot/spread_table.rb, line 78
def set_td_class(klass)
  @attribute[:cell_class] = klass
end
set_title_class(klass) click to toggle source
# File lib/rubypivot/spread_table.rb, line 82
def set_title_class(klass)
  @attribute[:cell_class] = klass
end
to_grid(framework = :bootstrap, widths = [], options = {}) click to toggle source
# File lib/rubypivot/spread_table.rb, line 157
def to_grid(framework = :bootstrap, widths = [], options = {})
  res = "<div class=\"row\">"
  if @title
    div = Rubypivot::HtmlTag.new('div', class: make_col_class(width: widths[0])) # klass: @attribute[:title_class]
    res << div.build{ @title } 
  end
  line_data_formatted.each_with_index do |cell_data, idx|
    div = Rubypivot::HtmlTag.new('div', class: make_col_class(width: widths[idx + 1])) # klass: @attribute[:title_class]
    res << div.build{ cell_data }
  end
  res << "</div>/n"
  res
end
to_html(options = {}) click to toggle source
# File lib/rubypivot/spread_table.rb, line 129
def to_html(options = {})
  tr = Rubypivot::HtmlTag.new('tr', class: @attribute[:line_class])
  td_str = ""
  if @title
    td = Rubypivot::HtmlTag.new('td', class: @attribute[:title_class] || @attribute[:cell_class])
    td_str << td.build{ @title } 
  end
  line_data_formatted.each_with_index do |cell_data, idx|
    if @attribute[:cell_callback]
      td_str << @attribute[:cell_callback].call(cell_data, @title)
    else
      td = Rubypivot::HtmlTag.new('td', class: @attribs[idx])
      td_str << td.build{ cell_data }
    end
  end
  res = tr.build { td_str }
  res << "\n" if options[:line_end] == :cr
  res
end
to_s() click to toggle source
# File lib/rubypivot/spread_table.rb, line 118
def to_s
  res = ""
  res << "#{LINE_TYPES[@line_type]}: "
  res << "#{@title}: " if @title
  res << line_data_formatted.join(', ')
  res << " : Line Class: #{@attribute[:line_class]}"
  res << " : Cell Class: #{@attribute[:cell_class]}"
  res << " : Title Class: #{@attribute[:title_class]}"
  res
end