class Soyuz::CommandChoice

Public Class Methods

new(choices) click to toggle source
# File lib/soyuz/command_choice.rb, line 6
def initialize(choices)
  raise ArgumentError, "Choices must be an array" unless choices.is_a?(Array)
  @choices = choices

  if $non_interactive
    @default_choice = default_choice_number
    raise ArgumentError, "Default choice required for non interactive mode." if @default_choice.nil?
  end
end

Public Instance Methods

build_command(choice) click to toggle source
# File lib/soyuz/command_choice.rb, line 35
def build_command(choice)
  Command.build(@choices[choice][:cmd]).run
end
default_choice_number() click to toggle source
# File lib/soyuz/command_choice.rb, line 16
def default_choice_number
  @choices.each_with_index do |choice, index|
    return index+1 if choice[:default]
  end
  nil
end
run() click to toggle source
# File lib/soyuz/command_choice.rb, line 23
def run
  if @default_choice.nil?
    @choices.each_with_index do |choice, index|
      say "#{index+1}) #{choice[:display]}"
    end
    choice = ask("? ", Integer) { |q| q.in = 1..@choices.length }
  else
    choice = default_choice_number
  end
  build_command(choice-1)
end