Class: StateManager::State
- Inherits:
-
Object
- Object
- StateManager::State
show all
- Extended by:
- DSL::State
- Includes:
- DelayedJob::State
- Defined in:
- lib/state_manager/dsl.rb,
lib/state_manager/state.rb,
lib/state_manager/plugins/delayed_job.rb
Direct Known Subclasses
Base
Defined Under Namespace
Classes: Specification
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from DSL::State
event, state
#delayed_events
Constructor Details
#initialize(name, parent_state) ⇒ State
Returns a new instance of State.
35
36
37
38
39
40
41
42
|
# File 'lib/state_manager/state.rb', line 35
def initialize(name, parent_state)
self.name = name
self.parent_state = parent_state
self.states = self.class.specification.states.inject({}) do |states, (name, klazz)|
states[name] = klazz.new(name, self)
states
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
146
147
148
|
# File 'lib/state_manager/state.rb', line 146
def method_missing(name, *args, &block)
resource.send(name, *args, &block)
end
|
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
33
34
35
|
# File 'lib/state_manager/state.rb', line 33
def name
@name
end
|
#parent_state ⇒ Object
Returns the value of attribute parent_state.
33
34
35
|
# File 'lib/state_manager/state.rb', line 33
def parent_state
@parent_state
end
|
#states ⇒ Object
Returns the value of attribute states.
33
34
35
|
# File 'lib/state_manager/state.rb', line 33
def states
@states
end
|
Class Method Details
.create_resource_accessor!(name) ⇒ Object
133
134
135
136
137
138
139
140
|
# File 'lib/state_manager/state.rb', line 133
def self.create_resource_accessor!(name)
unless method_defined?(name)
define_method name do
resource
end
end
specification.states.values.each {|s|s.create_resource_accessor!(name)}
end
|
.inherited(child) ⇒ Object
26
27
28
29
30
31
|
# File 'lib/state_manager/state.rb', line 26
def self.inherited(child)
child.specification = specification.clone
end
|
Instance Method Details
#enter ⇒ Object
52
53
|
# File 'lib/state_manager/state.rb', line 52
def enter
end
|
#entered ⇒ Object
58
59
|
# File 'lib/state_manager/state.rb', line 58
def entered
end
|
#exit ⇒ Object
55
56
|
# File 'lib/state_manager/state.rb', line 55
def exit
end
|
#exited ⇒ Object
61
62
|
# File 'lib/state_manager/state.rb', line 61
def exited
end
|
#find_state(path) ⇒ Object
Returns the state at the given path
112
113
114
115
|
# File 'lib/state_manager/state.rb', line 112
def find_state(path)
states = find_states(path)
states && states.last
end
|
#find_states(path) ⇒ Object
Find all the states along the path
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/state_manager/state.rb', line 99
def find_states(path)
state = self
parts = path.split('.')
ret = []
parts.each do |name|
state = state.states[name.to_sym]
ret << state
return unless state
end
ret
end
|
#has_event?(name) ⇒ Boolean
85
86
87
88
|
# File 'lib/state_manager/state.rb', line 85
def has_event?(name)
name = name.to_sym
!!self.class.specification.events[name]
end
|
#initial_state ⇒ Object
If an initial state is not explicitly specified, we choose the first leaf
state
123
124
125
126
127
128
129
130
131
|
# File 'lib/state_manager/state.rb', line 123
def initial_state
if state = self.class.specification.initial_state
find_state(state.to_s)
elsif leaf?
self
else
states.values.first.initial_state
end
end
|
#leaf? ⇒ Boolean
117
118
119
|
# File 'lib/state_manager/state.rb', line 117
def leaf?
states.empty?
end
|
#path ⇒ Object
String representing the path of the current state, e.g.:
'parentState.childState'
46
47
48
49
50
|
# File 'lib/state_manager/state.rb', line 46
def path
path = name.to_s
path = "#{parent_state.path}.#{path}" if parent_state && parent_state.name
path
end
|
90
91
92
93
94
95
96
|
# File 'lib/state_manager/state.rb', line 90
def perform_event(name, *args)
name = name.to_sym
event = self.class.specification.events[name]
result = send(name, *args) if respond_to?(name)
transition_to(event[:transitions_to]) if event[:transitions_to]
result
end
|
#resource ⇒ Object
Get the resource stored on the state manager
77
78
79
|
# File 'lib/state_manager/state.rb', line 77
def resource
state_manager.resource
end
|
#state_manager ⇒ Object
72
73
74
|
# File 'lib/state_manager/state.rb', line 72
def state_manager
parent_state.state_manager
end
|
#to_s ⇒ Object
64
65
66
|
# File 'lib/state_manager/state.rb', line 64
def to_s
"#{path}"
end
|
#to_sym ⇒ Object
68
69
70
|
# File 'lib/state_manager/state.rb', line 68
def to_sym
path.to_sym
end
|
#transition_to(*args) ⇒ Object
81
82
83
|
# File 'lib/state_manager/state.rb', line 81
def transition_to(*args)
state_manager.transition_to(*args)
end
|