Class: Finity::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/finity/event.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {}, &block
  @name, @transitions = name, {}
  instance_eval &block if block_given?
end

Instance Attribute Details

#nameObject

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.

Raises:



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 options = {}
  transition = Transition.new options
  [options[:from]].flatten.each do |from|
    @transitions[from] ||= []
    @transitions[from] << transition
  end
end