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, renderer = nil) ⇒ Object

Initialize Operations

Parameters:

  • table (TTY::Table)

    the table to perform operations on



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

def initialize(table, renderer=nil)
  @table = table
  @renderer = renderer # will be available to each operation
end

Instance Method Details

#add(type, object) ⇒ Hash

Add operation

Parameters:

  • type (Symbol)

    the operation type

  • object (Object)

    the callable object

Returns:

  • (Hash)


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

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

#operationsHash

Available operations

Returns:

  • (Hash)


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

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

#run_operations(*args) ⇒ TTY::Table

Apply operations to a table row

Parameters:

  • types (Array[Symbol])

    the operation types

  • options (Hash)

    the options for the row

Returns:



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

def run_operations(*args)
  types = args
  table.each_with_index do |val, row, col|
    types.each do |type|
      operations[type].each { |op| op.call(val, row, col) }
    end
  end
end