Module: Statesman::Machine::ClassMethods

Defined in:
lib/statesman/machine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#initial_stateObject (readonly)

Returns the value of attribute initial_state.



16
17
18
# File 'lib/statesman/machine.rb', line 16

def initial_state
  @initial_state
end

Instance Method Details

#after_transition(options = { from: nil, to: nil, after_commit: false }, &block) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/statesman/machine.rb', line 63

def after_transition(options = { from: nil, to: nil,
                                 after_commit: false }, &block)
  from = to_s_or_nil(options[:from])
  to   = to_s_or_nil(options[:to])

  validate_callback_condition(from: from, to: to)
  phase = options[:after_commit] ? :after_commit : :after
  callbacks[phase] << Callback.new(from: from, to: to, callback: block)
end

#before_transition(options = { from: nil, to: nil }, &block) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/statesman/machine.rb', line 55

def before_transition(options = { from: nil, to: nil }, &block)
  from = to_s_or_nil(options[:from])
  to   = to_s_or_nil(options[:to])

  validate_callback_condition(from: from, to: to)
  callbacks[:before] << Callback.new(from: from, to: to, callback: block)
end

#callbacksObject



35
36
37
38
39
40
41
42
# File 'lib/statesman/machine.rb', line 35

def callbacks
  @callbacks ||= {
    before:       [],
    after:        [],
    after_commit: [],
    guards:       []
  }
end

#guard_transition(options = { from: nil, to: nil }, &block) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/statesman/machine.rb', line 73

def guard_transition(options = { from: nil, to: nil }, &block)
  from = to_s_or_nil(options[:from])
  to   = to_s_or_nil(options[:to])

  validate_callback_condition(from: from, to: to)
  callbacks[:guards] << Guard.new(from: from, to: to, callback: block)
end

#state(name, options = { initial: false }) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/statesman/machine.rb', line 22

def state(name, options = { initial: false })
  name = name.to_s
  if options[:initial]
    validate_initial_state(name)
    @initial_state = name
  end
  states << name
end

#statesObject



18
19
20
# File 'lib/statesman/machine.rb', line 18

def states
  @states ||= []
end

#successorsObject



31
32
33
# File 'lib/statesman/machine.rb', line 31

def successors
  @successors ||= {}
end

#transition(options = { from: nil, to: nil }) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/statesman/machine.rb', line 44

def transition(options = { from: nil, to: nil })
  from = to_s_or_nil(options[:from])
  to = Array(options[:to]).map { |item| to_s_or_nil(item) }

  successors[from] ||= []

  ([from] + to).each { |state| validate_state(state) }

  successors[from] += to
end

#validate_callback_condition(options = { from: nil, to: nil }) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/statesman/machine.rb', line 81

def validate_callback_condition(options = { from: nil, to: nil })
  from = to_s_or_nil(options[:from])
  to   = to_s_or_nil(options[:to])

  [from, to].compact.each { |state| validate_state(state) }
  return if from.nil? && to.nil?

  validate_not_from_terminal_state(from)
  validate_not_to_initial_state(to)

  return if from.nil? || to.nil?

  validate_from_and_to_state(from, to)
end

#validate_from_and_to_state(from, to) ⇒ Object

Check that the transition is valid when ‘from’ and ‘to’ are given



113
114
115
116
117
118
# File 'lib/statesman/machine.rb', line 113

def validate_from_and_to_state(from, to)
  unless successors.fetch(from, []).include?(to)
    raise InvalidTransitionError,
          "Cannot transition from '#{from}' to '#{to}'"
  end
end

#validate_not_from_terminal_state(from) ⇒ Object

Check that the ‘from’ state is not terminal



97
98
99
100
101
102
# File 'lib/statesman/machine.rb', line 97

def validate_not_from_terminal_state(from)
  unless from.nil? || successors.keys.include?(from)
    raise InvalidTransitionError,
          "Cannot transition away from terminal state '#{from}'"
  end
end

#validate_not_to_initial_state(to) ⇒ Object

Check that the ‘to’ state is not initial



105
106
107
108
109
110
# File 'lib/statesman/machine.rb', line 105

def validate_not_to_initial_state(to)
  unless to.nil? || successors.values.flatten.include?(to)
    raise InvalidTransitionError,
          "Cannot transition to initial state '#{to}'"
  end
end