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

Returns a new instance of Passenger.



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

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

#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



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

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

#current_state=(value) ⇒ Object



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

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)


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

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

#transition_to(new_state, state_jacket: nil) ⇒ Object



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

def transition_to(new_state, state_jacket: nil)
  if !state_jacket.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, state_jacket: state_jacket)
  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