Sha256: b08fd100b65ca22c25704ef15b94338cd6e9ba6588686bc7ff16d9b73374a326

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

# -*- encoding: utf-8 -*-

module TTY
  class Table

    # A class holding table field operations
    class Operations

      # The table
      #
      # @api private
      attr_reader :table
      private :table

      # Initialize Operations
      #
      # @param [TTY::Table] table
      #   the table to perform operations on
      #
      # @return [Object]
      #
      # @api public
      def initialize(table)
        @table = table
      end

      # Available operations
      #
      # @return [Hash]
      #
      # @api public
      def operations
        @operations ||= Hash.new { |hash, key| hash[key] = [] }
      end

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

      # Apply operations to a table row
      #
      # @param [Array[Symbol]] types
      #  the operation types
      # @param [Hash] options
      #   the options for the row
      #
      # @return [TTY::Table]
      #
      # @api public
      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

    end # Operations
  end # Table
end # TTY

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tty-0.0.10 lib/tty/table/operations.rb