Class: Vedeu::Menus::Menu
- Inherits:
-
Object
- Object
- Vedeu::Menus::Menu
- Includes:
- Repositories::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.
- #repository ⇒ Vedeu::Repositories::Repository included from Repositories::Model
-
#selected ⇒ Fixnum
Returns the index of the value in the collection which is selected.
Instance Method Summary collapse
-
#absent?(variable) ⇒ Boolean
included
from Common
Returns a boolean indicating whether a variable is nil or empty.
-
#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.
-
#demodulize(klass) ⇒ String
included
from Common
Removes the module part from the expression in the string.
-
#deputy(client = nil) ⇒ void
included
from Repositories::Model
Returns a DSL instance responsible for defining the DSL methods of this model.
-
#deselect_item ⇒ Array
Removes the value of ‘selected`, meaning no items are selected.
-
#dsl_class ⇒ String
included
from Repositories::Model
private
Returns the DSL class name responsible for this model.
-
#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.
-
#present?(variable) ⇒ Boolean
included
from Common
Returns a boolean indicating whether a variable has a useful value.
-
#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.
-
#snake_case(name) ⇒ String
included
from Common
Converts a class name to a lowercase snake case string.
-
#store ⇒ void
included
from Repositories::Model
The model instance stored in the repository.
-
#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.
Constructor Details
#initialize(attributes = {}) ⇒ Vedeu::Menus::Menu
Returns a new instance of Vedeu::Menus::Menu.
45 46 47 48 49 |
# File 'lib/vedeu/menus/menu.rb', line 45 def initialize(attributes = {}) defaults.merge!(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 |
#repository ⇒ Vedeu::Repositories::Repository Originally defined in module Repositories::Model
#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 |
Instance Method Details
#absent?(variable) ⇒ Boolean Originally defined in module Common
Returns a boolean indicating whether a variable is nil or empty.
#bottom_item ⇒ Array
Sets the value of current to be the last item of the collection.
122 123 124 125 126 |
# File 'lib/vedeu/menus/menu.rb', line 122 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.
55 56 57 |
# File 'lib/vedeu/menus/menu.rb', line 55 def current_item @collection[@current] end |
#defaults ⇒ Hash (private)
The default values for a new instance of this class.
187 188 189 190 191 192 193 194 195 196 |
# File 'lib/vedeu/menus/menu.rb', line 187 def defaults { client: nil, collection: [], current: 0, name: '', repository: Vedeu., selected: nil, } end |
#demodulize(klass) ⇒ String Originally defined in module Common
Removes the module part from the expression in the string.
#deputy(client = nil) ⇒ void Originally defined in module Repositories::Model
This method returns an undefined value.
Returns a DSL instance responsible for defining the DSL methods of this model.
#deselect_item ⇒ Array
Removes the value of ‘selected`, meaning no items are selected.
162 163 164 165 166 |
# File 'lib/vedeu/menus/menu.rb', line 162 def deselect_item @selected = nil items end |
#dsl_class ⇒ String (private) Originally defined in module Repositories::Model
Returns the DSL class name responsible for this model.
#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.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/vedeu/menus/menu.rb', line 81 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.
171 172 173 |
# File 'lib/vedeu/menus/menu.rb', line 171 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.
132 133 134 135 136 |
# File 'lib/vedeu/menus/menu.rb', line 132 def next_item @current += 1 if @current < last items end |
#present?(variable) ⇒ Boolean Originally defined in module Common
Returns a boolean indicating whether a variable has a useful value.
#prev_item ⇒ Array
Sets the value of current to be the previous item in the collection until we reach the first.
142 143 144 145 146 |
# File 'lib/vedeu/menus/menu.rb', line 142 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.
152 153 154 155 156 |
# File 'lib/vedeu/menus/menu.rb', line 152 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.
63 64 65 66 67 |
# File 'lib/vedeu/menus/menu.rb', line 63 def selected_item return nil unless @selected @collection[@selected] end |
#size ⇒ Fixnum
Returns the size of the collection.
178 179 180 |
# File 'lib/vedeu/menus/menu.rb', line 178 def size @collection.size end |
#snake_case(name) ⇒ String Originally defined in module Common
Converts a class name to a lowercase snake case string.
#store ⇒ void Originally defined in module Repositories::Model
Perhaps some validation could be added here?
If a block is given, store the model, return the model after yielding.
This method returns an undefined value.
Returns The model instance stored in the repository.
#top_item ⇒ Array
Sets the value of current to be the first item of the collection.
112 113 114 115 116 |
# File 'lib/vedeu/menus/menu.rb', line 112 def top_item @current = 0 items end |
#view ⇒ Array
Returns a subset of all the items.
104 105 106 |
# File 'lib/vedeu/menus/menu.rb', line 104 def view items[@current, @collection.size] end |