Sha256: 60efaf54250c24664852bf3300d5c22728d9b5e06267ebdba31c6a00780517e7

Contents?: true

Size: 672 Bytes

Versions: 2

Compression:

Stored size: 672 Bytes

Contents

class FSM
	class State
		attr_reader :cb

		def initialize(cb)
			@cb = cb
		end
	end

	FSM_S_INIT = 0

	def initialize
		@state = FSM_S_INIT
		@fsm = Array.new
		@name2index = Hash.new
		@index2name = Array.new
	end

	def add(name, cb)
		if @name2index.key?(name)
			raise(ArgumentError, "duplicated key \"#{name}\"")
		end
		@fsm.push(State.new(cb))
		idx = @fsm.length - 1
		@name2index[name] = idx
		@index2name[idx]  = name
	end

	def cb
		return @fsm[@state].cb
	end

	def changeto(name)
		if ! @name2index.key?(name)
			raise(ArgumentError, "unnown state: \"#{name}\"")
		end
		@state = @name2index[name]
	end

	def state_name
		return @index2name[@state]
	end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
netutils-0.1.2 lib/netutils/fsm.rb
netutils-0.1.1 lib/netutils/fsm.rb