Class: Yummi::Table

Inherits:
Object
  • Object
show all
Includes:
OnBox, RowExtractor
Defined in:
lib/yummi/table.rb

Overview

A Table that supports colorizing title, header, values and also formatting the values.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RowExtractor

#extract_row_from_array, #extract_row_from_hash, #extract_row_from_object, #extract_row_from_range

Methods included from OnBox

#on_box

Constructor Details

#initialize(params = {}) ⇒ Table

Creates a new table. A hash containing the style properties may be given to override the defaults.

  • Title (title): none

  • Description (description): none

  • Header (header): none

Hash in “style” key:

  • Title color (title): bold.yellow

  • Header color (header): bold.blue

  • Values color (color): none

  • Colspan (colspan): 2

  • Default Align (align): right and first element to left



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/yummi/table.rb', line 72

def initialize(params = {})
  params = OpenStruct::new params
  params.style ||= {}
  @data = []
  @header = []
  @title = (params.title or nil)
  @description = (params.description or nil)
  @style = {
    :title => (params.style[:title] or 'bold.yellow'),
    :description => (params.style[:description] or 'bold.black'),
    :header => (params.style[:header] or 'bold.blue'),
    :value => (params.style[:color] or nil)
  }

  @colspan = (params.colspan or 2)
  @layout = (params.layout or :horizontal)
  @default_align = (params.align or :right)
  @aliases = []

  @align = [:left]
  @components = {}
  @contexts = [:default]
  _define_ :default
  @current_context = :default

  self.header = params.header if params.header
end

Instance Attribute Details

#aliasesObject

Aliases that can be used by formatters and colorizers instead of numeric indexes. The aliases are directed mapped to their respective index in this array



40
41
42
# File 'lib/yummi/table.rb', line 40

def aliases
  @aliases
end

#colspanObject

The table colspan



42
43
44
# File 'lib/yummi/table.rb', line 42

def colspan
  @colspan
end

#default_alignObject

Default align. #Yummi#Aligner should respond to it.



37
38
39
# File 'lib/yummi/table.rb', line 37

def default_align
  @default_align
end

#descriptionObject

The table description



35
36
37
# File 'lib/yummi/table.rb', line 35

def description
  @description
end

#headerObject

The table header



54
55
56
# File 'lib/yummi/table.rb', line 54

def header
  @header
end

#layoutObject

The table layout (horizontal or vertical)



52
53
54
# File 'lib/yummi/table.rb', line 52

def layout
  @layout
end

#styleObject

The table colors. This Map should have colors for the following elements:

  • Title: using :title key

  • Header: using :header key

  • Values: using :value key

The colors must be supported by #Yummi#Color#parse or defined in #Yummi#Color#COLORS



50
51
52
# File 'lib/yummi/table.rb', line 50

def style
  @style
end

#titleObject

The table title



33
34
35
# File 'lib/yummi/table.rb', line 33

def title
  @title
end

Instance Method Details

#add(row) ⇒ Object Also known as: <<

Adds the given data as a row. If the argument is a hash, its keys will be used to match header alias for building the row data.



255
256
257
# File 'lib/yummi/table.rb', line 255

def add(row)
  @data << row
end

#align(indexes, type) ⇒ Object

Sets the align for a column in the table. #Yummi#Aligner should respond to it.

Args

index

The column indexes or its aliases

type

The alignment type

Example

table.align :description, :left
table.align [:value, :total], :right


224
225
226
227
228
229
230
# File 'lib/yummi/table.rb', line 224

def align(indexes, type)
  [*indexes].each do |index|
    index = parse_index(index)
    raise Exception::new "Undefined column #{index}" unless index
    @align[index] = type
  end
end

#bottom(params = {}, &block) ⇒ Object

Groups definitions for a specified group of rows at the bottom of the table. Every customization can be used (formatter/colorizer for null values, for rows and columns). Customizations must be done in the given block.

Subsequent calls to this method creates different groups.

Args

+:rows+:: The number of rows to group using the customizations in the block

Examples

table.bottom :rows => 3 do
  table.colorize :subtotal, :with => :green
  table.format :approved, :using => Yummi::Formatters::boolean
end
table.bottom { table.colorize :total, :with => :white }


128
129
130
131
# File 'lib/yummi/table.rb', line 128

def bottom(params = {}, &block)
  index = @contexts.size
  _context_ index, params, &block
end

#colorize(indexes, params = {}, &block) ⇒ Object

Sets a component to colorize a column.

