Class: Statum::Machine
- Inherits:
-
Object
- Object
- Statum::Machine
- Defined in:
- lib/statum/machine.rb
Overview
Class for representing event machine
Instance Attribute Summary collapse
-
#events ⇒ Hash
readonly
Events.
-
#field ⇒ Symbol
(also: #name)
readonly
State field.
-
#states ⇒ Array<Symbol>
readonly
States.
Instance Method Summary collapse
-
#current(instance) ⇒ Symbol
Returns current state of instance.
-
#event?(name) ⇒ Boolean
Checks if event present.
-
#fire!(instance, name) ⇒ Object
Execute an event.
-
#initialize(options) ⇒ Machine
constructor
Creates machine instance.
-
#state?(name) ⇒ Boolean
Checks if state present.
Constructor Details
#initialize(options) ⇒ Machine
Creates machine instance
20 21 22 23 24 25 |
# File 'lib/statum/machine.rb', line 20 def initialize() @field = .delete(:field) @initial = .delete(:initial) @states = .delete(:states) @events = .delete(:events) end |
Instance Attribute Details
#events ⇒ Hash (readonly)
Events
7 8 9 |
# File 'lib/statum/machine.rb', line 7 def events @events end |
#field ⇒ Symbol (readonly) Also known as: name
State field
7 8 9 |
# File 'lib/statum/machine.rb', line 7 def field @field end |
#states ⇒ Array<Symbol> (readonly)
States
7 8 9 |
# File 'lib/statum/machine.rb', line 7 def states @states end |
Instance Method Details
#current(instance) ⇒ Symbol
Returns current state of instance
72 73 74 75 |
# File 'lib/statum/machine.rb', line 72 def current(instance) value = instance.send(field) value.nil? ? @initial : value.to_sym end |
#event?(name) ⇒ Boolean
Checks if event present
41 42 43 |
# File 'lib/statum/machine.rb', line 41 def event?(name) @events.keys.include?(name.to_sym) end |
#fire!(instance, name) ⇒ Object
Execute an event
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/statum/machine.rb', line 52 def fire!(instance, name) raise Statum::UnknownEventError, "Event #{name} not found" unless event?(name) current_state = current(instance) event = events[name.to_sym] unless event.can_fire?(current_state) raise Statum::ErrorTransitionError, "Cannot transition from #{current_state} to #{event.to}" end event.before.evaluate(instance) instance.send("#{field}=", event.to) event.after.evaluate(instance) end |
#state?(name) ⇒ Boolean
Checks if state present
32 33 34 |
# File 'lib/statum/machine.rb', line 32 def state?(name) @states.include?(name.to_sym) end |