Class: Baldrick::Command

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/baldrick/command.rb

Instance Method Summary collapse

Constructor Details

#initializeCommand

Returns a new instance of Command.



7
8
9
10
11
12
# File 'lib/baldrick/command.rb', line 7

def initialize
  @listener_classes = {:injour => Listeners::InjourListener, :feed => Listeners::FeedListener}
  @servant = Servant.new
  @wait_period = 2
  @should_serve = true  
end

Instance Method Details

#execute(stdout) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/baldrick/command.rb', line 14

def execute(stdout)
  stdout << "Servant started...\n"; stdout.flush   
  while should_serve? do
    @servant.serve
    sleep @wait_period
  end
end

#listen_every(period) ⇒ Object



30
31
32
# File 'lib/baldrick/command.rb', line 30

def listen_every period
  @wait_period = period
end

#listen_to(listener_type, options = {}) ⇒ Object



34
35
36
37
# File 'lib/baldrick/command.rb', line 34

def listen_to listener_type, options={}
  listener_class = listener_class_for(listener_type) || raise("No order listener implementation found for #{listener_type}")
  @servant.add_listener(listener_class.new(options))
end

#listener_class_for(type) ⇒ Object



48
49
50
# File 'lib/baldrick/command.rb', line 48

def listener_class_for type
  @listener_classes[type]
end

#on_hearing(matcher, &procedure) ⇒ Object Also known as: to



39
40
41
# File 'lib/baldrick/command.rb', line 39

def on_hearing(matcher, &procedure)
  @servant.add_task Task.new(matcher, procedure)
end

#register_listener_type(type, listener_class) ⇒ Object



44
45
46
# File 'lib/baldrick/command.rb', line 44

def register_listener_type type, listener_class
  @listener_classes[type] = listener_class
end

#should_serve?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/baldrick/command.rb', line 22

def should_serve?
  @should_serve
end

#stop!Object



26
27
28
# File 'lib/baldrick/command.rb', line 26

def stop!
  @should_serve = false  
end