Class: Yummi::Table
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 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
70 71 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 |
# File 'lib/yummi/table.rb', line 70 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
38 39 40 |
# File 'lib/yummi/table.rb', line 38 def aliases @aliases end |
#colspan ⇒ Object
The table colspan
40 41 42 |
# File 'lib/yummi/table.rb', line 40 def colspan @colspan end |
#default_align ⇒ Object
Default align. #Yummi#Aligner should respond to it.
35 36 37 |
# File 'lib/yummi/table.rb', line 35 def default_align @default_align end |
#description ⇒ Object
The table description
33 34 35 |
# File 'lib/yummi/table.rb', line 33 def description @description end |
#header ⇒ Object
The table header
52 53 54 |
# File 'lib/yummi/table.rb', line 52 def header @header end |
#layout ⇒ Object
The table layout (horizontal or vertical)
50 51 52 |
# File 'lib/yummi/table.rb', line 50 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
48 49 50 |
# File 'lib/yummi/table.rb', line 48 def style @style end |
#title ⇒ Object
The table title
31 32 33 |
# File 'lib/yummi/table.rb', line 31 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.
253 254 255 |
# File 'lib/yummi/table.rb', line 253 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
222 223 224 225 226 227 228 |
# File 'lib/yummi/table.rb', line 222 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 }
126 127 128 129 |
# File 'lib/yummi/table.rb', line 126 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 }
281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/yummi/table.rb', line 281 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
303 304 305 306 307 308 |
# File 'lib/yummi/table.rb', line 303 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 }
239 240 241 242 |
# File 'lib/yummi/table.rb', line 239 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
172 173 174 175 176 177 178 179 |
# File 'lib/yummi/table.rb', line 172 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
245 246 247 |
# File 'lib/yummi/table.rb', line 245 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'
331 332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'lib/yummi/table.rb', line 331 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
355 356 357 358 359 360 |
# File 'lib/yummi/table.rb', line 355 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.
99 100 101 102 103 104 105 106 |
# File 'lib/yummi/table.rb', line 99 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.
365 366 367 |
# File 'lib/yummi/table.rb', line 365 def print (to = $stdout) to.print to_s end |
#row(index) ⇒ Object
Retrieves the row at the given index
167 168 169 |
# File 'lib/yummi/table.rb', line 167 def row(index) @data[index] end |
#to_s ⇒ Object
Return a colorized and formatted table.
372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'lib/yummi/table.rb', line 372 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 }
149 150 151 |
# File 'lib/yummi/table.rb', line 149 def top params = {}, &block _context_ 0, params, &block end |
#width ⇒ Object
Calculates the table width using the rendered lines
390 391 392 393 394 395 396 397 |
# File 'lib/yummi/table.rb', line 390 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 |