class TTY::Table::Operations

A class holding table field operations.

@api private

Attributes

operations[R]

Available operations

@return [Hash]

@api public

Public Class Methods

new() click to toggle source

Initialize Operations

@api public

# File lib/tty/table/operations.rb, line 12
def initialize
  @operations = Hash.new { |hash, key| hash[key] = [] }
end

Public Instance Methods

[](operation) click to toggle source

Lookup operation

@param [Symbol] operation

@return [Object]

the operation

@api public

# File lib/tty/table/operations.rb, line 38
def [](operation)
  operations[operation]
end
add(operation_type, object) click to toggle source

Add operation

@param [Symbol] operation_type

the operation type

@param [Object] object

the callable object

@return [Hash]

@api public

# File lib/tty/table/operations.rb, line 26
def add(operation_type, object)
  operations[operation_type] << object
end
apply_to(table, *args) click to toggle source

Apply operations to a table data

@param [Array] types

the operation types

@param [Hash] options

the options for the row

@return [TTY::Table]

@api public

# File lib/tty/table/operations.rb, line 52
def apply_to(table, *args)
  operation_types = args
  table.data.each_with_index do |row, row_i|
    row.fields.each_with_index do |field, col_i|
      field.reset!
      operation_types.each do |type|
        operations[type].each do |operation|
          field.content = operation.(field, row_i, col_i)
        end
      end
    end
  end
end