Module: Statesmin::Machine::ClassMethods

Defined in:
lib/statesmin/machine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#initial_stateObject (readonly)

Returns the value of attribute initial_state.



27
28
29
# File 'lib/statesmin/machine.rb', line 27

def initial_state
  @initial_state
end

Instance Method Details

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



78
79
80
81
82
83
# File 'lib/statesmin/machine.rb', line 78

def after_transition(options = { after_commit: false }, &block)
  callback_type = options[:after_commit] ? :after_commit : :after

  add_callback(callback_type: callback_type, callback_class: Callback,
               from: options[:from], to: options[:to], &block)
end

#before_transition(options = {}, &block) ⇒ Object



68
69
70
71
# File 'lib/statesmin/machine.rb', line 68

def before_transition(options = {}, &block)
  add_callback(callback_type: :before, callback_class: Callback,
               from: options[:from], to: options[:to], &block)
end

#callbacksObject



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

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

#guard_transition(options = {}, &block) ⇒ Object



73
74
75
76
# File 'lib/statesmin/machine.rb', line 73

def guard_transition(options = {}, &block)
  add_callback(callback_type: :guards, callback_class: Guard,
               from: options[:from], to: options[:to], &block)
end

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



33
34
35
36
37
38
39
40
# File 'lib/statesmin/machine.rb', line 33

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



29
30
31
# File 'lib/statesmin/machine.rb', line 29

def states
  @states ||= []
end

#successorsObject



42
43
44
# File 'lib/statesmin/machine.rb', line 42

def successors
  @successors ||= {}
end

#transition(from: nil, to: nil) ⇒ Object

Raises:



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/statesmin/machine.rb', line 55

def transition(from: nil, to: nil)
  from = to_s_or_nil(from)
  to = array_to_s_or_nil(to)

  raise InvalidStateError, "No to states provided." if to.empty?

  successors[from] ||= []

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

  successors[from] += to
end

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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/statesmin/machine.rb', line 85

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

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

  validate_not_from_terminal_state(from)
  to.each { |state| validate_not_to_initial_state(state) }

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

  to.each { |state| validate_from_and_to_state(from, state) }
end

#validate_from_and_to_state(from, to) ⇒ Object

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



117
118
119
120
121
122
# File 'lib/statesmin/machine.rb', line 117

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



101
102
103
104
105
106
# File 'lib/statesmin/machine.rb', line 101

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



109
110
111
112
113
114
# File 'lib/statesmin/machine.rb', line 109

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

#validate_state(state) ⇒ Object



124
125
126
127
128
# File 'lib/statesmin/machine.rb', line 124

def validate_state(state)
  unless states.include?(state.to_s)
    raise InvalidStateError, "Invalid state '#{state}'"
  end
end