Class: Ellington::Conductor

Inherits:
Object
  • Object
show all
Defined in:
lib/ellington/conductor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(route) ⇒ Conductor

Returns a new instance of Conductor.



7
8
9
10
# File 'lib/ellington/conductor.rb', line 7

def initialize(route)
  @route = route
  @conducting = false
end

Instance Attribute Details

#routeObject (readonly)

Returns the value of attribute route.



5
6
7
# File 'lib/ellington/conductor.rb', line 5

def route
  @route
end

Instance Method Details

#conducting?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/ellington/conductor.rb', line 12

def conducting?
  @conducting
end

#escort(passenger) ⇒ Object



53
54
55
56
# File 'lib/ellington/conductor.rb', line 53

def escort(passenger)
  return unless verify(passenger) && passenger.locked?
  route.lines.first.board passenger
end

#gather_passengersObject



49
50
51
# File 'lib/ellington/conductor.rb', line 49

def gather_passengers
  raise Ellington::NotImplementedError
end

#startObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ellington/conductor.rb', line 16

def start
  return if conducting?

  mutex.synchronize do
    @stop = false
    @conducting = true
  end

  @thread = Thread.new do
    loop do
      if @stop
        mutex.synchronize { @conducting = false }
        Thread.current.exit
      end
      gather_passengers.each do |passenger|
        escort(passenger)
      end
    end
  end
end

#stopObject



37
38
39
# File 'lib/ellington/conductor.rb', line 37

def stop
  mutex.synchronize { @stop = true }
end

#verify(passenger) ⇒ Object



45
46
47
# File 'lib/ellington/conductor.rb', line 45

def verify(passenger)
  true
end

#waitObject



41
42
43
# File 'lib/ellington/conductor.rb', line 41

def wait
  thread.join
end