Class: QML::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

Instance Method Summary collapse

Methods inherited from ListModel

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

Constructor Details

#initialize(*columns) ⇒ ArrayModel

Returns a new instance of ArrayModel.

Parameters:



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

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.



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

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

#[]=(index, item) ⇒ Object

Updates an item.

Parameters:

  • index (Integer)
  • item

Returns:

  • the item.



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

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

#clearself

Deletes all items.

Returns:

  • (self)


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

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.



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

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.



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

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)


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

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.



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

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

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

Append items.

Returns:

  • (self)


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

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

#replace(ary) ⇒ self

Replaces entire array with given array.

Parameters:

Returns:

  • (self)


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

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.



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

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

#to_aArray

Duplicates the internal array and returns it.

Returns:



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

def to_a
  @array.dup
end

#unshift(*items) ⇒ self

Prepend items.

Returns:

  • (self)


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

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