Sha256: be82f0e92721994b03efe051f410a6db4bc1f8653f0db48e03ec9e03f8b586bb

Contents?: true

Size: 728 Bytes

Versions: 6

Compression:

Stored size: 728 Bytes

Contents

require "cp8_cli/table/row"

module Cp8Cli
  class Table
    def self.pick(records, caption: "Pick one:")
      new(records).pick(caption)
    end

    def initialize(records)
      @records = records.to_a.sort_by(&:position)
    end

    def pick(caption)
      return if records.none?
      render_table
      index = Command.ask(caption, type: Integer)
      records[index - 1]
    end

    private

      attr_reader :records

      def render_table
        if records.size > 0
          Command.table rows
        else
          Command.say "No records found"
        end
      end

      def rows
        records.each_with_index.map do |record, index|
          Row.new(record, index).to_h
        end
      end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
cp8_cli-6.0.1 lib/cp8_cli/table.rb
cp8_cli-6.0.0 lib/cp8_cli/table.rb
cp8_cli-5.0.0 lib/cp8_cli/table.rb
cp8_cli-4.2.1 lib/cp8_cli/table.rb
cp8_cli-4.2.0 lib/cp8_cli/table.rb
cp8_cli-4.1.3 lib/cp8_cli/table.rb