Class: TTY::Table::Operations

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/table/operations.rb

Overview

A class holding table field operations

Instance Method Summary collapse

Constructor Details

#initialize(table) ⇒ Object

Initialize Operations

Parameters:

  • table (TTY::Table)

    the table to perform operations on



23
24
25
# File 'lib/tty/table/operations.rb', line 23

def initialize(table)
  @table = table
end

Instance Method Details

#add_operation(type, object) ⇒ Hash

Add operation

Parameters:

  • type (Symbol)

    the operation type

  • object (Object)

    the callable object

Returns:

  • (Hash)


46
47
48
# File 'lib/tty/table/operations.rb', line 46

def add_operation(type, object)
  operations[type] << object
end

#operationsHash

Available operations

Returns:

  • (Hash)


32
33
34
# File 'lib/tty/table/operations.rb', line 32

def operations
  @operations ||= Hash.new { |hash, key| hash[key] = [] }
end

#run_operations(type, row, options = {}) ⇒ Hash

Apply operations to a table row

Parameters:

  • type (Symbol)

    the operation type

  • row (#to_ary, Row)

    the row to apply operation to

  • options (Hash) (defaults to: {})

    the options for the row

Returns:

  • (Hash)


62
63
64
65
# File 'lib/tty/table/operations.rb', line 62

def run_operations(type, row, options={})
  options.merge!(:column_widths => table.column_widths)
  operations[type].each { |op| op.call(row, options) }
end