Class: Vedeu::Menu

Inherits:
Object
  • Object
show all
Defined in:
lib/vedeu/support/menu.rb

Instance Method Summary collapse

Constructor Details

#initialize(collection) ⇒ Menu

Returns a new instance of Menu.



3
4
5
6
7
8
# File 'lib/vedeu/support/menu.rb', line 3

def initialize(collection)
  @collection = collection
  @current    = 0
  @selected   = nil
  @events     = events
end

Instance Method Details

#bottom_itemObject



73
74
75
76
77
# File 'lib/vedeu/support/menu.rb', line 73

def bottom_item
  @current = last

  items
end

#currentObject



25
26
27
# File 'lib/vedeu/support/menu.rb', line 25

def current
  @current
end

#current_itemObject



33
34
35
# File 'lib/vedeu/support/menu.rb', line 33

def current_item
  @collection[@current]
end

#deselect_itemObject



97
98
99
100
101
# File 'lib/vedeu/support/menu.rb', line 97

def deselect_item
  @selected = nil

  items
end

#eventsObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/vedeu/support/menu.rb', line 10

def events
  @_events ||= Vedeu.events.add(self) do
    event(:menu_next)     { next_item     }
    event(:menu_prev)     { prev_item     }
    event(:menu_top)      { top_item      }
    event(:menu_bottom)   { bottom_item   }
    event(:menu_select)   { select_item   }
    event(:menu_deselect) { deselect_item }

    event(:menu_selected) { selected_item }
    event(:menu_current)  { current_item  }
    event(:menu_items)    { items         }
  end
end

#itemsObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/vedeu/support/menu.rb', line 43

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

#lastObject



103
104
105
# File 'lib/vedeu/support/menu.rb', line 103

def last
  @collection.size - 1
end

#next_itemObject



79
80
81
82
83
# File 'lib/vedeu/support/menu.rb', line 79

def next_item
  @current += 1 if @current < last

  items
end

#prev_itemObject



85
86
87
88
89
# File 'lib/vedeu/support/menu.rb', line 85

def prev_item
  @current -= 1 if @current > 0

  items
end

#select_itemObject



91
92
93
94
95
# File 'lib/vedeu/support/menu.rb', line 91

def select_item
  @selected = @current

  items
end

#selectedObject



29
30
31
# File 'lib/vedeu/support/menu.rb', line 29

def selected
  @selected
end

#selected_itemObject



37
38
39
40
41
# File 'lib/vedeu/support/menu.rb', line 37

def selected_item
  return nil unless @selected

  @collection[@selected]
end

#sizeObject



107
108
109
# File 'lib/vedeu/support/menu.rb', line 107

def size
  @collection.size
end

#top_itemObject



67
68
69
70
71
# File 'lib/vedeu/support/menu.rb', line 67

def top_item
  @current = 0

  items
end

#viewObject



63
64
65
# File 'lib/vedeu/support/menu.rb', line 63

def view
  items[@current, @collection.size]
end