lib/shell/filter.rb


DEFINITIONS

This source file includes following functions.


   1  #
   2  #   shell/filter.rb - 
   3  #       $Release Version: 0.6.0 $
   4  #       $Revision: 1.1 $
   5  #       $Date: 2001/05/17 10:02:47 $
   6  #       by Keiju ISHITSUKA(Nihon Rational Software Co.,Ltd)
   7  #
   8  # --
   9  #
  10  #   
  11  #
  12  
  13  class Shell
  14    #
  15    # Filter
  16    # A method to require
  17    #    each()
  18    #
  19    class Filter
  20      include Enumerable
  21      include Error
  22  
  23      def initialize(sh)
  24        @shell = sh         # parent shell
  25        @input = nil        # input filter
  26      end
  27  
  28      attr_reader :input
  29  
  30      def input=(filter)
  31        @input = filter
  32      end
  33      
  34      def each(rs = nil)
  35        rs = @shell.record_separator unless rs
  36        if @input
  37          @input.each(rs){|l| yield l}
  38        end
  39      end
  40  
  41      def < (src)
  42        case src
  43        when String
  44          cat = Cat.new(@shell, src)
  45          cat | self
  46        when IO
  47          self.input = src
  48          self
  49        else
  50          Filter.Fail CanNotMethodApply, "<", to.type
  51        end
  52      end
  53  
  54      def > (to)
  55        case to
  56        when String
  57          dst = @shell.open(to, "w")
  58          begin
  59            each(){|l| dst << l}
  60          ensure
  61            dst.close
  62          end
  63        when IO
  64          each(){|l| to << l}
  65        else
  66          Filter.Fail CanNotMethodApply, ">", to.type
  67        end
  68        self
  69      end
  70  
  71      def >> (to)
  72        begin
  73          Shell.cd(@shell.pwd).append(to, self)
  74        rescue CanNotMethodApply
  75          Shell.Fail CanNotMethodApply, ">>", to.type
  76        end
  77      end
  78  
  79      def | (filter)
  80        filter.input = self
  81        if active?
  82          @shell.process_controller.start_job filter
  83        end
  84        filter
  85      end
  86  
  87      def + (filter)
  88        Join.new(@shell, self, filter)
  89      end
  90  
  91      def to_a
  92        ary = []
  93        each(){|l| ary.push l}
  94        ary
  95      end
  96  
  97      def to_s
  98        str = ""
  99        each(){|l| str.concat l}
 100        str
 101      end
 102  
 103      def inspect
 104        if @shell.debug.kind_of?(Integer) && @shell.debug > 2
 105          super
 106        else
 107          to_s
 108        end
 109      end
 110    end
 111  end