Class: RIO::Factory

Inherits:
Object show all
Includes:
Singleton
Defined in:
lib/rio/factory.rb

Overview

:nodoc: all

Constant Summary collapse

STATE2FILE =
{
  'Path::Reset' => 'rio/path/reset',
  'Path::Empty' => 'rio/path',
  'Path::Str' => 'rio/path',
  'Path::NonExisting' => 'rio/path',

  'File::Existing' => 'rio/file',
  'File::NonExisting' => 'rio/file',

  'Dir::Existing' => 'rio/dir',
  'Dir::Open' => 'rio/dir',
  'Dir::Close' => 'rio/dir',
  'Dir::Stream' => 'rio/dir',
  'Dir::NonExisting' => 'rio/dir',

  'Stream::Close' => 'rio/stream/open',
  'Stream::Reset' => 'rio/stream',
  'Stream::Open' => 'rio/stream/open',
  'Stream::Input' => 'rio/stream',
  'Stream::Output' => 'rio/stream',
  'Stream::InOut' => 'rio/stream',

  'Stream::Duplex::Open' => 'rio/stream/duplex',

  'Path::Stream::Open' => 'rio/scheme/path',

  'StrIO::Stream::Open' => 'rio/scheme/strio',

  'Null::Stream::Open' => 'rio/scheme/null',

  'CmdPipe::Stream::Reset' => 'rio/scheme/cmdpipe',

  'HTTP::Stream::Input' => 'rio/scheme/http',
  'HTTP::Stream::Open' => 'rio/scheme/http',

  'Temp::Reset' => 'rio/scheme/temp',
  'Temp::Stream::Open' => 'rio/scheme/temp',

  'Ext::YAML::Doc::Existing' => 'rio/ext/yaml/doc',
  'Ext::YAML::Doc::Open' => 'rio/ext/yaml/doc',
  'Ext::YAML::Doc::Stream' => 'rio/ext/yaml/doc',
  'Ext::YAML::Doc::Close' => 'rio/ext/yaml/doc',


}

Instance Method Summary collapse

Constructor Details

#initializeFactory

Returns a new instance of Factory.



252
253
254
255
256
257
# File 'lib/rio/factory.rb', line 252

def initialize()
  @ss_module = {}
  @reset_class = {}
  @state_class = {}
  @ss_class = {}
end

Instance Method Details

#clone_state(state) ⇒ Object



415
416
417
# File 'lib/rio/factory.rb', line 415

def clone_state(state)
  create_handle(state.target.clone)
end

#create_handle(new_state) ⇒ Object



418
419
420
421
422
# File 'lib/rio/factory.rb', line 418

def create_handle(new_state)
  hndl = Handle.new(new_state)
  new_state.try_state = try_state_proc(new_state,hndl)
  hndl
end

#create_state(*args) ⇒ Object



411
412
413
414
# File 'lib/rio/factory.rb', line 411

def create_state(*args)
  riorl = RIO::RL::Builder.build(*args)
  create_handle(state2class(reset_state(riorl)).new(riorl))
end

#create_state1(*args) ⇒ Object

factory creates a state from args



407
408
409
410
# File 'lib/rio/factory.rb', line 407

def create_state1(*args)
  riorl = RIO::RL::Builder.build(*args)
  create_handle(state2class(reset_state(riorl)).new(riorl))
end

#reset_state(rl) ⇒ Object



367
368
369
370
# File 'lib/rio/factory.rb', line 367

def reset_state(rl)
  mod = subscheme_module(rl.scheme)
  mod.const_get(:RESET_STATE) unless mod.nil?
end

#riorl_class(sch) ⇒ Object



363
364
365
# File 'lib/rio/factory.rb', line 363

def riorl_class(sch)
  subscheme_module(sch).const_get(:RL) 
end

#state2class(state_name) ⇒ Object



372
373
374
375
376
377
378
379
380
# File 'lib/rio/factory.rb', line 372

def state2class(state_name)
  return @state_class[state_name] if @state_class.has_key?(state_name)
  if STATE2FILE.has_key?(state_name)
    require STATE2FILE[state_name]
    return @state_class[state_name] = RIO.module_eval(state_name)
  else
    raise ArgumentError,"Unknown State Name (#{state_name})" 
  end
end

#subscheme_module(sch) ⇒ Object



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/rio/factory.rb', line 259

def subscheme_module(sch)
  @ss_module[sch] ||= case sch
                      when 'file','path'
                        require 'rio/scheme/path'
                        Path
                      when 'zipfile'
                        require 'rio/ext/zipfile/rl'
                        ZipFile::RootDir
                      when 'stdio','stdin','stdout'
                        require 'rio/scheme/stdio'
                        StdIO
                      when 'stderr'
                        require 'rio/scheme/stderr'
                        StdErr
                      when 'null'
                        require 'rio/scheme/null'
                        Null
                      when 'tempfile'
                        require 'rio/scheme/temp'
                        Temp::File
                      when 'temp'
                        require 'rio/scheme/temp'
                        Temp
                      when 'tempdir'
                        require 'rio/scheme/temp'
                        Temp::Dir
                      when 'strio','stringio','string'
                        require 'rio/scheme/strio'
                        StrIO
                      when 'cmdpipe'
                        require 'rio/scheme/cmdpipe'
                        CmdPipe
                      when 'aryio'
                        require 'rio/scheme/aryio'
                        AryIO
                      when 'http','https'
                        require 'rio/scheme/http'
                        HTTP
                      when 'ftp'
                        require 'rio/scheme/ftp'
                        FTP
                      when 'tcp'
                        require 'rio/scheme/tcp'
                        TCP
                      when 'sysio'
                        require 'rio/scheme/sysio'
                        SysIO
                      when 'fd'
                        require 'rio/scheme/fd'
                        FD
                      when 'cmdio'
                        require 'rio/scheme/cmdio'
                        CmdIO
                      else
                        require 'rio/scheme/path'
                        Path
                      end
end

#try_state_proc(current_state, rio_handle) ⇒ Object



388
389
390
391
392
393
# File 'lib/rio/factory.rb', line 388

def try_state_proc(current_state,rio_handle)
  proc { |new_state_name|
#        new_state_class = state2class(new_state_name)
    _change_state(state2class(new_state_name),current_state,rio_handle)
  }
end

#try_state_proc1(current_state, rio_handle) ⇒ Object



381
382
383
384
385
386
387
# File 'lib/rio/factory.rb', line 381

def try_state_proc1(current_state,rio_handle)
  #p "try_state_proc cur=#{current_state}[#{current_state.class}] han=#{rio_handle}[#{rio_handle.class}]"
  proc { |new_state_name|
#        new_state_class = state2class(new_state_name)
    _change_state(state2class(new_state_name,rio_handle),current_state,rio_handle)
  }
end