Class: UU::OS::GVC::MenuModel

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

Constructor Details

- (MenuModel) initialize(data = nil)

Creates a new instance of MenuModel.

Examples:

# init empty MenuModel, with no menu items
mm = UU::OS::GVC::MenuModel.new()
# init MenuModel as copy of another model
menu_model = ... # get existing MenuModel
mm_copy = UU::OS::GVC::MenuModel.new(menu_model)
# init MenuModel from Array

array_data = [
             { :text => "Menu item 1", :actionUri => "ues:TER:ART:" },
             { :text => "Menu item 2", :actionUri => "ues:${this-art}?:myevent" },
             { :text => "Menu item 3", :disabled => true },
             {},
             { :text => "Menu item 4", :actionUri => "http://google.com/", :target => "NEW_WINDOW" }
           ]
menu_model = UU::OS::GVC::MenuModel.new(array_data)
# init MenuModel from String containing data in JSON Format

str_data = '[
              {"text":"Menu item 1", "actionUri": "ues:TER:ART:"},
              {"text":"Menu item 2", "actionUri": "ues:${this-art}?:myevent"},
              {"text":"Menu item 3", "disabled": true},
              {},
              {"text":"Menu item 4", "actionUri": "http://google.com/", "target": "NEW_WINDOW"}
            ]'
menu_model = UU::OS::GVC::MenuModel.new(str_data)

Parameters:

  • data (String, Array, UU::OS::GVC::MenuModel) (defaults to: nil)

    Initialization data in form of JSON String, Array or MenuModel.

    • String - String with valid JSON containing data in Array Format.

    • Array - Array containing data in Array Format.

    • UU::OS::GVC::MenuModel - Another MenuModel, new model will be copy of passed data.

Raises:

  • (ArgumentError)

    If data is not one of JSON String, Array or 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.

Examples:

# add item using JSON String
item = '{"text": "Menu item 1", "actionUri": "ues:TER:ART:"}'
mm.add_item(item)

# add same item using Hash
item = { :text => "Menu item 1", :actionUri => "ues:TER:ART:" }
mm.add_item(item)

Parameters:

  • menu_item (String, Hash, MenuItem)

    Menu item to add.

Returns:

  • (Numeric)

    Index of the new item.

Raises:

  • (ArgumentError)

    If row has bad format or is not one of String, Hash or MenuItem.



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(menu_item)
  item = MenuItem.new(menu_item)
  @json = [] if @json.nil?
  @json << item
  @json.size - 1
end

- (MenuModel) clone

Returns copy of data representation as MenuModel.

Examples:

# copy MenuModel
mm_copy = mm.clone

mm.number_of_items # 5
mm_copy.number_of_items # 5

# add item to original MenuModel
mm.add_item({ :text => "Another item", :actionUri => "ues:TER:ART:" })

mm.number_of_items # 6
mm_copy.number_of_items # 5

Returns:



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.

Examples:

# get third row
item = mm.get_item(2)

# get last row
item = mm.get_item(-1)

Parameters:

  • index (Integer)

    Index of menu item. An index can be represented by a single positive integer between 0 (index 0 = first row) and the number of rows (excluding) which is returned by #number_of_items method. You can use negative indices as well, which start counting from the end (i.e. minus number of rows), with -1 being the last element.

Returns:

  • (MenuItem)

    Menu item at specified index.

Raises:

  • (ArgumentError)

    If index is greater than or equal to number of rows.



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).

Examples:

# insert item at fifth position using JSON String
item = '{"text": "Menu item 1", "actionUri": "ues:TER:ART:"}'
mm.insert_item(4, item)

# insert same item using Hash
item = { :text => "Menu item 1", :actionUri => "ues:TER:ART:" }
mm.insert_item(4, item)

Parameters:

  • index (Integer)

    Index where new item will be inserted. An index can be represented by a single positive integer between 0 (index 0 = first row) and the number of items (excluding) which is returned by #number_of_items method. You can use negative indices as well, which start counting from the end (i.e. minus number of items), with -1 being the last element. Inserting with negative index adds the new item after the element.

  • menu_item (String, Hash, MenuItem)

    Menu item.

Raises:

  • (ArgumentError)

    If item has bad format or is not one of String, Hash or MenuItem. If index is greater than or equal to number of rows.



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, menu_item)
  check_index(index, -number_of_items, number_of_items, "index")
  item = MenuItem.new(menu_item)
  @json = [] if @json.nil?
  @json.insert(index, item)
  return
end

- (Numeric) number_of_items

Returns number of items in the menu. May be zero.

Examples:

# get number of items in the menu
items_count = mm.number_of_items

Returns:

  • (Numeric)

    Number of items.



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).

Examples:

# remove second item
dtm.remove_item(1)

# remove last item
dtm.remove_item(-1)

Parameters:

  • index (Integer)

    Index where menu item for removal is at. An index can be represented by a single positive integer between 0 (index 0 = first row) and the number of rows (excluding) which is returned by #number_of_items method. You can use negative indices as well, which start counting from the end (i.e. minus number of items), with -1 being the last element.

Raises:

  • (ArgumentError)

    If index is greater than or equal to number of items.



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.

Examples:

# set item at second position
item = '{"text": "Menu item 1", "actionUri": "ues:TER:ART:"}'
mm.set_item(1 ,item)

# set same item using Hash format
item = { :text => "Menu item 1", :actionUri => "ues:TER:ART:" }
mm.set_item(1 ,item)

Parameters:

  • index (Integer)

    Index of item. An index can be represented by a single positive integer between 0 (index 0 = first row) and the number of rows (excluding) which is returned by #number_of_items method. You can use negative indices as well, which start counting from the end (i.e. minus number of items), with -1 being the last element.

  • menu_item (String, Hash, MenuItem)

    Menu item.

Raises:

  • (ArgumentError)

    If item has bad format or is not one of String, Hash or MenuItem. If index is greater than or equal to number of items.



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, menu_item)
  check_index(index, -number_of_items, number_of_items-1, "index")
  item = MenuItem.new(menu_item)
  @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.

Examples:

# get data as Array
array = mm.to_a

Returns:

  • (Array)

    Data as Array of MenuItem-s.



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.

Examples:

# get JSON String
data_str = mm.to_json

# output:
# [
#   {"text":"Menu item 1", "actionUri": "ues:TER:ART:"},
#   {"text":"Menu item 2", "actionUri": "ues:${this-art}?:myevent"},
#   {"text":"Menu item 3", "disabled": true},
#   {},
#   {"text":"Menu item 4", "actionUri": "http://google.com/", "target": "NEW_WINDOW"}
# ]

Returns:

  • (String)

    Data 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(options = {})
  @json.to_json(options)
end

- (String) to_s

Returns data representation as String.

Examples:

# get String
data_str = mm.to_s

# output:
# [
#   {"text":"Menu item 1", "actionUri": "ues:TER:ART:"},
#   {"text":"Menu item 2", "actionUri": "ues:${this-art}?:myevent"},
#   {"text":"Menu item 3", "disabled": true},
#   {},
#   {"text":"Menu item 4", "actionUri": "http://google.com/", "target": "NEW_WINDOW"}
# ]

Returns:

  • (String)

    Data 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