Class: Lomic::EventEngine

Inherits:
Object
  • Object
show all
Defined in:
lib/lomic/EventEngine.rb

Instance Method Summary collapse

Constructor Details

#initializeEventEngine

Returns a new instance of EventEngine.



5
6
7
8
# File 'lib/lomic/EventEngine.rb', line 5

def initialize
  @stack = []
  @counter = 0 # the number of event code blocks executed
end

Instance Method Details

#counterObject



46
47
48
# File 'lib/lomic/EventEngine.rb', line 46

def counter
  @counter
end

#listen(*valid_events) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/lomic/EventEngine.rb', line 25

def listen(*valid_events)
  puts "Listening for valid events: #{valid_events.inspect}" if Lomic.verbose?
  res = @socket.gets.strip!
  if not valid_events.include? res
    result = {:status => 'fail', :reason => 'invalid event'}.to_json
    @socket.puts result
  else
    result = {:status => 'ok'}.to_json
    @socket.puts result
  end
  set_next res
end

#next_eventObject



42
43
44
# File 'lib/lomic/EventEngine.rb', line 42

def next_event
  @next_event
end

#run(event_name, rules, socket) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/lomic/EventEngine.rb', line 10

def run(event_name, rules, socket)
  @socket = socket
  add_sort_events rules
  @next_event = event_name

  begin
    event_name = @next_event
    @next_event = nil
    for event in @events[event_name]
      # event code blocks can set @next_event through set_next
      instance_eval &event.block
    end
  end while @next_event.nil? == false
end

#set_next(event_name) ⇒ Object



38
39
40
# File 'lib/lomic/EventEngine.rb', line 38

def set_next(event_name)
  @next_event = event_name
end