Sha256: aa1e11118fa17da1d30c06a42da4eded60ba9e774a1517533d30c1e12a814f0c

Contents?: true

Size: 1.85 KB

Versions: 5

Compression:

Stored size: 1.85 KB

Contents

# encoding: utf-8

module TTY
  class Table
    # A class holding table field operations.
    #
    # @api private
    class Operations
      # Initialize Operations
      #
      # @param [TTY::Table] table
      #   the table to perform operations on
      #
      # @return [Object]
      #
      # @api public
      def initialize(table)
        @table      = table
        @operations = Hash.new { |hash, key| hash[key] = [] }
      end

      # Add operation
      #
      # @param [Symbol] operation_type
      #   the operation type
      # @param [Object] object
      #   the callable object
      #
      # @return [Hash]
      #
      # @api public
      def add(operation_type, object)
        operations[operation_type] << object
      end

      # Lookup operation
      #
      # @param [Symbol] operation
      #
      # @return [Object]
      #   the operation
      #
      # @api public
      def [](operation)
        operations[operation]
      end

      # Apply operations to a table data
      #
      # @param [Array[Symbol]] types
      #  the operation types
      # @param [Hash] options
      #   the options for the row
      #
      # @return [TTY::Table]
      #
      # @api public
      def run_operations(*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.call(field, row_i, col_i)
              end
            end
          end
        end
      end

      protected

      # The table
      #
      # @api private
      attr_reader :table

      # Available operations
      #
      # @return [Hash]
      #
      # @api public
      attr_reader :operations
    end # Operations
  end # Table
end # TTY

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
tty-table-0.9.0 lib/tty/table/operations.rb
tty-table-0.8.0 lib/tty/table/operations.rb
tty-table-0.7.0 lib/tty/table/operations.rb
tty-table-0.6.0 lib/tty/table/operations.rb
tty-table-0.5.0 lib/tty/table/operations.rb