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.



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_itemObject



93
94
95
96
97
# File 'lib/vedeu/support/menu.rb', line 93

def bottom_item
  @current = last

  items
end

#currentObject



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

def current
  @current
end

#current_itemObject



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

def current_item
  @collection[@current]
end

#deselect_itemObject



117
118
119
120
121
# File 'lib/vedeu/support/menu.rb', line 117

def deselect_item
  @selected = nil

  items
end

#eventsObject



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

#itemsObject



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

#lastObject



123
124
125
# File 'lib/vedeu/support/menu.rb', line 123

def last
  @collection.size - 1
end

#next_itemObject



99
100
101
102
103
# File 'lib/vedeu/support/menu.rb', line 99

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

  items
end

#prev_itemObject



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

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

  items
end

#renderObject



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_itemObject



111
112
113
114
115
# File 'lib/vedeu/support/menu.rb', line 111

def select_item
  @selected = @current

  items
end

#selectedObject



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

def selected
  @selected
end

#selected_itemObject



41
42
43
44
45
# File 'lib/vedeu/support/menu.rb', line 41

def selected_item
  return nil unless @selected

  @collection[@selected]
end

#sizeObject



127
128
129
# File 'lib/vedeu/support/menu.rb', line 127

def size
  @collection.size
end

#top_itemObject



87
88
89
90
91
# File 'lib/vedeu/support/menu.rb', line 87

def top_item
  @current = 0

  items
end