class Shellout::MenuQuery
Public Class Methods
new(items, last_is_the_zero_item=false)
click to toggle source
# File lib/shellout/menu_query.rb, line 9 def initialize(items, last_is_the_zero_item=false) @items = items.is_a?(Array) ? Hash[*(items.zip(items).flatten)] : items @has_a_zero_item = last_is_the_zero_item initialize_menu @prompt_class = Query end
Public Instance Methods
ask()
click to toggle source
# File lib/shellout/menu_query.rb, line 29 def ask return @items.values[0] if only_one_option? answer = @prompt_class.new('', 1).call.to_i return nil if answer < 0 return nil if answer == 0 and !@has_a_zero_item index = (@has_a_zero_item and answer == "0") ? -1 : answer.to_i-1 @items.values[index] end
call()
click to toggle source
# File lib/shellout/menu_query.rb, line 16 def call loop do @menu.print answer = ask return answer if answer puts "Invalid user input!" end end
prompt_class=(klass)
click to toggle source
# File lib/shellout/menu_query.rb, line 25 def prompt_class=(klass) @prompt_class = klass end
Private Instance Methods
only_one_option?()
click to toggle source
# File lib/shellout/menu_query.rb, line 46 def only_one_option? @items.length == 1 && !@has_a_zero_item end