class Asynchro::State
Public Class Methods
new(&block)
click to toggle source
Create a state map object. If a block is given, it is called with the instance created, then that instance is run.
# File lib/asynchro/state.rb, line 4 def initialize(&block) @states = { :finish => [ ] } if (block_given?) case (block.arity) when 0 instance_exec(&block) else block.call(self) end self.run! end end
Public Instance Methods
declare(state, &block)
click to toggle source
Declares the action to be taken when a particular state is entered. The argument is the state name and should be a Symbol. A block must be provided. If this state is already declared, the given block will be executed after the previous declarations.
# File lib/asynchro/state.rb, line 25 def declare(state, &block) (@states[state.to_sym] ||= [ ]) << block end
declared?(state)
click to toggle source
Returns true if a particular state has been declared, false otherwise.
# File lib/asynchro/state.rb, line 30 def declared?(state) !!@states[state] end
run!(state = :start)
click to toggle source
Runs a particular state, or if the state is not specified, then :start by default.
# File lib/asynchro/state.rb, line 36 def run!(state = :start) procs = @states[state.to_sym] case (state) when :start procs = [ lambda { finish! } ] unless (procs) end if (procs) procs.each(&:call) else STDERR.puts "WARNING: No state #{state} defined." end end
Protected Instance Methods
method_missing(name, *args, &block)
click to toggle source
This lets the object instance function as a simple DSL by allowing arbitrary method names to map to various functions.
Calls superclass method
# File lib/asynchro/state.rb, line 54 def method_missing(name, *args, &block) name_s = name.to_s if (args.empty?) case (name) when /\?$/ self.declared?(name_s.sub(/\?$/, '')) when /\!$/ self.run!(name_s.sub(/\!$/, '')) else self.declare(name, &block) end else super(name, *args) end end