lib/rio/state.rb in rio-0.4.2 vs lib/rio/state.rb in rio-0.5.1

- old
+ new

@@ -1,8 +1,8 @@ #-- -# =============================================================================== -# Copyright (c) 2005,2006,2007,2008 Christopher Kleckner +# =========================================================================== +# Copyright (c) 2005-2012 Christopher Kleckner # All rights reserved # # This file is part of the Rio library for ruby. # # Rio is free software; you can redistribute it and/or modify @@ -16,33 +16,29 @@ # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Rio; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -# =============================================================================== +# =========================================================================== #++ # -# To create the documentation for Rio run the command -# ruby build_doc.rb -# from the distribution directory. -# -# Suggested Reading -# * RIO::Doc::SYNOPSIS -# * RIO::Doc::INTRO -# * RIO::Doc::HOWTO -# * RIO::Doc::EXAMPLES -# * RIO::Rio -# +#require 'rio/context' +#require 'rio/context/methods' +#require 'rio/ext' +#require 'rio/filter' +#require 'rio/fs/native' +#require 'rio/fwd' + require 'rio/exception/state' -require 'rio/context' -require 'rio/context/methods' -require 'rio/ext' +require 'rio/state/data' require 'rio/symantics' -require 'rio/filter' -require 'rio/fs/native' +module RIO + autoload :Cx,'rio/context' + autoload :Ext,'rio/ext' +end module RIO module State #:nodoc: all @@ -52,64 +48,87 @@ # * some basic house keeping methods # * the methods to communicate with the rio object # * the state changing mechanism # * and some basic error handling stubs class Base - KIOSYMS = [:gets,:getc,:open,:readline,:readlines,:chop,:to_a,:putc,:puts,:print,:printf, - #:split, - :=~,:===,:==,:eql?,:sub,:sub!,:gsub,:gsub!,:load] + kiosyms = [] + kiosyms << :gets + kiosyms << :open + kiosyms << :readline + kiosyms << :readlines + kiosyms << :putc + kiosyms << :puts + kiosyms << :print + kiosyms << :printf + kiosyms << :=~ + kiosyms << :=== + kiosyms << :== + kiosyms << :eql? + kiosyms << :load + kiosyms << :to_a + #kiosyms << :split + #kiosyms << :sub + #kiosyms << :sub! + #kiosyms << :gsub + #kiosyms << :gsub! + #kiosyms << :chop + #kiosyms << :getc + + # In 1.8 #to_a is inherited from Object + # For 1.9 we create this -- only to undef it immediatly. + # So we end up in the same state for both ruby versions. + def to_a() end + + KIOSYMS = kiosyms @@kernel_cleaned ||= KIOSYMS.each { |sym| undef_method(sym) } undef_method(:rio) end class Base attr_accessor :try_state #attr_accessor :handled_by - attr_accessor :rl - attr_accessor :ioh - - attr_accessor :cx - + # include Enumerable # Context handling include Cx::Methods include RIO::Ext::Cx - def initialize(rl=nil,cx=nil,ioh=nil) - cx ||= self.class.default_cx - _init(rl,cx,ioh) - # @handled_by = self.class.to_s + attr_reader :data + def initialize(iv) + #p "State#initialize(#{iv.keys.inspect})" + @data = State::Data.new + iv.keys.each do |k| + @data[k] = iv[k] + end + @data.cx ||= self.class.default_cx end - def _init(riorl,cntx,iohandle=nil) - @rl = riorl - @cx = cntx - @ioh = iohandle -# raise Exception::FailedCheck.new(self) unless check? - self - end - private :_init - - def initialize_copy(*args) - #p callstr('initialize_copy',args[0].inspect) + def initialize_copy(other) super - @rl = @rl.clone unless @rl.nil? - @cx = @cx.clone unless @cx.nil? - @ioh = @ioh.clone unless @ioh.nil? - # @fs = @fs + @data = State::Data.new + data.rl = other.data.rl.clone if other.data.rl + data.cx = other.data.cx.clone if other.data.cx + data.ioh = other.data.ioh.clone if other.data.ioh end + extend Forwardable + def_instance_delegators(:rl,:path,:netpath,:to_s,:fspath,:length) + + extend RIO::Fwd + fwd :data,:rl,:cx + fwd :data,:ioh + alias :ior :ioh + alias :iow :ioh + def self.default_cx Cx::Vars.new( { 'closeoneof' => true, 'closeoncopy' => true } ) end def self.new_other(other) - new(other.rl,other.cx,other.ioh) + new(other.data) end - alias :ior :ioh - alias :iow :ioh # Section: State Switching @@ -120,39 +139,36 @@ def become(new_class,*args) p "become : #{self.class.to_s} => #{new_class.to_s} (#{self.mode?})" if $trace_states #p "BECOME #{new_class}: #{cx['ss_type']}" return self if new_class == self.class - begin - new_state = try_state[new_class,*args] - rescue Exception::FailedCheck => ex - p "not a valid "+new_class.to_s+": "+ex.to_s+" '"+self.to_s+"'" - raise - end + new_state = try_state[new_class,*args] became(new_state) new_state end def became(obj) #RIO::Ext.became(obj) end def method_missing_trace_str(sym,*args) - "missing: "+self.class.to_s+'['+self.to_url+" {#{self.rl.fs}}"+']'+'.'+sym.to_s+'('+args.join(',')+')' + # "missing: "+self.class.to_s+'['+self.to_url+" {#{self.rl.fs}}"+']'+'.'+sym.to_s+'('+args.join(',')+')' + "missing: "+self.class.to_s+'['+self.to_url+']'+'.'+sym.to_s+'('+args.join(',')+')' #"missing: "+self.class.to_s+'['+self.to_url+""+']'+'.'+sym.to_s+'('+args.join(',')+')' end def method_missing(sym,*args,&block) p method_missing_trace_str(sym,*args) if $trace_states obj = when_missing(sym,*args) raise RuntimeError,"when_missing returns nil" if obj.nil? + #p "STATE: METHOD_MISSING",obj.to_s,sym,args obj.__send__(sym,*args,&block) #unless obj == self end def when_missing(sym,*args) gofigure(sym,*args) end - def base_state() Factory.instance.reset_state(@rl) end + def base_state() Factory.instance.reset_state(rl) end def softreset #p "softreset(#{self.class}) => #{self.base_state}" cx['retrystate'] = nil become(self.base_state) @@ -178,22 +194,24 @@ end def to_rl() self.rl.rl end def fs() self.rl.fs end - extend Forwardable - def_instance_delegators(:rl,:path,:to_s,:fspath,:urlpath,:length) + #def_instance_delegators(:uri,:path,:netpath,:to_s,:fspath,:length) - def ==(other) @rl == other end + def ==(other) rl == other end def ===(other) self == other end def =~(other) other =~ self.to_str end - def to_url() @rl.url end - def to_uri() @rl.uri end - alias to_str to_s + def to_url() rl.url end + def to_uri() rl.uri end + def uri() rl.uri end + alias :to_str :to_s + alias :to_path :to_s + def to_ary() nil end - def hash() @rl.to_s.hash end - def eql?(other) @rl.to_s.eql?(other.to_s) end + def hash() rl.to_s.hash end + #def eql?(other) @rl.to_s.eql?(other.to_s) end def stream?() false end # Section: Rio Interface # gives states the ability to create new rio objects @@ -207,11 +225,11 @@ n end def clone_rio() cp = Rio.new(self.rl) cp.cx = self.cx.clone - cp.ioh = self.ioh.clone unless self.ioh.nil? + #cp.ioh = self.ioh.clone unless self.ioh.nil? cp.rl = self.rl.clone cp end def ensure_rio(arg0) @@ -221,10 +239,10 @@ else new_rio(arg0) end end def ensure_cmd_rio(arg) case arg - when ::String then new_rio(:cmdio,arg) + when ::String then new_rio("cmdio:"+arg) when ::Fixnum then new_rio(arg) when Rio then arg.clone else ensure_rio(arg) end end