Class: Volt::ReactiveArray
Instance Method Summary
collapse
Methods included from Eventable
#on, #remove_listener, #trigger!
Constructor Details
Returns a new instance of ReactiveArray.
7
8
9
10
11
12
|
# File 'lib/volt/reactive/reactive_array.rb', line 7
def initialize(array = [])
@array = array
@array_deps = []
@size_dep = Dependency.new
@old_size = 0
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
Forward any missing methods to the array
15
16
17
|
# File 'lib/volt/reactive/reactive_array.rb', line 15
def method_missing(method_name, *args, &block)
@array.send(method_name, *args, &block)
end
|
Instance Method Details
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
# File 'lib/volt/reactive/reactive_array.rb', line 193
def +(array)
fail 'not implemented yet'
old_size = size
result = ReactiveArray.new(@array.dup + array)
old_size.upto(result.size - 1) do |index|
trigger_for_index!('changed', index)
trigger_added!(old_size + index)
end
trigger_size_change!
result
end
|
#<<(value) ⇒ Object
183
184
185
186
187
188
189
190
191
|
# File 'lib/volt/reactive/reactive_array.rb', line 183
def <<(value)
result = (@array << value)
trigger_for_index!(size - 1)
trigger_added!(size - 1)
trigger_size_change!
result
end
|
#==(*args) ⇒ Object
19
20
21
|
# File 'lib/volt/reactive/reactive_array.rb', line 19
def ==(*args)
@array.==(*args)
end
|
#[](index) ⇒ Object
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/volt/reactive/reactive_array.rb', line 93
def [](index)
index = size + index if index < 0
dep = (@array_deps[index] ||= Dependency.new)
dep.depend
@array[index]
end
|
#[]=(index, value) ⇒ Object
107
108
109
110
111
112
113
114
|
# File 'lib/volt/reactive/reactive_array.rb', line 107
def []=(index, value)
@array[index] = value
trigger_for_index!(index)
trigger_size_change!
end
|
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/volt/reactive/reactive_array.rb', line 76
def all?
if block_given?
size.times do |index|
val = self[index]
unless yield(val).true?
return false
end
end
return true
else
return @array.all?
end
end
|
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/volt/reactive/reactive_array.rb', line 60
def any?
if block_given?
size.times do |index|
val = self[index]
if yield(val).true?
return true
end
end
return false
else
return @array.any?
end
end
|
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
# File 'lib/volt/reactive/reactive_array.rb', line 160
def clear
old_size = @array.size
deps = @array_deps
@array_deps = []
old_size.times do |index|
trigger_removed!(old_size - index - 1)
end
if deps
deps.each do |dep|
dep.changed! if dep
end
end
@array = []
end
|
#count(&block) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/volt/reactive/reactive_array.rb', line 33
def count(&block)
if block
count = 0
size.times do |index|
if block.call(self[index]).true?
count += 1
end
end
return count
else
return size
end
end
|
#delete(val) ⇒ Object
149
150
151
152
153
154
155
156
157
158
|
# File 'lib/volt/reactive/reactive_array.rb', line 149
def delete(val)
index = @array.index(val)
if index
delete_at(index)
else
@persistor.removed(val) if @persistor
end
end
|
#delete_at(index) ⇒ Object
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
# File 'lib/volt/reactive/reactive_array.rb', line 124
def delete_at(index)
index = size + index if index < 0
model = @array.delete_at(index)
index_deps = @array_deps.delete_at(index)
index_deps.remove if index_deps
trigger_removed!(index)
index.upto(size + 1) do |position|
trigger_for_index!(position)
end
trigger_size_change!
@persistor.removed(model) if @persistor
model
end
|
#each(&block) ⇒ Object
At the moment, each just passes through.
24
25
26
|
# File 'lib/volt/reactive/reactive_array.rb', line 24
def each(&block)
@array.each(&block)
end
|
28
29
30
31
|
# File 'lib/volt/reactive/reactive_array.rb', line 28
def empty?
@size_dep.depend
@array.empty?
end
|
#insert(index, *objects) ⇒ Object
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
# File 'lib/volt/reactive/reactive_array.rb', line 210
def insert(index, *objects)
result = @array.insert(index, *objects)
index.upto(result.size) do |index|
trigger_for_index!(index)
end
objects.size.times do |count|
trigger_added!(index + count)
end
trigger_size_change!
result
end
|
227
228
229
230
231
|
# File 'lib/volt/reactive/reactive_array.rb', line 227
def inspect
@size_dep.depend
"#<#{self.class}:#{object_id} #{@array.inspect}>"
end
|
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/volt/reactive/reactive_array.rb', line 48
def select
result = []
size.times do |index|
val = self[index]
if yield(val).true?
result << val
end
end
result
end
|
#size ⇒ Object
Also known as:
length
116
117
118
119
120
|
# File 'lib/volt/reactive/reactive_array.rb', line 116
def size
@size_dep.depend
@array.size
end
|