Class: TTY::Table::Operations Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

A class holding table field operations.

Instance Method Summary collapse

Constructor Details

#initializeOperations

Initialize Operations



12
13
14
# File 'lib/tty/table/operations.rb', line 12

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

Instance Method Details

#[](operation) ⇒ Object

Lookup operation

Parameters:

  • operation (Symbol)

Returns:

  • (Object)

    the operation



38
39
40
# File 'lib/tty/table/operations.rb', line 38

def [](operation)
  operations[operation]
end

#add(operation_type, object) ⇒ Hash

Add operation

Parameters:

  • operation_type (Symbol)

    the operation type

  • object (Object)

    the callable object

Returns:

  • (Hash)


26
27
28
# File 'lib/tty/table/operations.rb', line 26

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

#apply_to(table, *args) ⇒ TTY::Table

Apply operations to a table data

Parameters:

  • types (Array[Symbol])

    the operation types

  • options (Hash)

    the options for the row

Returns:



52
53
54
55
56
57
58
59
60
61
62
63
64
# 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