Class: QML::Data::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, #update
Methods included from QML::Dispatchable
Methods included from Wrappable
Constructor Details
#initialize(*columns) ⇒ ArrayModel
Returns a new instance of ArrayModel.
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.
26 27 28 |
# File 'lib/qml/data/array_model.rb', line 26 def [](index) @array[index] end |
#[]=(index, item) ⇒ Object
Updates an 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 |
#clear ⇒ self
Deletes all items.
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 |
#count ⇒ Integer
Returns 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
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.
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 |
#pop ⇒ Object #pop(count) ⇒ Array
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.
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.
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 |
#shift ⇒ Object #shift(count) ⇒ Array
82 83 84 |
# File 'lib/qml/data/array_model.rb', line 82 def shift(count = nil) delete_at(0, count) end |
#to_a ⇒ Array
Duplicates the internal array and returns it.
14 15 16 |
# File 'lib/qml/data/array_model.rb', line 14 def to_a @array.dup end |
#unshift(*items) ⇒ self
Prepend items.
72 73 74 |
# File 'lib/qml/data/array_model.rb', line 72 def unshift(*items) insert(0, *items) end |