Class: StateMachine::Machine

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-state-machine/state_machine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Machine

Returns a new instance of Machine.



189
190
191
192
193
194
# File 'lib/ruby-state-machine/state_machine.rb', line 189

def initialize(opts)
  @states=opts[:states].collect{|state| state.to_sym}
  @events=opts[:events].collect{|event| event.to_sym}
  @default_state=opts[:default_state].to_sym if opts[:default_state]
  @transitions=[]
end

Instance Attribute Details

#default_stateObject

Returns the value of attribute default_state.



187
188
189
# File 'lib/ruby-state-machine/state_machine.rb', line 187

def default_state
  @default_state
end

#eventsObject

Returns the value of attribute events.



185
186
187
# File 'lib/ruby-state-machine/state_machine.rb', line 185

def events
  @events
end

#statesObject

Returns the value of attribute states.



184
185
186
# File 'lib/ruby-state-machine/state_machine.rb', line 184

def states
  @states
end

#transitionsObject

Returns the value of attribute transitions.



186
187
188
# File 'lib/ruby-state-machine/state_machine.rb', line 186

def transitions
  @transitions
end

Instance Method Details

#add_transition(opts) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/ruby-state-machine/state_machine.rb', line 196

def add_transition(opts)
  new_opts=strings_to_sym(opts)
  
  # Some validation for arrays of actions:
  if (new_opts[:next].is_a?Array)
    if new_opts[:next].length==1
      new_opts[:next] = new_opts[:next].first
    elsif new_opts[:next].length>1
      raise ArgumentError, "A decider must be present for multiple actions." if String(new_opts[:decider]).empty?
    end
  end
  
  @transitions << new_opts
end

#next_state(state, event) ⇒ Object

Next state (symbol only) for given state and event. (equivalent to next_state_instruction(state, event))



233
234
235
236
237
# File 'lib/ruby-state-machine/state_machine.rb', line 233

def next_state(state, event)
  transition = next_state_instruction(state, event)
  ns = transition ? transition[:next] : nil
  (ns.is_a?Symbol) ? ns : ns && ns[:state]
end

#next_state_instruction(state, event) ⇒ Object

Next state instruction (as hash) for given state and event:



240
241
242
# File 'lib/ruby-state-machine/state_machine.rb', line 240

def next_state_instruction(state, event)
  @transitions.detect{|t| t[:state]==state and t[:event]==event}
end

#strings_to_sym(hash) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/ruby-state-machine/state_machine.rb', line 211

def strings_to_sym(hash)
  new_hash=hash.dup
  hash.each do |k, v| 
    unless (v.nil? or (v.respond_to?:empty? and v.empty? ))
      if (v.is_a?(Hash))
        new_hash[k] = strings_to_sym(v)
      elsif (v.is_a?(Array))
        new_array = v.dup
        v.each_with_index do |array_hash, index|
          new_array[index] = strings_to_sym(array_hash) if array_hash.is_a?(Hash)
        end
        new_hash[k]=new_array
      elsif (v.is_a?(String))
        new_hash[k] = v.to_sym
      end
    end
  end
  new_hash
end