The component must respond to call with the column value (or row if used with #using_row) as the arguments and return a color or nil if default color should be used. A block can also be used.

Args

indexes

The column indexes or its aliases

params

A hash with params in case a block is not given:

- :using defines the component to use
- :with defines the color to use (to use the same color for all columns)

Example

table.colorize :description, :with => :magenta
table.colorize([:value, :total]) { |value| :red if value < 0 }


283
284
285
286
287
288
289
290
291
292
293
# File 'lib/yummi/table.rb', line 283

def colorize(indexes, params = {}, &block)
  [*indexes].each do |index|
    index = parse_index(index)
    if index
      obj = extract_component(params, &block)
      component[:colorizers][index] = obj
    else
      colorize_null params, &block
    end
  end
end

#colorize_null(params = {}, &block) ⇒ Object

Defines a colorizer to null values.

Args

params

A hash with params in case a block is not given:

- :using defines the component to use
- :with defines the format to use


305
306
307
308
309
310
# File 'lib/yummi/table.rb', line 305

def colorize_null(params = {}, &block)
  component[:null_colorizer] = (params[:using] or block)
  component[:null_colorizer] ||= proc do |value|
    params[:with]
  end
end

#colorize_row(params = nil, &block) ⇒ Object

Adds a component to colorize the entire row (overrides column color). The component must respond to call with the index and the row as the arguments and return a color or nil if default color should be used. A block can also be used.

Example

table.colorize_row { |i, row| :red if row[:value] < 0 }


241
242
243
244
# File 'lib/yummi/table.rb', line 241

def colorize_row(params = nil, &block)
  obj = extract_component(params, &block)
  component[:row_colorizer] = obj
end

#column(index) ⇒ Object

Retrieves the column at the given index. Aliases can be used



174
175
176
177
178
179
180
181
# File 'lib/yummi/table.rb', line 174

def column(index)
  index = parse_index(index)
  columns = []
  @data.each do |row|
    columns << row_to_array(row)[index].value
  end
  columns
end

#data=(data) ⇒ Object

Sets the table data



247
248
249
# File 'lib/yummi/table.rb', line 247

def data=(data)
  @data = data
end

#format(indexes, params = {}, &block) ⇒ Object

Sets a component to format a column.

The component must respond to call with the column value as the arguments and return a color or nil if default color should be used. A block can also be used.

Args

indexes

The column indexes or its aliases

params

A hash with params in case a block is not given:

- :using defines the component to use
- :with defines the format to use (to use the same format for all columns)

Example

table.format :value, :with => '%.2f'
table.format [:value, :total], :with => '%.2f'


333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/yummi/table.rb', line 333

def format(indexes, params = {}, &block)
  [*indexes].each do |index|
    index = parse_index(index)
    if index
      component[:formatters][index] = (params[:using] or block)
      component[:formatters][index] ||= proc do |ctx|
        params[:with] % ctx.value
      end
    else
      format_null params, &block
    end
  end
end

#format_null(params = {}, &block) ⇒ Object

Defines a formatter to null values.

Args

params

A hash with params in case a block is not given:

- :using defines the component to use
- :with defines the format to use


357
358
359
360
361
362
# File 'lib/yummi/table.rb', line 357

def format_null(params = {}, &block)
  component[:null_formatter] = (params[:using] or block)
  component[:null_formatter] ||= proc do |value|
    params[:with] % value
  end
end

#no_colorsObject

Indicates that the table should not use colors.



101
102
103
104
105
106
107
108
# File 'lib/yummi/table.rb', line 101

def no_colors
  @style = {
    :title => nil,
    :header => nil,
    :value => nil
  }
  @no_colors = true
end

Prints the #to_s into the given object.



367
368
369
# File 'lib/yummi/table.rb', line 367

def print(to = $stdout)
  to.print to_s
end

#row(index) ⇒ Object

Retrieves the row at the given index



169
170
171
# File 'lib/yummi/table.rb', line 169

def row(index)
  @data[index]
end

#to_sObject

Return a colorized and formatted table.



374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/yummi/table.rb', line 374

def to_s
  header_output = build_header_output
  data_output = build_data_output

  string = ''
  string << Yummi.colorize(@title, @style[:title]) << $/ if @title
  string << Yummi.colorize(@description, @style[:description]) << $/ if @description
  table_data = header_output + data_output
  if @layout == :vertical
    # don't use array transpose because the data may differ in each line size
    table_data = rotate table_data
  end
  string << content(table_data)
end

#top(params = {}, &block) ⇒ Object

Groups definitions for a specified group of rows at the top of the table. Every customization can be used (formatter/colorizer for null values, for rows and columns). Customizations must be done in the given block.

Subsequent calls to this method creates different groups.

Args

+:rows+:: The number of rows to group using the customizations in the block

Examples

table.top :rows => 3 do
  table.colorize :subtotal, :with => :green
  table.format :approved, :using => Yummi::Formatters::boolean
end
table.top { table.colorize :total, :with => :white }


151
152
153
# File 'lib/yummi/table.rb', line 151

def top(params = {}, &block)
  _context_ 0, params, &block
end

#widthObject

Calculates the table width using the rendered lines



392
393
394
395
396
397
398
399
# File 'lib/yummi/table.rb', line 392

def width
  string = to_s
  max_width = 0
  string.each_line do |line|
    max_width = [max_width, line.uncolored.chomp.size].max
  end
  max_width
end