Class: EventedBluepill::Trigger

Inherits:
Object
  • Object
show all
Defined in:
lib/evented_bluepill/trigger.rb

Direct Known Subclasses

EventedBluepill::Triggers::Flapping

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(process, options = {}) ⇒ Trigger

Returns a new instance of Trigger.



37
38
39
40
41
# File 'lib/evented_bluepill/trigger.rb', line 37

def initialize(process, options = {})
  self.process = process
  self.logger = options[:logger]
  self.scheduled_events = []
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



35
36
37
# File 'lib/evented_bluepill/trigger.rb', line 35

def logger
  @logger
end

#processObject

Returns the value of attribute process.



35
36
37
# File 'lib/evented_bluepill/trigger.rb', line 35

def process
  @process
end

#scheduled_eventsObject

Returns the value of attribute scheduled_events.



35
36
37
# File 'lib/evented_bluepill/trigger.rb', line 35

def scheduled_events
  @scheduled_events
end

Class Method Details

.[](name) ⇒ Object



31
32
33
# File 'lib/evented_bluepill/trigger.rb', line 31

def self.[](name)
  @implementations[name]
end

.inherited(klass) ⇒ Object



27
28
29
# File 'lib/evented_bluepill/trigger.rb', line 27

def self.inherited(klass)
  @implementations[klass.name.split('::').last.underscore.to_sym] = klass
end

Instance Method Details

#cancel_all_eventsObject



66
67
68
69
70
# File 'lib/evented_bluepill/trigger.rb', line 66

def cancel_all_events
  self.logger.info "Canceling all scheduled events"
  self.scheduled_events.each {|e| e.detach }
  self.scheduled_events.clear
end

#dispatch!(event) ⇒ Object



51
52
53
# File 'lib/evented_bluepill/trigger.rb', line 51

def dispatch!(event)
  self.process.dispatch!(event, self.class.name.split("::").last)
end

#notify(transition) ⇒ Object



47
48
49
# File 'lib/evented_bluepill/trigger.rb', line 47

def notify(transition)
  raise "Implement in subclass"
end

#remove_timer(timer) ⇒ Object



55
56
57
# File 'lib/evented_bluepill/trigger.rb', line 55

def remove_timer(timer)
  self.scheduled_events.delete(timer)
end

#reset!Object



43
44
45
# File 'lib/evented_bluepill/trigger.rb', line 43

def reset!
  self.cancel_all_events
end

#schedule_event(event, delay) ⇒ Object



59
60
61
62
63
64
# File 'lib/evented_bluepill/trigger.rb', line 59

def schedule_event(event, delay)
  timer = EventedBluepill::TriggerTimer.new(self, event, delay)
  self.scheduled_events << timer
  
  EventedBluepill::Event.attach(timer)
end