unless defined? $__fsm_util__ $__fsm_util__ = __FILE__ module FSM FSM::LIBDIR = File::dirname(File::expand_path(__FILE__)) + File::SEPARATOR unless defined? FSM::LIBDIR FSM::INCDIR = File::dirname(FSM::LIBDIR) + File::SEPARATOR unless defined? FSM::INCDIR require INCDIR + 'fsm' module Util class Const def self.new s s = s.to_s (@instances ||= {})[s] ||= super(s) end attr 'to_s' alias_method 'to_str', 'to_s' alias_method 'inspect', 'to_s' def initialize s @to_s = s.to_s freeze end end module Methods include Sync_m def const(*a) Const.new(*a) end def string_list *list list.flatten.map{|elem| elem.to_s} end def string *list string_list(*list).first end def klass self.class end def bcall b, *a arity = b.arity if arity > 0 a = a[0...arity] elsif arity < 0 arity = arity.abs a = a[0...arity] + (a[arity..-1] || []) else a.clear end b.call *a end def mcall obj, msg, *a m = obj.method(msg).to_proc bcall m, *a end def icall b, *a arity = b.arity if arity > 0 a = a[0...arity] elsif arity < 0 arity = arity.abs a = a[0...arity] + (a[arity..-1] || []) else a.clear end instance_exec *a, &b end def system *a, &b verbose = $VERBOSE $VERBOSE = nil ::Kernel.system *a, &b ensure $VERBOSE = verbose end unless instance_methods.include? 'instance_exec' def instance_exec *a, &b m, n = nil, -1 loop{ m = "__instance_exec_#{ Thread.current.object_id.abs }_#{ n += 1 }__" break unless respond_to? m } singleton_class{ define_method m, &b } send m, *a ensure singleton_class{ undef_method m } end end unless instance_methods.include? 'singleton_class' def singleton_class &b sc = class << self; self; end sc.module_eval &b if b sc end end def sh(&b) synchronize(:SH, &b) end def ex(&b) synchronize(:EX, &b) end def initialize(*a, &b) sync_initialize end alias_method 'sync_init', 'sync_initialize' end module ClassMethods include Methods def tattrs *ms ms.flatten.each do |m| module_eval <<-code def #{ m }(*a, &b) if a.empty? sh{ @#{ m } } else self.#{ m }= a.shift end end alias_method '#{ m }?', '#{ m }' def #{ m }= val ex{ @#{ m } = val } end code end end alias_method 'tattr', 'tattrs' def delegate hash = {} methods, to = hash.to_a.first methods.each do |m| module_eval <<-code def #{ m } *a, &b #{ to }.#{ m } *a, &b end code end end end module InstanceMethods include Methods end def self.included other other.extend ClassMethods other.module_eval{ include InstanceMethods include Sync_m } super end end # module Util end # module FSM end