Class: Vnehm::UI::Menu
- Inherits:
-
Object
- Object
- Vnehm::UI::Menu
- Defined in:
- lib/vnehm/menu.rb
Instance Method Summary collapse
- #call_selected_block(selected) ⇒ Object
- #choice(index, desc, &block) ⇒ Object
- #header=(value) ⇒ Object
-
#initialize {|_self| ... } ⇒ Menu
constructor
A new instance of Menu.
- #newline ⇒ Object
- #select ⇒ Object
Constructor Details
#initialize {|_self| ... } ⇒ Menu
Returns a new instance of Menu.
5 6 7 8 9 10 11 12 |
# File 'lib/vnehm/menu.rb', line 5 def initialize @choices = {} @inc_index = 1 @items = [] yield self select end |
Instance Method Details
#call_selected_block(selected) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/vnehm/menu.rb', line 64 def call_selected_block(selected) loop do if @choices.keys.include? selected block = @choices[selected] block.call unless block.nil? break else selected = UI.ask "Вы должны выбрать одно из [#{@choices.keys.join(', ')}]" end end end |
#choice(index, desc, &block) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/vnehm/menu.rb', line 14 def choice(index, desc, &block) # Visual index - index that you see in menu # Select index - index than can be selected # For example, if you use ':added' index # In menu you see 'A', but you can select it by number # You receive a warning though visual_index = select_index = index if index == :inc visual_index = select_index = @inc_index.to_s @inc_index += 1 end if index == :added visual_index = 'A'.green select_index = @inc_index.to_s @inc_index += 1 end @choices[select_index] = block @items << "#{visual_index} #{desc}" end |
#header=(value) ⇒ Object
38 39 40 |
# File 'lib/vnehm/menu.rb', line 38 def header=(value) @items.unshift(value) end |
#newline ⇒ Object
42 43 44 |
# File 'lib/vnehm/menu.rb', line 42 def newline @items << "\n" end |
#select ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/vnehm/menu.rb', line 46 def select # Add exit option newline choice('e', 'Выйти'.red) { UI.term } # Output items @items.each do |item| UI.say item end UI.newline selected = UI.ask('Введите опцию:'.yellow.freeze) call_selected_block(selected) UI.newline end |