Class: PrettyStateMachine::Machine
- Inherits:
-
Object
- Object
- PrettyStateMachine::Machine
- Defined in:
- lib/pretty_state_machine/machine.rb
Instance Attribute Summary collapse
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Class Method Summary collapse
- .initial_state ⇒ Object
- .state(name, options = {}) ⇒ Object
- .states ⇒ Object
- .transition(name, &block) ⇒ Object
- .transitions ⇒ Object
Instance Method Summary collapse
-
#initialize(state = self.class.initial_state) ⇒ Machine
constructor
A new instance of Machine.
Constructor Details
#initialize(state = self.class.initial_state) ⇒ Machine
Returns a new instance of Machine.
43 44 45 46 |
# File 'lib/pretty_state_machine/machine.rb', line 43 def initialize(state=self.class.initial_state) raise InvalidMachine.new('an initial state is required') if state.nil? @state = State(state) end |
Instance Attribute Details
#state ⇒ Object (readonly)
Returns the value of attribute state.
4 5 6 |
# File 'lib/pretty_state_machine/machine.rb', line 4 def state @state end |
Class Method Details
.initial_state ⇒ Object
14 15 16 |
# File 'lib/pretty_state_machine/machine.rb', line 14 def self.initial_state states.values.find(&:initial?) end |
.state(name, options = {}) ⇒ Object
18 19 20 21 22 |
# File 'lib/pretty_state_machine/machine.rb', line 18 def self.state(name, ={}) initial = .fetch(:initial) { false } state = State.new(name, initial: initial) states[name] = state end |
.states ⇒ Object
6 7 8 |
# File 'lib/pretty_state_machine/machine.rb', line 6 def self.states @_states ||= {} end |
.transition(name, &block) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/pretty_state_machine/machine.rb', line 24 def self.transition(name, &block) transition = Transition.new(self, name) transition.instance_eval(&block) if transition.to_state.nil? raise InvalidTransition, "transition '#{name}' requires an end state" end define_method(name) do if transition.permitted_from?(@state) @state = transition.to_state else raise InvalidTransition, "cannot transition to '#{transition.to_state}' via '#{name}' from '#{@state}'" end end transitions[name] = transition end |
.transitions ⇒ Object
10 11 12 |
# File 'lib/pretty_state_machine/machine.rb', line 10 def self.transitions @_transitions ||= {} end |