Class: Finity::Event
- Inherits:
-
Object
- Object
- Finity::Event
- Defined in:
- lib/finity/event.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
-
#handle(object, state) ⇒ Object
Handle the current state and execute the first allowed transition.
-
#initialize(name, options = {}, &block) ⇒ Event
constructor
Initialize a new event and execute the block which holds the transition definitions.
-
#transitions(options = {}) ⇒ Object
Add a transition to the event.
Constructor Details
#initialize(name, options = {}, &block) ⇒ Event
Initialize a new event and execute the block which holds the transition definitions.
29 30 31 32 |
# File 'lib/finity/event.rb', line 29 def initialize name, = {}, &block @name, @transitions = name, {} instance_eval &block if block_given? end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
25 26 27 |
# File 'lib/finity/event.rb', line 25 def name @name end |
Instance Method Details
#handle(object, state) ⇒ Object
Handle the current state and execute the first allowed transition.
44 45 46 47 48 49 50 51 |
# File 'lib/finity/event.rb', line 44 def handle object, state raise InvalidState, "No match for (:#{state.name}) on (:#{name})" unless @transitions.key? state.name @transitions[state.name].find do |transition| name = transition.handle object return name unless name.nil? end end |
#transitions(options = {}) ⇒ Object
Add a transition to the event.
35 36 37 38 39 40 41 |
# File 'lib/finity/event.rb', line 35 def transitions = {} transition = Transition.new [[:from]].flatten.each do |from| @transitions[from] ||= [] @transitions[from] << transition end end |