Class: MotionWiretap::WiretapArray

Inherits:
Wiretap
  • Object
show all
Defined in:
lib/motion-wiretap/all/wiretap.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Wiretap

#and_then, #combine, #enqueue, #filter, #listen, #map, #on_error, #queue, #reduce, #trigger_changed_on, #trigger_completed, #trigger_completed_on, #trigger_error, #trigger_error_on

Constructor Details

#initialize(targets, &block) ⇒ WiretapArray

Returns a new instance of WiretapArray.



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/motion-wiretap/all/wiretap.rb', line 247

def initialize(targets, &block)
  raise "Not only is listening to an empty array pointless, it will also cause errors" if targets.length == 0

  # the complete trigger isn't called until all the wiretap are complete
  @uncompleted = targets.length

  # targets can be an array of Wiretap objects (they will be monitored), or
  # plain objects (they'll just be included in the sequence)
  super(&block)

  # gets assigned to the wiretap value if it's a Wiretap, or the object
  # itself if it is anything else.
  @value = []
  @initial_is_set = true
  # maps the wiretap object (which is unique)
  @wiretaps = {}

  targets.each_with_index do |wiretap,index|
    unless wiretap.is_a? Wiretap
      @value << wiretap
      # not a wiretap, so doesn't need to be "completed"
      @uncompleted -= 1
    else
      raise "You cannot store a Wiretap twice in the same sequence (for now - do you really need this?)" if @wiretaps.key?(wiretap)
      @wiretaps[wiretap] = index

      @value << wiretap.value

      wiretap.listen do |value|
        indx = @wiretaps[wiretap]
        @value[index] = wiretap.value
        trigger_changed(*@value)
      end

      wiretap.on_error do |error|
        trigger_error(error)
      end

      wiretap.and_then do |error|
        @uncompleted -= 1
        if @uncompleted == 0
          trigger_completed
        end
      end
    end
  end
end

Instance Attribute Details

#targetsObject (readonly)

Returns the value of attribute targets.



245
246
247
# File 'lib/motion-wiretap/all/wiretap.rb', line 245

def targets
  @targets
end

Instance Method Details

#cancel!Object



295
296
297
298
299
300
# File 'lib/motion-wiretap/all/wiretap.rb', line 295

def cancel!
  @wiretaps.each do |wiretap,index|
    wiretap.cancel!
  end
  super
end

#teardownObject



302
303
304
305
# File 'lib/motion-wiretap/all/wiretap.rb', line 302

def teardown
  @wiretaps = nil
  super
end

#trigger_changed(*values) ⇒ Object



307
308
309
310
# File 'lib/motion-wiretap/all/wiretap.rb', line 307

def trigger_changed(*values)
  values = @value if values.length == 0
  super(*values)
end