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.

Parameters:

  • value (Fixnum, Object)

    is the integer position within the menu to select or the item in the options to select.



46
47
48
49
50
51
52
# File 'lib/metro/models/properties/options_property/options.rb', line 46

def current_selected_index=(value)
  value = index(value) unless value.is_a?(Fixnum)
  @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



84
85
86
# File 'lib/metro/models/properties/options_property/options.rb', line 84

def next!
  self.current_selected_index += 1
end

#previous!Object

Move the current selected item to the previous item



91
92
93
# File 'lib/metro/models/properties/options_property/options.rb', line 91

def previous!
  self.current_selected_index -= 1
end

#selectedObject

Returns the currently selected option.

Returns:

  • (Object)

    the currently selected option.



64
65
66
# File 'lib/metro/models/properties/options_property/options.rb', line 64

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.



73
74
75
76
77
78
79
# File 'lib/metro/models/properties/options_property/options.rb', line 73

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.



57
58
59
# File 'lib/metro/models/properties/options_property/options.rb', line 57

def unselected
  self - [ selected ]
end