Class: SimpleStateMachine::Transition

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_state_machine/transition.rb

Overview

Defines transitions for events

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event_name, from, to) ⇒ Transition

Returns a new instance of Transition.



5
6
7
8
9
# File 'lib/simple_state_machine/transition.rb', line 5

def initialize(event_name, from, to)
  @event_name = event_name.to_s
  @from       = from.is_a?(Class) ? from : from.to_s
  @to         = to.to_s
end

Instance Attribute Details

#event_nameObject (readonly)

Returns the value of attribute event_name.



4
5
6
# File 'lib/simple_state_machine/transition.rb', line 4

def event_name
  @event_name
end

#fromObject (readonly)

Returns the value of attribute from.



4
5
6
# File 'lib/simple_state_machine/transition.rb', line 4

def from
  @from
end

#toObject (readonly)

Returns the value of attribute to.



4
5
6
# File 'lib/simple_state_machine/transition.rb', line 4

def to
  @to
end

Instance Method Details

#is_error_transition_for?(event_name, error) ⇒ Boolean

returns true if it’s a error transition for event_name and error

Returns:

  • (Boolean)


17
18
19
# File 'lib/simple_state_machine/transition.rb', line 17

def is_error_transition_for?(event_name, error)
  is_same_event?(event_name) && from.is_a?(Class) && error.is_a?(from)
end

#is_transition_for?(event_name, subject_state) ⇒ Boolean

returns true if it’s a transition for event_name and subject_state

Returns:

  • (Boolean)


12
13
14
# File 'lib/simple_state_machine/transition.rb', line 12

def is_transition_for?(event_name, subject_state)
  is_same_event?(event_name) && is_same_from?(subject_state)
end

#to_graphviz_dotObject

TODO move to Graphiz module



26
27
28
# File 'lib/simple_state_machine/transition.rb', line 26

def to_graphviz_dot
  %("#{from}"->"#{to}"[label=#{event_name}])
end

#to_sObject



21
22
23
# File 'lib/simple_state_machine/transition.rb', line 21

def to_s
  "#{from}.#{event_name}! => #{to}"
end