Class: Vedeu::Menus::Menu

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Vedeu::Menus::Menu

Returns a new instance of Vedeu::Menus::Menu.

Parameters:

  • attributes (Hash) (defaults to: {})

Options Hash (attributes):

  • collection (Array)
  • name (String|Symbol)
  • current (Fixnum)
  • selected (Fixnum|NilClass)


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

#collectionArray

Returns:

  • (Array)


14
15
16
# File 'lib/vedeu/menus/menu.rb', line 14

def collection
  @collection
end

#currentFixnum

Returns the index of the value in the collection which is current.

Returns:

  • (Fixnum)


21
22
23
# File 'lib/vedeu/menus/menu.rb', line 21

def current
  @current
end

#nameString

The name of the menu. Used to reference the menu throughout the application’s execution lifetime.

Returns:

  • (String)


28
29
30
# File 'lib/vedeu/menus/menu.rb', line 28

def name
  @name
end

#repositoryVedeu::Repositories::Repository Originally defined in module Repositories::Model

#selectedFixnum

Returns the index of the value in the collection which is selected.

Returns:

  • (Fixnum)


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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a boolean indicating whether a variable is nil or empty.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

  • (Boolean)

#bottom_itemArray

Sets the value of current to be the last item of the collection.

Returns:

  • (Array)


134
135
136
137
138
# File 'lib/vedeu/menus/menu.rb', line 134

def bottom_item
  @current = last

  items
end

#current_itemvoid

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

#defaultsHash (private)

The default values for a new instance of this class.

Returns:

  • (Hash)


199
200
201
202
203
204
205
206
207
208
# File 'lib/vedeu/menus/menu.rb', line 199

def defaults
  {
    client:     nil,
    collection: [],
    current:    0,
    name:       '',
    repository: Vedeu.menus,
    selected:   nil,
  }
end

#demodulize(klass) ⇒ String Originally defined in module Common

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Removes the module part from the expression in the string.

Examples:

demodulize('Vedeu::SomeModule::SomeClass') # => "SomeClass"

Parameters:

  • klass (Class|String)

Returns:

  • (String)

#deputy(client = nil) ⇒ Vedeu::Menus::DSL

Returns a DSL instance responsible for defining the DSL methods of this model.

Parameters:

  • client (Object|NilClass) (defaults to: nil)

    The client binding represents the client application object that is currently invoking a DSL method. It is required so that we can send messages to the client application object should we need to.

Returns:



67
68
69
# File 'lib/vedeu/menus/menu.rb', line 67

def deputy(client = nil)
  Vedeu::Menus::DSL.new(self, client)
end

#deselect_itemArray

Removes the value of ‘selected`, meaning no items are selected.

Returns:

  • (Array)


174
175
176
177
178
# File 'lib/vedeu/menus/menu.rb', line 174

def deselect_item
  @selected = nil

  items
end

#itemsArray

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.

Returns:

  • (Array)


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/vedeu/menus/menu.rb', line 93

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

#lastFixnum

Returns the last index of the collection.

Returns:

  • (Fixnum)


183
184
185
# File 'lib/vedeu/menus/menu.rb', line 183

def last
  @collection.size - 1
end

#next_itemArray

Sets the value of current to be the next item in the collection until we reach the last.

Returns:

  • (Array)


144
145
146
147
148
# File 'lib/vedeu/menus/menu.rb', line 144

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

  items
end

#present?(variable) ⇒ Boolean Originally defined in module Common

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a boolean indicating whether a variable has a useful value.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

  • (Boolean)

#prev_itemArray

Sets the value of current to be the previous item in the collection until we reach the first.

Returns:

  • (Array)


154
155
156
157
158
# File 'lib/vedeu/menus/menu.rb', line 154

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

  items
end

#select_itemArray

Sets the selected item to be the same value as the current item.

Returns:

  • (Array)


164
165
166
167
168
# File 'lib/vedeu/menus/menu.rb', line 164

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.

Returns:

  • (|NilClass)


75
76
77
78
79
# File 'lib/vedeu/menus/menu.rb', line 75

def selected_item
  return nil unless @selected

  @collection[@selected]
end

#sizeFixnum

Returns the size of the collection.

Returns:

  • (Fixnum)


190
191
192
# File 'lib/vedeu/menus/menu.rb', line 190

def size
  @collection.size
end

#snake_case(name) ⇒ String Originally defined in module Common

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts a class name to a lowercase snake case string.

Examples:

snake_case(MyClassName) # => "my_class_name"
snake_case(NameSpaced::ClassName)
# => "name_spaced/class_name"

Parameters:

  • name (String)

Returns:

  • (String)

#storevoid Originally defined in module Repositories::Model

TODO:

Perhaps some validation could be added here?

Note:

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_itemArray

Sets the value of current to be the first item of the collection.

Returns:

  • (Array)


124
125
126
127
128
# File 'lib/vedeu/menus/menu.rb', line 124

def top_item
  @current = 0

  items
end

#viewArray

Returns a subset of all the items.

Returns:

  • (Array)


116
117
118
# File 'lib/vedeu/menus/menu.rb', line 116

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