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.
44
45
46
47
48
49
50
51
|
# File 'lib/state_manager/state.rb', line 44
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
155
156
157
|
# File 'lib/state_manager/state.rb', line 155
def method_missing(name, *args, &block)
resource.send(name, *args, &block)
end
|
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
42
43
44
|
# File 'lib/state_manager/state.rb', line 42
def name
@name
end
|
#parent_state ⇒ Object
Returns the value of attribute parent_state.
42
43
44
|
# File 'lib/state_manager/state.rb', line 42
def parent_state
@parent_state
end
|
#states ⇒ Object
Returns the value of attribute states.
42
43
44
|
# File 'lib/state_manager/state.rb', line 42
def states
@states
end
|
Class Method Details
.create_resource_accessor!(name) ⇒ Object
142
143
144
145
146
147
148
149
|
# File 'lib/state_manager/state.rb', line 142
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
35
36
37
38
39
40
|
# File 'lib/state_manager/state.rb', line 35
def self.inherited(child)
child.specification = specification.clone
end
|
Instance Method Details
#enter ⇒ Object
61
62
|
# File 'lib/state_manager/state.rb', line 61
def enter
end
|
#entered ⇒ Object
67
68
|
# File 'lib/state_manager/state.rb', line 67
def entered
end
|
#exit ⇒ Object
64
65
|
# File 'lib/state_manager/state.rb', line 64
def exit
end
|
#exited ⇒ Object
70
71
|
# File 'lib/state_manager/state.rb', line 70
def exited
end
|
#find_state(path) ⇒ Object
Returns the state at the given path
121
122
123
124
|
# File 'lib/state_manager/state.rb', line 121
def find_state(path)
states = find_states(path)
states && states.last
end
|
#find_states(path) ⇒ Object
Find all the states along the path
108
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/state_manager/state.rb', line 108
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
94
95
96
97
|
# File 'lib/state_manager/state.rb', line 94
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
132
133
134
135
136
137
138
139
140
|
# File 'lib/state_manager/state.rb', line 132
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
126
127
128
|
# File 'lib/state_manager/state.rb', line 126
def leaf?
states.empty?
end
|
#path ⇒ Object
String representing the path of the current state, e.g.: ‘parentState.childState’
55
56
57
58
59
|
# File 'lib/state_manager/state.rb', line 55
def path
path = name.to_s
path = "#{parent_state.path}.#{path}" if parent_state && parent_state.name
path
end
|
99
100
101
102
103
104
105
|
# File 'lib/state_manager/state.rb', line 99
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
86
87
88
|
# File 'lib/state_manager/state.rb', line 86
def resource
state_manager.resource
end
|
#state_manager ⇒ Object
81
82
83
|
# File 'lib/state_manager/state.rb', line 81
def state_manager
parent_state.state_manager
end
|
#to_s ⇒ Object
73
74
75
|
# File 'lib/state_manager/state.rb', line 73
def to_s
"#{path}"
end
|
#to_sym ⇒ Object
77
78
79
|
# File 'lib/state_manager/state.rb', line 77
def to_sym
path.to_sym
end
|
#transition_to(*args) ⇒ Object
90
91
92
|
# File 'lib/state_manager/state.rb', line 90
def transition_to(*args)
state_manager.transition_to(*args)
end
|