Class: Ellington::Station

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Observable
Defined in:
lib/ellington/station.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#lineObject

Returns the value of attribute line.



9
10
11
# File 'lib/ellington/station.rb', line 9

def line
  @line
end

#line_classObject

Returns the value of attribute line_class.



9
10
11
# File 'lib/ellington/station.rb', line 9

def line_class
  @line_class
end

Instance Method Details

#call(passenger, _ = nil) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ellington/station.rb', line 72

def call(passenger, _=nil)
  if can_engage?(passenger)
    attendant = Ellington::Attendant.new(self)
    passenger.add_observer attendant
    engage_and_transition passenger
    passenger.delete_observer attendant
    raise Ellington::AttendantDisapproves unless attendant.approve?
    changed
    notify_observers Ellington::StationInfo.new(self, passenger, attendant.passenger_transitions.first)
  end

  passenger
end

#can_engage?(passenger) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
# File 'lib/ellington/station.rb', line 49

def can_engage?(passenger)
  return false unless route.states.can_transition?(passenger.current_state => states.keys)
  return false if passenger.state_history_includes?(passed)
  true
end

#engage(passenger) ⇒ Object



55
56
57
# File 'lib/ellington/station.rb', line 55

def engage(passenger)
  raise Ellington::NotImplementedError
end

#engage_and_transition(passenger) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ellington/station.rb', line 59

def engage_and_transition(passenger)
  begin
    if !!engage(passenger)
      pass_passenger passenger
    else
      fail_passenger passenger
    end
  rescue StandardError => e
    Ellington.logger.error "Failure while engaging passenger! #{e}" if Ellington.logger
    error_passenger passenger
  end
end

#error_passenger(passenger) ⇒ Object



94
95
96
# File 'lib/ellington/station.rb', line 94

def error_passenger(passenger)
  passenger.transition_to errored
end

#erroredObject



34
35
36
# File 'lib/ellington/station.rb', line 34

def errored
  @error_state ||= state_name(:error)
end

#fail_passenger(passenger) ⇒ Object



90
91
92
# File 'lib/ellington/station.rb', line 90

def fail_passenger(passenger)
  passenger.transition_to failed
end

#failedObject



30
31
32
# File 'lib/ellington/station.rb', line 30

def failed
  @fail_state ||= state_name(:fail)
end

#initial_statesObject



12
13
14
15
16
# File 'lib/ellington/station.rb', line 12

def initial_states
  route.states.keys.select do |state|
    route.states.can_transition?(state => states.keys)
  end
end

#nameObject



18
19
20
# File 'lib/ellington/station.rb', line 18

def name
  @name ||= "#{line_class.name} #{self.class.name}"
end

#pass_passenger(passenger) ⇒ Object



86
87
88
# File 'lib/ellington/station.rb', line 86

def pass_passenger(passenger)
  passenger.transition_to passed
end

#passedObject



26
27
28
# File 'lib/ellington/station.rb', line 26

def passed
  @pass_state ||= state_name(:pass)
end

#state(passenger) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/ellington/station.rb', line 98

def state(passenger)
  case passenger.current_state
  when passed then "PASS"
  when failed then "FAIL"
  when errored then "ERROR"
  end
end

#state_name(state) ⇒ Object



22
23
24
# File 'lib/ellington/station.rb', line 22

def state_name(state)
  "#{state.to_s.upcase} #{name}"
end

#statesObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/ellington/station.rb', line 38

def states
  @states ||= begin
    catalog = StateJacket::Catalog.new
    catalog.add passed
    catalog.add failed
    catalog.add errored => [ passed, failed, errored ]
    catalog.lock
    catalog
  end
end