Class: EndState::StateMachine
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- EndState::StateMachine
show all
- Defined in:
- lib/end_state/state_machine.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/end_state/state_machine.rb', line 81
def method_missing(method, *args, &block)
check_state = method.to_s[0..-2].to_sym
return current_state?(check_state) if method.to_s.end_with?('?')
check_state = state_for_event(check_state) || check_state
return false if check_state == :__invalid_event__
return super unless self.class.states.include?(check_state)
if method.to_s.end_with?('!')
transition check_state, args[0]
else
super
end
end
|
Instance Attribute Details
#failure_messages ⇒ Object
Returns the value of attribute failure_messages.
3
4
5
|
# File 'lib/end_state/state_machine.rb', line 3
def failure_messages
@failure_messages
end
|
#success_messages ⇒ Object
Returns the value of attribute success_messages.
3
4
5
|
# File 'lib/end_state/state_machine.rb', line 3
def success_messages
@success_messages
end
|
Class Method Details
.end_states ⇒ Object
48
49
50
|
# File 'lib/end_state/state_machine.rb', line 48
def self.end_states
transitions.keys.map { |state_map| state_map.values.first }.uniq
end
|
.events ⇒ Object
31
32
33
|
# File 'lib/end_state/state_machine.rb', line 31
def self.events
@events ||= {}
end
|
.start_states ⇒ Object
44
45
46
|
# File 'lib/end_state/state_machine.rb', line 44
def self.start_states
transitions.keys.map { |state_map| state_map.keys.first }.uniq
end
|
.state_attribute(attribute) ⇒ Object
35
36
37
38
|
# File 'lib/end_state/state_machine.rb', line 35
def self.state_attribute(attribute)
define_method(:state) { send(attribute.to_sym) }
define_method(:state=) { |val| send("#{attribute}=".to_sym, val) }
end
|
.states ⇒ Object
40
41
42
|
# File 'lib/end_state/state_machine.rb', line 40
def self.states
(start_states + end_states).uniq
end
|
.store_states_as_strings ⇒ Object
9
10
11
|
# File 'lib/end_state/state_machine.rb', line 9
def self.store_states_as_strings
!!@store_states_as_strings
end
|
.store_states_as_strings! ⇒ Object
5
6
7
|
# File 'lib/end_state/state_machine.rb', line 5
def self.store_states_as_strings!
@store_states_as_strings = true
end
|
.transition(state_map) {|transition| ... } ⇒ Object
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/end_state/state_machine.rb', line 13
def self.transition(state_map)
initial_states = Array(state_map.keys.first)
final_state = state_map.values.first
transition_alias = state_map[:as] if state_map.keys.length > 1
transition = Transition.new(final_state)
initial_states.each do |state|
transitions[{ state.to_sym => final_state.to_sym }] = transition
end
unless transition_alias.nil?
events[transition_alias.to_sym] = initial_states.map { |s| { s.to_sym => final_state.to_sym } }
end
yield transition if block_given?
end
|
.transitions ⇒ Object
27
28
29
|
# File 'lib/end_state/state_machine.rb', line 27
def self.transitions
@transitions ||= {}
end
|
Instance Method Details
#can_transition?(state, params = {}) ⇒ Boolean
56
57
58
59
60
61
62
|
# File 'lib/end_state/state_machine.rb', line 56
def can_transition?(state, params = {})
previous_state = self.state.to_sym
state = state.to_sym
transition = self.class.transitions[{ previous_state => state }]
return block_transistion(transition, state, :soft) unless transition
transition.will_allow? state, params
end
|
#object ⇒ Object
52
53
54
|
# File 'lib/end_state/state_machine.rb', line 52
def object
__getobj__
end
|
#transition(state, params = {}, mode = :soft) ⇒ Object
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/end_state/state_machine.rb', line 64
def transition(state, params = {}, mode = :soft)
@failure_messages = []
@success_messages = []
previous_state = self.state ? self.state.to_sym : self.state
state = state.to_sym
transition = self.class.transitions[{ previous_state => state }]
return block_transistion(transition, state, mode) unless transition
return guard_failed(state, mode) unless transition.allowed?(self, params)
return false unless transition.action.new(self, state).call
return finalize_failed(state, mode) unless transition.finalize(self, previous_state, params)
true
end
|
#transition!(state, params = {}) ⇒ Object
77
78
79
|
# File 'lib/end_state/state_machine.rb', line 77
def transition!(state, params = {})
transition state, params, :hard
end
|