Module: Juggler::StateMachine

Included in:
JobRunner
Defined in:
lib/juggler/state_machine.rb

Overview

Special eventmachine state machine

Callbacks are defined for before :exit, :pre enter and after :enter Asynchrous callbacks are not properly supported yet - only :pre must return a deferrable which will continue the callback chain on complete

state :foobar, :enter => ‘get_http’

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



10
11
12
# File 'lib/juggler/state_machine.rb', line 10

def self.included(klass)
  klass.extend(ClassMethods)
end

Instance Method Details

#bind(state, &callback) ⇒ Object



42
43
44
45
# File 'lib/juggler/state_machine.rb', line 42

def bind(state, &callback)
  @on_state ||= Hash.new { |h, k| h[k] = [] }
  @on_state[state] << callback
end

#change_state(new_state) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/juggler/state_machine.rb', line 18

def change_state(new_state)
  old_state = @_state
  
  Juggler.logger.debug "#{to_s}: Changing state: #{old_state} to #{new_state}"
  
  return nil if old_state == new_state
  
  raise "#{to_s}: Invalid state #{new_state}" unless self.class.states[new_state]

  if method = self.class.states[new_state][:pre]
    deferable = self.send(method)
    deferable.callback {
      run_synchronous_callbacks(old_state, new_state)
    }
    deferable.errback {
      Juggler.logger.warn "#{to_s}: State change aborted - pre failed"
    }
  else
    run_synchronous_callbacks(old_state, new_state)
  end

  return true
end

#stateObject



14
15
16
# File 'lib/juggler/state_machine.rb', line 14

def state
  @_state
end