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: route, ticket: Ellington::Ticket.new, state_history: []) ⇒ 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: route, ticket: Ellington::Ticket.new, state_history: [])
  @context = context
  @route = route
  @ticket = ticket
  @state_history = state_history
  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

#state_historyObject (readonly)

Returns the value of attribute state_history.



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

def state_history
  @state_history
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



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

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

#current_state=(value) ⇒ Object



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

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

#state_history_includes?(*states) ⇒ Boolean

Returns:

  • (Boolean)


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

def state_history_includes?(*states)
  (state_history & states).length == states.length
end

#transition_to(new_state) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ellington/passenger.rb', line 31

def transition_to(new_state)
  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