Class: UU::OS::GVC::MenuModel
- Inherits:
-
Object
- Object
- UU::OS::GVC::MenuModel
- Defined in:
- uu_os_gvc-0.28.16/lib/uu/os/gvc/menu_model.rb
Overview
Representation of menu data as used by components with menu, such as drop-down button.
Defined Under Namespace
Classes: MenuItem
Instance Method Summary (collapse)
-
- (Numeric) add_item(menu_item)
Adds a new item to the end of Menu, and returns the index of the new item.
-
- (MenuModel) clone
Returns copy of data representation as MenuModel.
-
- (MenuItem) get_item(index)
Returns menu item at specified index.
-
- (MenuModel) initialize(data = nil)
constructor
Creates a new instance of MenuModel.
-
- (void) insert_item(index, menu_item)
Inserts a new item to the menu at specified index.
-
- (Numeric) number_of_items
Returns number of items in the menu.
-
- (void) remove_item(index)
Removes the item at specific index.
-
- (void) set_item(index, menu_item)
Set item at specific index, the original item at specified index will be overwritten.
-
- (Array) to_a
Returns copy of data representation as Array of MenuItem-s.
-
- (String) to_json(options = {})
Returns data representation as JSON String.
-
- (String) to_s
Returns data representation as String.
Constructor Details
- (MenuModel) initialize(data = nil)
Creates a new instance of MenuModel.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'uu_os_gvc-0.28.16/lib/uu/os/gvc/menu_model.rb', line 50 def initialize(data=nil) if data.nil? @json = nil elsif data.kind_of? String js = JSON.parse(data, :symbolize_names => true) if (!js.kind_of? Array) raise ArgumentError.new("JSON String has bad format. The root element must be Array but was #{js.class}.") end init_array(js) elsif data.kind_of? Array init_array(data) elsif data.kind_of? MenuModel @json = data.to_a else raise ArgumentError.new("MenuModel data must be JSON String, Array or MenuModel, but was #{data.class}.") end end |
Instance Method Details
- (Numeric) add_item(menu_item)
Adds a new item to the end of Menu, and returns the index of the new item.
82 83 84 85 86 87 |
# File 'uu_os_gvc-0.28.16/lib/uu/os/gvc/menu_model.rb', line 82 def add_item() item = MenuItem.new() @json = [] if @json.nil? @json << item @json.size - 1 end |
- (MenuModel) clone
Returns copy of data representation as MenuModel.
212 213 214 |
# File 'uu_os_gvc-0.28.16/lib/uu/os/gvc/menu_model.rb', line 212 def clone MenuModel.new(self) end |
- (MenuItem) get_item(index)
Returns menu item at specified index. Returned object is of MenuItem class. Modification of the returned object has no influence on the original data of Menu.
104 105 106 107 |
# File 'uu_os_gvc-0.28.16/lib/uu/os/gvc/menu_model.rb', line 104 def get_item(index) check_index(index, -number_of_items, number_of_items-1, "index") deep_copy(@json[index]) end |
- (void) insert_item(index, menu_item)
This method returns an undefined value.
Inserts a new item to the menu at specified index. All existing items from specified index to the end are shifted (index is increased by one).
143 144 145 146 147 148 149 |
# File 'uu_os_gvc-0.28.16/lib/uu/os/gvc/menu_model.rb', line 143 def insert_item(index, ) check_index(index, -number_of_items, number_of_items, "index") item = MenuItem.new() @json = [] if @json.nil? @json.insert(index, item) return end |
- (Numeric) number_of_items
Returns number of items in the menu. May be zero.
116 117 118 119 120 121 122 |
# File 'uu_os_gvc-0.28.16/lib/uu/os/gvc/menu_model.rb', line 116 def number_of_items if @json.nil? 0 else @json.size end end |
- (void) remove_item(index)
This method returns an undefined value.
Removes the item at specific index. All existing items after specified index are shifted (index is decreased by one).
190 191 192 193 194 |
# File 'uu_os_gvc-0.28.16/lib/uu/os/gvc/menu_model.rb', line 190 def remove_item(index) check_index(index, -number_of_items, number_of_items-1, "index") @json.delete_at(index) return end |
- (void) set_item(index, menu_item)
This method returns an undefined value.
Set item at specific index, the original item at specified index will be overwritten.
168 169 170 171 172 173 |
# File 'uu_os_gvc-0.28.16/lib/uu/os/gvc/menu_model.rb', line 168 def set_item(index, ) check_index(index, -number_of_items, number_of_items-1, "index") item = MenuItem.new() @json[index] = item return end |
- (Array) to_a
Returns copy of data representation as Array of MenuItem-s. Modification of the returned array object has no influence on the original data of MenuModel.
224 225 226 |
# File 'uu_os_gvc-0.28.16/lib/uu/os/gvc/menu_model.rb', line 224 def to_a deep_copy(@json) end |
- (String) to_json(options = {})
Returns data representation as JSON String.
244 245 246 |
# File 'uu_os_gvc-0.28.16/lib/uu/os/gvc/menu_model.rb', line 244 def to_json( = {}) @json.to_json() end |
- (String) to_s
Returns data representation as String.
264 265 266 |
# File 'uu_os_gvc-0.28.16/lib/uu/os/gvc/menu_model.rb', line 264 def to_s to_json end |