Class: QML::ArrayModel
- 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
Instance Method Summary collapse
-
#[](index) ⇒ Object
Returns an item.
-
#[]=(index, item) ⇒ Object
Updates an item.
-
#clear ⇒ self
Deletes all items.
-
#count ⇒ Integer
The number of the items.
- #delete_at(index, count = nil) ⇒ Object
-
#initialize(*columns) ⇒ ArrayModel
constructor
A new instance of ArrayModel.
-
#insert(index, *items) ⇒ self
Inserts items.
- #pop(count = nil) ⇒ Object
-
#push(*items) ⇒ self
(also: #<<)
Append items.
-
#replace(ary) ⇒ self
Replaces entire array with given array.
- #shift(count = nil) ⇒ Object
-
#to_a ⇒ Array
Duplicates the internal array and returns it.
-
#unshift(*items) ⇒ self
Prepend items.
Methods inherited from ListModel
#each, #inserting, #moving, #removing, #resetting, #to_qml, #update
Constructor Details
#initialize(*columns) ⇒ ArrayModel
Returns a new instance of ArrayModel.
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.
25 26 27 |
# File 'lib/qml/data/array_model.rb', line 25 def [](index) @array[index] end |
#[]=(index, item) ⇒ Object
Updates an 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 |
#clear ⇒ self
Deletes all items.
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 |
#count ⇒ Integer
Returns 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
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.
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 |
#pop ⇒ Object #pop(count) ⇒ Array
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.
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.
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 |
#shift ⇒ Object #shift(count) ⇒ Array
81 82 83 |
# File 'lib/qml/data/array_model.rb', line 81 def shift(count = nil) delete_at(0, count) end |
#to_a ⇒ Array
Duplicates the internal array and returns it.
13 14 15 |
# File 'lib/qml/data/array_model.rb', line 13 def to_a @array.dup end |
#unshift(*items) ⇒ self
Prepend items.
71 72 73 |
# File 'lib/qml/data/array_model.rb', line 71 def unshift(*items) insert(0, *items) end |