Class: Yummi::Table
- Inherits:
-
Object
- Object
- Yummi::Table
- 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
-
#aliases ⇒ Object
Aliases that can be used by formatters and colorizers instead of numeric indexes.
-
#colspan ⇒ Object
The table colspan.
-
#default_align ⇒ Object
Default align.
-
#description ⇒ Object
The table description.
-
#header ⇒ Object
The table header.
-
#layout ⇒ Object
The table layout (horizontal or vertical).
-
#style ⇒ Object
The table colors.
-
#title ⇒ Object
The table title.
Instance Method Summary collapse
-
#add(row) ⇒ Object
(also: #<<)
Adds the given data as a row.
-
#align(indexes, type) ⇒ Object
Sets the align for a column in the table.
-
#bottom(params = {}, &block) ⇒ Object
Groups definitions for a specified group of rows at the bottom of the table.
-
#colorize(indexes, params = {}, &block) ⇒ Object
Sets a component to colorize a column.
-
#colorize_null(params = {}, &block) ⇒ Object
Defines a colorizer to null values.
-
#colorize_row(params = nil, &block) ⇒ Object
Adds a component to colorize the entire row (overrides column color).
-
#column(index) ⇒ Object
Retrieves the column at the given index.
-
#data=(data) ⇒ Object
Sets the table data.
-
#format(indexes, params = {}, &block) ⇒ Object
Sets a component to format a column.
-
#format_null(params = {}, &block) ⇒ Object
Defines a formatter to null values.
-
#initialize(params = {}) ⇒ Table
constructor
Creates a new table.
-
#no_colors ⇒ Object
Indicates that the table should not use colors.
-
#print(to = $stdout) ⇒ Object
Prints the #to_s into the given object.
-
#row(index) ⇒ Object
Retrieves the row at the given index.
-
#to_s ⇒ Object
Return a colorized and formatted table.
-
#top(params = {}, &block) ⇒ Object
Groups definitions for a specified group of rows at the top of the table.
-
#width ⇒ Object
Calculates the table width using the rendered lines.
Methods included from RowExtractor
#extract_row_from_array, #extract_row_from_hash, #extract_row_from_object, #extract_row_from_range
Methods included from OnBox
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
#aliases ⇒ Object
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 |
#colspan ⇒ Object
The table colspan
42 43 44 |
# File 'lib/yummi/table.rb', line 42 def colspan @colspan end |
#default_align ⇒ Object
Default align. #Yummi#Aligner should respond to it.
37 38 39 |
# File 'lib/yummi/table.rb', line 37 def default_align @default_align end |
#description ⇒ Object
The table description
35 36 37 |
# File 'lib/yummi/table.rb', line 35 def description @description end |
#header ⇒ Object
The table header
54 55 56 |
# File 'lib/yummi/table.rb', line 54 def header @header end |
#layout ⇒ Object
The table layout (horizontal or vertical)
52 53 54 |
# File 'lib/yummi/table.rb', line 52 def layout @layout end |
#style ⇒ Object
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 |
#title ⇒ Object
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_colors ⇒ Object
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 |
#print(to = $stdout) ⇒ Object
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_s ⇒ Object
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 |
#width ⇒ Object
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 |