Class: QML::Data::ArrayModel

Inherits:
ListModel show all
Defined in:
lib/qml/data/array_model.rb

Overview

ArrayModel is one of ruby-qml’s list models and it stores data in Array simply.

Instance Attribute Summary

Attributes inherited from ListModel

#columns, #qt_models

Instance Method Summary collapse

Methods inherited from ListModel

#each, #inserting, #moving, #removing, #resetting, #update

Methods included from QML::Dispatchable

#later

Methods included from Wrappable

#create_wrapper

Constructor Details

#initialize(*columns) ⇒ ArrayModel

Returns a new instance of ArrayModel.

Parameters:

  • columns (Array<Symbol|String>)


7
8
9
10
# File 'lib/qml/data/array_model.rb', line 7

def initialize(*columns)
  super
  @array = []
end

Instance Method Details

#[](index) ⇒ Object

Returns an item.

Parameters:

  • index (Integer)

    the index of the item.

Returns:

  • the item.



26
27
28
# File 'lib/qml/data/array_model.rb', line 26

def [](index)
  @array[index]
end

#[]=(index, item) ⇒ Object

Updates an item.

Parameters:

  • index (Integer)
  • item

Returns:

  • the item.



34
35
36
37
38
# File 'lib/qml/data/array_model.rb', line 34

def []=(index, item)
  @array[index] = item
  update(index .. index)
  item
end

#clearself

Deletes all items.

Returns:

  • (self)


106
107
108
109
110
111
# File 'lib/qml/data/array_model.rb', line 106

def clear
  removing(0 ... count) do
    @array.clear
  end
  self
end

#countInteger

Returns the number of the items.

Returns:

  • (Integer)

    the number of the items.



19
20
21
# File 'lib/qml/data/array_model.rb', line 19

def count
  @array.count
end

#delete_at(index) ⇒ Object #delete_at(index, count) ⇒ Array

Overloads:

  • #delete_at(index) ⇒ Object

    Deletes an item.

    Parameters:

    • index (Integer)

    Returns:

    • the deleted item.

  • #delete_at(index, count) ⇒ Array

    Deletes items.

    Parameters:

    • index (Integer)

    Returns:

    • (Array)

      the deleted items.



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/qml/data/array_model.rb', line 58

def delete_at(index, count = nil)
  if count
    removing(index ... index + count) do
      count.times.map { @array.delete_at(index) }
    end
  else
    removing(index .. index) do
      @array.delete_at(index)
    end
  end
end

#insert(index, *items) ⇒ self

Inserts items.

Parameters:

  • index (Integer)

Returns:

  • (self)


43
44
45
46
47
48
# File 'lib/qml/data/array_model.rb', line 43

def insert(index, *items)
  inserting(index ... index + items.size) do
    @array.insert(index, *items)
  end
  self
end

#popObject #pop(count) ⇒ Array

Overloads:

  • #popObject

    Deletes the last item.

    Returns:

    • the deleted item.

  • #pop(count) ⇒ Array

    Deletes the last items.

    Returns:

    • (Array)

      the deleted items.



100
101
102
# File 'lib/qml/data/array_model.rb', line 100

def pop(count = nil)
  delete_at(@array.size - count, count)
end

#push(*items) ⇒ self Also known as: <<

Append items.

Returns:

  • (self)


88
89
90
# File 'lib/qml/data/array_model.rb', line 88

def push(*items)
  insert(@array.size, *items)
end

#replace(ary) ⇒ self

Replaces entire array with given array.

Parameters:

  • ary (Array)

Returns:

  • (self)


116
117
118
119
120
121
# File 'lib/qml/data/array_model.rb', line 116

def replace(ary)
  resetting do
    @array = ary.dup
  end
  self
end

#shiftObject #shift(count) ⇒ Array

Overloads:

  • #shiftObject

    Deletes the first item.

    Returns:

    • the deleted item.

  • #shift(count) ⇒ Array

    Deletes the first items.

    Returns:

    • (Array)

      the deleted items.



82
83
84
# File 'lib/qml/data/array_model.rb', line 82

def shift(count = nil)
  delete_at(0, count)
end

#to_aArray

Duplicates the internal array and returns it.

Returns:

  • (Array)


14
15
16
# File 'lib/qml/data/array_model.rb', line 14

def to_a
  @array.dup
end

#unshift(*items) ⇒ self

Prepend items.

Returns:

  • (self)


72
73
74
# File 'lib/qml/data/array_model.rb', line 72

def unshift(*items)
  insert(0, *items)
end