Class: Vedeu::Menu
- Inherits:
-
Object
- Object
- Vedeu::Menu
- Defined in:
- lib/vedeu/support/menu.rb
Instance Method Summary collapse
- #bottom_item ⇒ Object
- #current ⇒ Object
- #current_item ⇒ Object
- #deselect_item ⇒ Object
- #events ⇒ Object
-
#initialize(collection) ⇒ Menu
constructor
A new instance of Menu.
- #items ⇒ Object
- #last ⇒ Object
- #next_item ⇒ Object
- #prev_item ⇒ Object
- #render ⇒ Object
- #select_item ⇒ Object
- #selected ⇒ Object
- #selected_item ⇒ Object
- #size ⇒ Object
- #top_item ⇒ Object
Constructor Details
#initialize(collection) ⇒ Menu
Returns a new instance of Menu.
6 7 8 9 10 11 |
# File 'lib/vedeu/support/menu.rb', line 6 def initialize(collection) @collection = collection @current = 0 @selected = nil @events = events end |
Instance Method Details
#bottom_item ⇒ Object
93 94 95 96 97 |
# File 'lib/vedeu/support/menu.rb', line 93 def bottom_item @current = last items end |
#current ⇒ Object
29 30 31 |
# File 'lib/vedeu/support/menu.rb', line 29 def current @current end |
#current_item ⇒ Object
37 38 39 |
# File 'lib/vedeu/support/menu.rb', line 37 def current_item @collection[@current] end |
#deselect_item ⇒ Object
117 118 119 120 121 |
# File 'lib/vedeu/support/menu.rb', line 117 def deselect_item @selected = nil items end |
#events ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/vedeu/support/menu.rb', line 13 def events @_events ||= Vedeu.events.add(self) do on(:menu_next) { next_item } on(:menu_prev) { prev_item } on(:menu_top) { top_item } on(:menu_bottom) { bottom_item } on(:menu_select) { select_item } on(:menu_deselect) { deselect_item } on(:menu_selected) { selected_item } on(:menu_current) { current_item } on(:menu_items) { items } on(:menu_render) { render } end end |
#items ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/vedeu/support/menu.rb', line 47 def items items = [] @collection.each_with_index do |item, index| if index == @current && index == @selected items << [true, true, item] elsif index == @current items << [false, true, item] elsif index == @selected items << [true, false, item] else items << [false, false, item] end end items end |
#last ⇒ Object
123 124 125 |
# File 'lib/vedeu/support/menu.rb', line 123 def last @collection.size - 1 end |
#next_item ⇒ Object
99 100 101 102 103 |
# File 'lib/vedeu/support/menu.rb', line 99 def next_item @current += 1 if @current < last items end |
#prev_item ⇒ Object
105 106 107 108 109 |
# File 'lib/vedeu/support/menu.rb', line 105 def prev_item @current -= 1 if @current > 0 items end |
#render ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/vedeu/support/menu.rb', line 67 def render lines = [] items.each do |(sel, cur, item)| if sel && cur lines << "*> #{item}" elsif cur lines << " > #{item}" elsif sel lines << "* #{item}" else lines << " #{item}" end end lines end |
#select_item ⇒ Object
111 112 113 114 115 |
# File 'lib/vedeu/support/menu.rb', line 111 def select_item @selected = @current items end |
#selected ⇒ Object
33 34 35 |
# File 'lib/vedeu/support/menu.rb', line 33 def selected @selected end |
#selected_item ⇒ Object
41 42 43 44 45 |
# File 'lib/vedeu/support/menu.rb', line 41 def selected_item return nil unless @selected @collection[@selected] end |
#size ⇒ Object
127 128 129 |
# File 'lib/vedeu/support/menu.rb', line 127 def size @collection.size end |
#top_item ⇒ Object
87 88 89 90 91 |
# File 'lib/vedeu/support/menu.rb', line 87 def top_item @current = 0 items end |