Class: Watchr::Script::Batch

Inherits:
Object
  • Object
show all
Defined in:
lib/watchr/script.rb

Instance Method Summary collapse

Constructor Details

#initialize(rule) ⇒ Batch

Returns a new instance of Batch.



23
24
25
26
27
# File 'lib/watchr/script.rb', line 23

def initialize rule
  @timer = nil
  @rule = rule
  @events = []
end

Instance Method Details

#call(data, event, path) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/watchr/script.rb', line 29

def call data, event, path
  if @timer
    @timer.cancel
  end
  @timer = EM::Timer.new(0.001) do
    deliver
    Watchr.batches.delete self
  end
  Watchr.batches[self] = self
  @events << [ data, event, path ]
end

#deliverObject



41
42
43
44
45
46
47
48
49
# File 'lib/watchr/script.rb', line 41

def deliver
  events = @events
  @timer = nil
  @events = []
  @rule.action.call [events]
  events.each do |event|
    Script.learn event[2]
  end
end