Class: Deltacloud::StateMachine

Inherits:
Object
  • Object
show all
Defined in:
lib/deltacloud/models/state_machine.rb

Defined Under Namespace

Classes: State, Transition

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}, &block) ⇒ StateMachine

Returns a new instance of StateMachine.



22
23
24
25
26
27
# File 'lib/deltacloud/models/state_machine.rb', line 22

def initialize(opts={}, &block)
  @all_states = opts[:all_states]
  @all_actions = opts[:all_actions]
  @states  = []
  instance_eval &block if block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



57
58
59
60
# File 'lib/deltacloud/models/state_machine.rb', line 57

def method_missing(sym,*args)
  return state( sym ) if ( args.empty? )
  super( sym, *args )
end

Instance Attribute Details

#statesObject (readonly)

Returns the value of attribute states.



20
21
22
# File 'lib/deltacloud/models/state_machine.rb', line 20

def states
  @states
end

Instance Method Details

#finishObject



33
34
35
# File 'lib/deltacloud/models/state_machine.rb', line 33

def finish()
  state(:finish)
end

#startObject



29
30
31
# File 'lib/deltacloud/models/state_machine.rb', line 29

def start()
  state(:start)
end

#state(name) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/deltacloud/models/state_machine.rb', line 37

def state(name)
  unless valid_state_name?(name)
    raise "State '#{name}' not in list of allowed states"
  end
  state = @states.find{|e| e.name == name.to_sym}
  if ( state.nil? )
    state = State.new( self, name.to_sym )
    @states << state
  end
  state
end

#valid_action_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/deltacloud/models/state_machine.rb', line 53

def valid_action_name?(name)
  @all_actions.nil? || @all_actions.include?(name.to_sym)
end

#valid_state_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/deltacloud/models/state_machine.rb', line 49

def valid_state_name?(name)
  @all_states.nil? || @all_states.include?(name.to_sym)
end