Class: BeerBot::Utils::InOut

Inherits:
Object
  • Object
show all
Defined in:
lib/beerbot/00.utils/InOut.rb

Overview

Represents a thread that waits on an in-queue, processes any things received and puts them on an out-queue.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inq: nil, outq: nil, &block) ⇒ InOut

Returns a new instance of InOut.



18
19
20
21
22
23
# File 'lib/beerbot/00.utils/InOut.rb', line 18

def initialize inq:nil,outq:nil,&block
  @inq = inq
  @outq = outq
  @run = block
  raise "No block given" unless block_given?
end

Instance Attribute Details

#inqObject (readonly)

Returns the value of attribute inq.



16
17
18
# File 'lib/beerbot/00.utils/InOut.rb', line 16

def inq
  @inq
end

#outqObject (readonly)

Returns the value of attribute outq.



16
17
18
# File 'lib/beerbot/00.utils/InOut.rb', line 16

def outq
  @outq
end

#runObject (readonly)

Returns the value of attribute run.



16
17
18
# File 'lib/beerbot/00.utils/InOut.rb', line 16

def run
  @run
end

#threadObject (readonly)

Returns the value of attribute thread.



16
17
18
# File 'lib/beerbot/00.utils/InOut.rb', line 16

def thread
  @thread
end

Instance Method Details

#start!Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/beerbot/00.utils/InOut.rb', line 25

def start!
  @thread = Thread.new {
    loop {
      begin
        thing = @inq.deq
        response = @run.call(thing)
        if response then
          @outq.enq(response)
        else
          # TODO
        end
      rescue => e
        puts e
        puts e.backtrace
      end
    }
  }
end