Sha256: 48fb51e0c1240ccf52db3d8fcd1e81b53b90a306076da073064b76a4a9bd5e2f

Contents?: true

Size: 901 Bytes

Versions: 2

Compression:

Stored size: 901 Bytes

Contents

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

module TTY
  class Table

    # A class for transforming table values
    class Transformation

      # Extract the header and row tuples from the value
      #
      # @param [Array] args
      #
      # @return [Object]
      #
      # @api public
      def self.extract_tuples(args)
        rows   = args.pop
        header = args.size.zero? ? nil : args.first
        if rows.first.is_a?(Hash)
          header, rows = group_header_and_rows(rows)
        end
        { header: header, rows: rows }
      end

      # Group hash keys into header and values into rows
      #
      # @params [Hash] value
      #
      # @api public
      def self.group_header_and_rows(value)
        header = value.map(&:keys).flatten.uniq
        rows   = value.inject([]) { |arr, el| arr + el.values }
        [header, rows]
      end

    end # Transformation
  end # Table
end # TTY

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tty-0.0.11 lib/tty/table/transformation.rb
tty-0.0.10 lib/tty/table/transformation.rb