Class: XMigra::Console::Menu
Instance Attribute Summary collapse
-
#prompt ⇒ Object
readonly
Returns the value of attribute prompt.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#title_rule ⇒ Object
readonly
Returns the value of attribute title_rule.
-
#title_width ⇒ Object
readonly
Returns the value of attribute title_width.
-
#trailing_newlines ⇒ Object
readonly
Returns the value of attribute trailing_newlines.
Instance Method Summary collapse
- #get_selection ⇒ Object
-
#initialize(title, options, prompt, opts = {}) ⇒ Menu
constructor
A new instance of Menu.
- #show_once ⇒ Object
Constructor Details
#initialize(title, options, prompt, opts = {}) ⇒ Menu
Returns a new instance of Menu.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/xmigra/console.rb', line 4 def initialize(title, , prompt, opts={}) @title = title @prompt = prompt @title_width = opts[:title_width] || 40 @title_rule = opts[:title_rule] || '=' @trailing_newlines = opts[:trailing_newlines] || 3 get_name = opts[:get_name] || lambda {|o| o.to_s} @name_map = {} @menu_map = {} .each_with_index do |opt, i| opt_name = get_name[opt] @name_map[opt_name] = opt @menu_map[i + 1] = opt_name end end |
Instance Attribute Details
#prompt ⇒ Object (readonly)
Returns the value of attribute prompt.
20 21 22 |
# File 'lib/xmigra/console.rb', line 20 def prompt @prompt end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
20 21 22 |
# File 'lib/xmigra/console.rb', line 20 def title @title end |
#title_rule ⇒ Object (readonly)
Returns the value of attribute title_rule.
20 21 22 |
# File 'lib/xmigra/console.rb', line 20 def title_rule @title_rule end |
#title_width ⇒ Object (readonly)
Returns the value of attribute title_width.
20 21 22 |
# File 'lib/xmigra/console.rb', line 20 def title_width @title_width end |
#trailing_newlines ⇒ Object (readonly)
Returns the value of attribute trailing_newlines.
20 21 22 |
# File 'lib/xmigra/console.rb', line 20 def trailing_newlines @trailing_newlines end |
Instance Method Details
#get_selection ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/xmigra/console.rb', line 44 def get_selection selection = nil loop do selection = show_once break unless selection.nil? puts "That input did not uniquely identify one of the available options." puts end @trailing_newlines.times {puts} return @name_map[selection] end |
#show_once ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/xmigra/console.rb', line 22 def show_once Console.output_section( title, :trailing_newlines => @trailing_newlines ) do @menu_map.each_pair do |item_num, name| puts "#{item_num.to_s.rjust(4)}. #{name}" end puts print prompt + ': ' user_choice = $stdin.gets.strip @menu_map[user_choice.to_i].tap do |mapped| return mapped unless mapped.nil? end return user_choice if @menu_map.values.include? user_choice by_prefix = @menu_map.values.select {|e| e.start_with? user_choice} return by_prefix[0] if by_prefix.length == 1 end return nil end |