Class: Ellington::Passenger

Inherits:
SimpleDelegator
  • Object
show all
Includes:
Observable
Defined in:
lib/ellington/passenger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, route, ticket = nil) ⇒ Passenger

Returns a new instance of Passenger.



9
10
11
12
13
14
15
# File 'lib/ellington/passenger.rb', line 9

def initialize(context, route, ticket=nil)
  ticket ||= Ellington::Ticket.new
  @context = context
  @route = route
  @ticket = ticket
  super context
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



6
7
8
# File 'lib/ellington/passenger.rb', line 6

def context
  @context
end

#routeObject (readonly)

Returns the value of attribute route.



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

def route
  @route
end

#ticketObject

Returns the value of attribute ticket.



6
7
8
# File 'lib/ellington/passenger.rb', line 6

def ticket
  @ticket
end

Instance Method Details

#current_stateObject



32
33
34
35
# File 'lib/ellington/passenger.rb', line 32

def current_state
  return context.current_state if context.respond_to?(:current_state)
  @current_state
end

#current_state=(value) ⇒ Object



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

def current_state=(value)
  return context.current_state=(value) if context.respond_to?(:current_state=)
  @current_state = value
end

#lockObject



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

def lock
  return context.lock if context.respond_to?(:lock)
  @locked = true
end

#locked?Boolean

Returns:

  • (Boolean)


27
28
29
30
# File 'lib/ellington/passenger.rb', line 27

def locked?
  return context.locked? if context.respond_to?(:locked?)
  @locked
end

#state_historyObject



42
43
44
# File 'lib/ellington/passenger.rb', line 42

def state_history
  @state_history ||= []
end

#transition_to(new_state) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ellington/passenger.rb', line 46

def transition_to(new_state)
  if !locked?
    message = "Cannot transition an unlocked #{self.class.name}'s state"
    raise Ellington::InvalidStateTransition.new(message)
  end

  if !route.states.can_transition?(current_state => new_state)
    message = "Cannot transition #{self.class.name} from:#{current_state} to:#{new_state}"
    raise Ellington::InvalidStateTransition.new(message)
  end

  old_state = current_state

  if context.respond_to?(:transition_to)
    return_value = context.transition_to(new_state) 
  else
    self.current_state = new_state
  end

  state_history << new_state

  changed
  notify_observers Ellington::TransitionInfo.new(self, old_state, new_state)
  return_value || new_state
end

#unlockObject



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

def unlock
  return context.unlock if context.respond_to?(:unlock)
  @locked = false
end