Class: Metro::Model::OptionsProperty::Options

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/metro/models/properties/options_property/options.rb

Overview

Options maintains the list of menu options that would be displayed in a menu. Each option is a component that can be rendered during the draw phase.

Also, options maintains the currently selected item and has the ability to manage the changes from the previous.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Options

Create options with the provided array of options.

Parameters:

  • options (Array)

    the array of options that this object will maintain as it’s list of options.



27
28
29
# File 'lib/metro/models/properties/options_property/options.rb', line 27

def initialize(options)
  super(options)
end

Class Method Details

.emptyObject

Generate an empty set of options.



17
18
19
# File 'lib/metro/models/properties/options_property/options.rb', line 17

def self.empty
  new []
end

Instance Method Details

#current_selected_indexFixnum

Returns the index of the current selected option.

Returns:

  • (Fixnum)

    the index of the current selected option.



34
35
36
# File 'lib/metro/models/properties/options_property/options.rb', line 34

def current_selected_index
  @current_selected_index ||= 0
end

#current_selected_index=(value) ⇒ Object

Set the index of the currently selected item. Values that exceed the possible count of options will reset to the beginning of the list of options. Values that proceed the start of of the list of options will fallback to the last option.



43
44
45
46
47
48
# File 'lib/metro/models/properties/options_property/options.rb', line 43

def current_selected_index=(value)
  @current_selected_index = value || 0
  @current_selected_index = 0 if @current_selected_index >= count
  @current_selected_index = count - 1 if @current_selected_index <= -1
  @current_selected_index
end

#next!Object

Move the current selected item to the next item



80
81
82
# File 'lib/metro/models/properties/options_property/options.rb', line 80

def next!
  self.current_selected_index += 1
end

#previous!Object

Move the current selected item to the previous item



87
88
89
# File 'lib/metro/models/properties/options_property/options.rb', line 87

def previous!
  self.current_selected_index -= 1
end

#selectedObject

Returns the currently selected option.

Returns:

  • (Object)

    the currently selected option.



60
61
62
# File 'lib/metro/models/properties/options_property/options.rb', line 60

def selected
  at(current_selected_index) || NoOption.new
end

#selected_actionSymbol

The action name of the currently selected option. If no option is currently selected then the NoOption ‘missing_menu_action’ will be returned.

Returns:

  • (Symbol)

    the action name of the currently selected option. If no option is currently selected then the NoOption ‘missing_menu_action’ will be returned.



69
70
71
72
73
74
75
# File 'lib/metro/models/properties/options_property/options.rb', line 69

def selected_action
  if selected.respond_to?(:properties) && selected.properties[:action]
    selected.properties[:action]
  else
    selected.to_sym
  end
end

#unselectedArray

Returns a list of all the options that are currently not selected.

Returns:

  • (Array)

    a list of all the options that are currently not selected.



53
54
55
# File 'lib/metro/models/properties/options_property/options.rb', line 53

def unselected
  self - [ selected ]
end