Class: Ctrl::InstanceSelector

Inherits:
Object
  • Object
show all
Includes:
Virtus::Model
Defined in:
lib/ctrl/instance_selector.rb

Instance Method Summary collapse

Instance Method Details

#display_optionsObject



7
8
9
10
11
12
13
# File 'lib/ctrl/instance_selector.rb', line 7

def display_options
  puts "Instances"
  instances.each_with_index do |instance, i|
    puts ("%2d %s" % [i + 1, instance.to_s])
  end
  puts "\nPlease select an instance"
end

#performObject



28
29
30
# File 'lib/ctrl/instance_selector.rb', line 28

def perform
  instances.size == 1 ? instances.first : selection
end

#selectionObject



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ctrl/instance_selector.rb', line 15

def selection
  return @selection if @selection

  loop do
    display_options
    selected_index = Readline.readline(">> ")
    @selection = instances[selected_index.to_i - 1]
    break if selected_index.to_i > 0 && @selection
  end

  @selection
end