Class: Shellout::MenuQuery

Inherits:
Object
  • Object
show all
Includes:
Shellout
Defined in:
lib/shellout/menu_query.rb

Constant Summary

Constants included from Shellout

VERSION

Instance Method Summary collapse

Methods included from Shellout

#Calendar, #Menu, #Shadowbox, #Table

Constructor Details

#initialize(items, last_is_the_zero_item = false) ⇒ MenuQuery

Returns a new instance of MenuQuery.



9
10
11
12
13
14
# 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

Instance Method Details

#askObject



29
30
31
32
33
34
35
36
# 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

#callObject



16
17
18
19
20
21
22
23
# 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) ⇒ Object



25
26
27
# File 'lib/shellout/menu_query.rb', line 25

def prompt_class=(klass)
  @prompt_class = klass
end