Sha256: fb6b15a373fccc197f808ad1b9607f26eea5d14d2e8c6d9c1a552e64c06bcb92

Contents?: true

Size: 722 Bytes

Versions: 4

Compression:

Stored size: 722 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, 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

4 entries across 4 versions & 1 rubygems

Version Path
cp8_cli-4.1.2 lib/cp8_cli/table.rb
cp8_cli-4.1.1 lib/cp8_cli/table.rb
cp8_cli-4.1.0 lib/cp8_cli/table.rb
cp8_cli-4.0.1 lib/cp8_cli/table.rb