Class: Vedeu::Menu

Inherits:
Object
  • Object
show all
Includes:
Model
Defined in:
lib/vedeu/models/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

Attributes included from Model

#repository

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Model

#demodulize, #deputy, #dsl_class, included, #store

Constructor Details

#initialize(attributes = {}) ⇒ Menu

Returns a new instance of Menu.

Parameters:

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

Options Hash (attributes):

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


60
61
62
63
64
65
66
67
# File 'lib/vedeu/models/menu.rb', line 60

def initialize(attributes = {})
  @attributes = defaults.merge!(attributes)
  @collection = @attributes[:collection]
  @current    = @attributes[:current]
  @name       = @attributes[:name]
  @repository = Vedeu.menus
  @selected   = @attributes[:selected]
end

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



12
13
14
# File 'lib/vedeu/models/menu.rb', line 12

def collection
  @collection
end

#currentFixnum

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

Returns:



17
18
19
# File 'lib/vedeu/models/menu.rb', line 17

def current
  @current
end

#nameObject

Returns the value of attribute name.



19
20
21
# File 'lib/vedeu/models/menu.rb', line 19

def name
  @name
end

#selectedFixnum

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

Returns:



24
25
26
# File 'lib/vedeu/models/menu.rb', line 24

def selected
  @selected
end

Class Method Details

Register a menu by name which will display a collection of items for your users to select; and provide interactivity within your application.

Examples:

Vedeu.menu 'my_interface' do
  items [:item_1, :item_2, :item_3]
  ...

Vedeu.menu do
  name 'menus_must_have_a_name'
  items Track.all_my_favourites
  ...

Parameters:

  • name (String) (defaults to: '')

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

  • block (Proc)

    A set of attributes which define the features of the menu. See DSL::Menu#items and DSL::Menu#name.

Returns:

  • (API::Menu)

Raises:

  • (InvalidSyntax)

    The required block was not given.



46
47
48
49
50
# File 'lib/vedeu/models/menu.rb', line 46

def self.menu(name = '', &block)
  fail InvalidSyntax, 'block not given' unless block_given?

  build({ name: name }, &block).store
end

Instance Method Details

#bottom_itemArray

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

Returns:

  • (Array)


136
137
138
139
140
# File 'lib/vedeu/models/menu.rb', line 136

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.



73
74
75
# File 'lib/vedeu/models/menu.rb', line 73

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
# File 'lib/vedeu/models/menu.rb', line 199

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

#deselect_itemArray

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

Returns:

  • (Array)


174
175
176
177
178
# File 'lib/vedeu/models/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)


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/vedeu/models/menu.rb', line 97

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:



183
184
185
# File 'lib/vedeu/models/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)


146
147
148
149
150
# File 'lib/vedeu/models/menu.rb', line 146

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

  items
end

#prev_itemArray

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

Returns:

  • (Array)


156
157
158
159
160
# File 'lib/vedeu/models/menu.rb', line 156

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)


165
166
167
168
169
# File 'lib/vedeu/models/menu.rb', line 165

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)


81
82
83
84
85
# File 'lib/vedeu/models/menu.rb', line 81

def selected_item
  return nil unless @selected

  @collection[@selected]
end

#sizeFixnum

Returns the size of the collection.

Returns:



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

def size
  @collection.size
end

#top_itemArray

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

Returns:

  • (Array)


127
128
129
130
131
# File 'lib/vedeu/models/menu.rb', line 127

def top_item
  @current = 0

  items
end

#viewArray

Returns a subset of all the items.

Returns:

  • (Array)


120
121
122
# File 'lib/vedeu/models/menu.rb', line 120

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