Sha256: 75aeb0d622dd5fa6b2b06b060d001f42a56f4c41cf77d2f72bf8750621a6ff1c
Contents?: true
Size: 873 Bytes
Versions: 1
Compression:
Stored size: 873 Bytes
Contents
class MicroMachine InvalidEvent = Class.new(NoMethodError) attr :transitions_for attr :state def initialize initial_state @state = initial_state @transitions_for = Hash.new @callbacks = Hash.new { |hash, key| hash[key] = [] } end def on key, &block @callbacks[key] << block end def when(event, transitions) transitions_for[event] = transitions end def trigger event if trigger?(event) @state = transitions_for[event][@state] callbacks = @callbacks[@state] + @callbacks[:any] callbacks.each { |callback| callback.call } true else false end end def trigger?(event) raise InvalidEvent unless transitions_for.has_key?(event) transitions_for[event][state] ? true : false end def events transitions_for.keys end def ==(some_state) state == some_state end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
micromachine-1.0.4 | lib/micromachine.rb |