Sha256: 8bccb6bff3f08b09cb7f62cb35dbeeccd412d4e3309a604df67cb15c74bc35b9

Contents?: true

Size: 852 Bytes

Versions: 3

Compression:

Stored size: 852 Bytes

Contents

# Aruba
module Aruba
  # Platforms
  module Platforms
    # Generate simple table
    class SimpleTable
      private

      attr_reader :hash, :opts

      public

      # Create
      #
      # @param [Hash] hash
      #   Input
      def initialize(hash, opts)
        @hash = hash
        @opts = {
          sort: true
        }.merge opts
      end

      # Generate table
      #
      # @return [String]
      #   The table
      def to_s
        longest_key = hash.keys.map(&:to_s).max_by(&:length)
        return '' if longest_key.nil?

        name_size = longest_key.length

        rows = hash.each_with_object([]) do |(k, v), a|
          a << format("# %-#{name_size}s => %s", k, v)
        end

        if opts[:sort] == true
          rows.sort.join("\n")
        else
          rows.join("\n")
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
aruba-1.0.1 lib/aruba/platforms/simple_table.rb
aruba-1.0.0 lib/aruba/platforms/simple_table.rb
aruba-1.0.0.pre.alpha.5 lib/aruba/platforms/simple_table.rb