Class: Vedeu::Menus::Menu
- Inherits:
-
Object
- Object
- Vedeu::Menus::Menu
- Includes:
- Vedeu::Model
- Defined in:
- lib/vedeu/menus/menu.rb
Overview
Converts the collection passed into a list of menu items which can be navigated using the instance methods or events provided.
Instance Attribute Summary collapse
- #collection ⇒ Array
-
#current ⇒ Fixnum
Returns the index of the value in the collection which is current.
-
#name ⇒ String
The name of the menu.
-
#selected ⇒ Fixnum
Returns the index of the value in the collection which is selected.
Attributes included from Vedeu::Model
Class Method Summary collapse
-
.menu(name = '', &block) ⇒ API::Menu
Register a menu by name which will display a collection of items for your users to select; and provide interactivity within your application.
Instance Method Summary collapse
-
#bottom_item ⇒ Array
Sets the value of current to be the last item of the collection.
-
#current_item ⇒ void
Returns the item from the collection which shares the same index as the value of #current.
-
#defaults ⇒ Hash
private
The default values for a new instance of this class.
-
#deselect_item ⇒ Array
Removes the value of ‘selected`, meaning no items are selected.
-
#initialize(attributes = {}) ⇒ Vedeu::Menus::Menu
constructor
Returns a new instance of Vedeu::Menus::Menu.
-
#items ⇒ Array
Returns a new collection of items.
-
#last ⇒ Fixnum
Returns the last index of the collection.
-
#next_item ⇒ Array
Sets the value of current to be the next item in the collection until we reach the last.
-
#prev_item ⇒ Array
Sets the value of current to be the previous item in the collection until we reach the first.
-
#select_item ⇒ Array
Sets the selected item to be the same value as the current item.
-
#selected_item ⇒ |NilClass
Returns the item from the collection which shares the same index as the value of #selected.
-
#size ⇒ Fixnum
Returns the size of the collection.
-
#top_item ⇒ Array
Sets the value of current to be the first item of the collection.
-
#view ⇒ Array
Returns a subset of all the items.
Methods included from Vedeu::Model
#deputy, #dsl_class, included, #store
Methods included from Common
#demodulize, #present?, #snake_case
Constructor Details
#initialize(attributes = {}) ⇒ Vedeu::Menus::Menu
Returns a new instance of Vedeu::Menus::Menu.
75 76 77 78 79 80 81 |
# File 'lib/vedeu/menus/menu.rb', line 75 def initialize(attributes = {}) @attributes = defaults.merge!(attributes) @attributes.each do |key, value| instance_variable_set("@#{key}", value) end end |
Instance Attribute Details
#collection ⇒ Array
14 15 16 |
# File 'lib/vedeu/menus/menu.rb', line 14 def collection @collection end |
#current ⇒ Fixnum
Returns the index of the value in the collection which is current.
21 22 23 |
# File 'lib/vedeu/menus/menu.rb', line 21 def current @current end |
#name ⇒ String
The name of the menu. Used to reference the menu throughout the application’s execution lifetime.
28 29 30 |
# File 'lib/vedeu/menus/menu.rb', line 28 def name @name end |
#selected ⇒ Fixnum
Returns the index of the value in the collection which is selected.
35 36 37 |
# File 'lib/vedeu/menus/menu.rb', line 35 def selected @selected end |
Class Method Details
.menu(name = '', &block) ⇒ API::Menu
Register a menu by name which will display a collection of items for your users to select; and provide interactivity within your application.
61 62 63 64 65 |
# File 'lib/vedeu/menus/menu.rb', line 61 def self.(name = '', &block) fail Vedeu::Error::InvalidSyntax, 'block not given' unless block_given? build(name: name, &block).store end |
Instance Method Details
#bottom_item ⇒ Array
Sets the value of current to be the last item of the collection.
154 155 156 157 158 |
# File 'lib/vedeu/menus/menu.rb', line 154 def bottom_item @current = last items end |
#current_item ⇒ void
This method returns an undefined value.
Returns the item from the collection which shares the same index as the value of #current.
87 88 89 |
# File 'lib/vedeu/menus/menu.rb', line 87 def current_item @collection[@current] end |
#defaults ⇒ Hash (private)
The default values for a new instance of this class.
219 220 221 222 223 224 225 226 227 228 |
# File 'lib/vedeu/menus/menu.rb', line 219 def defaults { client: nil, collection: [], current: 0, name: '', repository: Vedeu., selected: nil, } end |
#deselect_item ⇒ Array
Removes the value of ‘selected`, meaning no items are selected.
194 195 196 197 198 |
# File 'lib/vedeu/menus/menu.rb', line 194 def deselect_item @selected = nil items end |
#items ⇒ Array
Returns a new collection of items. Each element of the collection is of the format:
[selected, current, item]
‘selected` is a boolean indicating whether the item is selected. `current` is a boolean indicating whether the item is current. `item` is the item itself.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/vedeu/menus/menu.rb', line 113 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 ⇒ Fixnum
Returns the last index of the collection.
203 204 205 |
# File 'lib/vedeu/menus/menu.rb', line 203 def last @collection.size - 1 end |
#next_item ⇒ Array
Sets the value of current to be the next item in the collection until we reach the last.
164 165 166 167 168 |
# File 'lib/vedeu/menus/menu.rb', line 164 def next_item @current += 1 if @current < last items end |
#prev_item ⇒ Array
Sets the value of current to be the previous item in the collection until we reach the first.
174 175 176 177 178 |
# File 'lib/vedeu/menus/menu.rb', line 174 def prev_item @current -= 1 if @current > 0 items end |
#select_item ⇒ Array
Sets the selected item to be the same value as the current item.
184 185 186 187 188 |
# File 'lib/vedeu/menus/menu.rb', line 184 def select_item @selected = @current items end |
#selected_item ⇒ |NilClass
Returns the item from the collection which shares the same index as the value of #selected.
95 96 97 98 99 |
# File 'lib/vedeu/menus/menu.rb', line 95 def selected_item return nil unless @selected @collection[@selected] end |
#size ⇒ Fixnum
Returns the size of the collection.
210 211 212 |
# File 'lib/vedeu/menus/menu.rb', line 210 def size @collection.size end |
#top_item ⇒ Array
Sets the value of current to be the first item of the collection.
144 145 146 147 148 |
# File 'lib/vedeu/menus/menu.rb', line 144 def top_item @current = 0 items end |
#view ⇒ Array
Returns a subset of all the items.
136 137 138 |
# File 'lib/vedeu/menus/menu.rb', line 136 def view items[@current, @collection.size] end |