Class: ReactiveArray
- Includes:
- ObjectTracking, ReactiveTags
- Defined in:
- lib/volt/reactive/reactive_array.rb
Overview
< Array
Direct Known Subclasses
Instance Method Summary collapse
- #+(array) ⇒ Object
-
#<<(value) ⇒ Object
alias :__old_append :<<.
- #==(*args) ⇒ Object
-
#[]=(index, value) ⇒ Object
alias :__old_assign :[]=.
- #clear ⇒ Object
-
#count(*args, &block) ⇒ Object
tag_method(:count) do destructive! end.
- #delete(val) ⇒ Object
-
#delete_at(index) ⇒ Object
alias :__old_delete_at :delete_at.
-
#each(&block) ⇒ Object
At the moment, each just passes through.
-
#initialize(array = []) ⇒ ReactiveArray
constructor
A new instance of ReactiveArray.
- #insert(index, *objects) ⇒ Object
- #inspect ⇒ Object
-
#method_missing(method_name, *args, &block) ⇒ Object
Forward any missing methods to the array.
- #reject(*args, &block) ⇒ Object
-
#split_scope(scope) ⇒ Object
TODO: This is an opal work around.
-
#trigger_for_index!(event_name, index, *passed_args) ⇒ Object
Trigger the changed event to any values fetched either through the lookup ([]), #last, or any fetched through the array its self.
- #trigger_on_direct_listeners!(event, *args) ⇒ Object
- #trigger_size_change! ⇒ Object
Methods included from ObjectTracking
Methods included from ReactiveTags
included, #reactive_method_tag
Constructor Details
#initialize(array = []) ⇒ ReactiveArray
Returns a new instance of ReactiveArray.
8 9 10 |
# File 'lib/volt/reactive/reactive_array.rb', line 8 def initialize(array=[]) @array = array 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
13 14 15 |
# File 'lib/volt/reactive/reactive_array.rb', line 13 def method_missing(method_name, *args, &block) @array.send(method_name, *args, &block) end |
Instance Method Details
#+(array) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/volt/reactive/reactive_array.rb', line 124 def +(array) old_size = self.size # TODO: += is funky here, might need to make a .plus! method result = ReactiveArray.new(@array.dup + array) old_size.upto(result.size-1) do |index| trigger_for_index!('changed', index) trigger_on_direct_listeners!('added', old_size + index) end trigger_size_change! return result end |
#<<(value) ⇒ Object
alias :__old_append :<<
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/volt/reactive/reactive_array.rb', line 110 def <<(value) result = (@array << value) # Track new value __track_element(self.size-1, value) trigger_for_index!('changed', self.size-1) trigger_on_direct_listeners!('added', self.size-1) trigger_size_change! return result end |
#==(*args) ⇒ Object
17 18 19 |
# File 'lib/volt/reactive/reactive_array.rb', line 17 def ==(*args) @array.==(*args) end |
#[]=(index, value) ⇒ Object
alias :__old_assign :[]=
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/volt/reactive/reactive_array.rb', line 34 def []=(index, value) index_val = index.cur if index_val < 0 # Handle a negative index index_val = size + index_val end # Clean old value __clear_element(index) @array[index.cur] = value # Track new value __track_element(index, value) # Also track the index if its reactive if index.reactive? # TODO: Need to clean this up when the index changes event_chain.add_object(index.reactive_manager) do |event, *args| trigger_for_index!(event, index.cur) end end # Trigger changed trigger_for_index!('changed', index_val) end |
#clear ⇒ Object
101 102 103 104 |
# File 'lib/volt/reactive/reactive_array.rb', line 101 def clear @array = [] trigger!('changed') end |
#count(*args, &block) ⇒ Object
tag_method(:count) do
destructive!
end
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/volt/reactive/reactive_array.rb', line 246 def count(*args, &block) # puts "GET COUNT" if block run_block = Proc.new do |source| count = 0 source.cur.size.times do |index| val = source[index] result = block.call(val).cur if result == true count += 1 end end count end return ReactiveBlock.new(self, block, run_block) else @array.count(*args) end end |
#delete(val) ⇒ Object
93 94 95 |
# File 'lib/volt/reactive/reactive_array.rb', line 93 def delete(val) self.delete_at(@array.index(val)) end |
#delete_at(index) ⇒ Object
alias :__old_delete_at :delete_at
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/volt/reactive/reactive_array.rb', line 66 def delete_at(index) index_val = index.cur __clear_element(index) model = @array.delete_at(index_val) trigger_on_direct_listeners!('removed', index_val) # Trigger a changed event for each element in the zone where the # lookup would change index.upto(self.size+1) do |position| trigger_for_index!('changed', position) end trigger_size_change! @persistor.removed(model) if @persistor return model end |
#each(&block) ⇒ Object
At the moment, each just passes through.
25 26 27 |
# File 'lib/volt/reactive/reactive_array.rb', line 25 def each(&block) @array.each(&block) end |
#insert(index, *objects) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/volt/reactive/reactive_array.rb', line 143 def insert(index, *objects) result = @array.insert(index, *objects) # All objects from index to the end have "changed" index.upto(result.size-1) do |idx| trigger_for_index!('changed', idx) end objects.size.times do |count| trigger_on_direct_listeners!('added', index+count) end trigger_size_change! return result end |
#inspect ⇒ Object
239 240 241 |
# File 'lib/volt/reactive/reactive_array.rb', line 239 def inspect "#<#{self.class.to_s}:#{object_id} #{@array.inspect}>" end |
#reject(*args, &block) ⇒ Object
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/volt/reactive/reactive_array.rb', line 268 def reject(*args, &block) if block run_block = Proc.new do |source| puts "RUN REJECT" new_array = [] source.cur.size.times do |index| val = source[index] result = block.call(val).cur if result != true new_array << val.cur end end ReactiveArray.new(new_array) end return ReactiveBlock.new(self, block, run_block) else @array.count end end |
#split_scope(scope) ⇒ Object
TODO: This is an opal work around. Currently there is a bug with destructuring method_name, *args, block = scope
187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/volt/reactive/reactive_array.rb', line 187 def split_scope(scope) if scope scope = scope.dup method_name = scope.shift block = scope.pop return method_name, scope, block else return nil,[],nil end end |
#trigger_for_index!(event_name, index, *passed_args) ⇒ Object
Trigger the changed event to any values fetched either through the lookup ([]), #last, or any fetched through the array its self. (sum, max, etc…) On an array, when an element is added or removed, we need to trigger change events on each method that does the following:
-
uses the whole array (max, sum, etc…)
-
accesses this specific element - array
-
accesses an element via a method (first, last)
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/volt/reactive/reactive_array.rb', line 206 def trigger_for_index!(event_name, index, *passed_args) self.trigger_by_scope!(event_name, *passed_args) do |scope| # method_name, *args, block = scope method_name, args, block = split_scope(scope) result = case method_name when nil # no method name means the event was bound directly, we don't # want to trigger changed on the array its self. false when :[] # Extract the current index if its reactive arg_index = args[0].cur # TODO: we could handle negative indicies better arg_index == index.cur || arg_index < 0 when :last index.cur == self.size-1 when :first index.cur == 0 when :size, :length # Size does not depend on the contents of the cells false else true end result = false if method_name == :reject result end end |
#trigger_on_direct_listeners!(event, *args) ⇒ Object
160 161 162 163 164 165 166 167 |
# File 'lib/volt/reactive/reactive_array.rb', line 160 def trigger_on_direct_listeners!(event, *args) trigger_by_scope!(event, *args) do |scope| # Only if it is bound directly to us. Don't pass # down the chain !scope || scope[0] == nil end end |
#trigger_size_change! ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/volt/reactive/reactive_array.rb', line 169 def trigger_size_change! trigger_by_scope!('changed') do |scope| # method_name, *args, block = scope method_name, args, block = split_scope(scope) result = case method_name && method_name.to_sym when :size, :length true else false end result end end |