Class: Soyuz::CommandChoice

Inherits:
Object
  • Object
show all
Defined in:
lib/soyuz/command_choice.rb

Instance Method Summary collapse

Constructor Details

#initialize(choices) ⇒ CommandChoice

Returns a new instance of CommandChoice.

Raises:

  • (ArgumentError)


6
7
8
9
10
11
# 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
  @default_choice = default_choice_number
  raise ArgumentError, "Default choice required for non interactive mode." if @default_choice.nil? && $non_interactive
end

Instance Method Details

#build_command(choice) ⇒ Object



32
33
34
# File 'lib/soyuz/command_choice.rb', line 32

def build_command(choice)
  Command.build(@choices[choice][:cmd]).run
end

#default_choice_numberObject



13
14
15
16
17
18
# File 'lib/soyuz/command_choice.rb', line 13

def default_choice_number
  @choices.each_with_index do |choice, index|
    return index+1 if choice[:default]
  end
  nil
end

#runObject



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/soyuz/command_choice.rb', line 20

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