* io.c (rb_open_file): encoding in mode string was ignored if perm is
[ruby-svn.git] / lib / shell / filter.rb
blob3bb683db22aae4d478944d0d1b49710191e431cc
2 #   shell/filter.rb - 
3 #       $Release Version: 0.7 $
4 #       $Revision$
5 #       by Keiju ISHITSUKA(keiju@ruby-lang.org)
7 # --
9 #   
12 class Shell
13   #
14   # Filter
15   # A method to require
16   #    each()
17   #
18   class Filter
19     include Enumerable
21     def initialize(sh)
22       @shell = sh         # parent shell
23       @input = nil        # input filter
24     end
26     attr_reader :input
28     def input=(filter)
29       @input = filter
30     end
31     
32     def each(rs = nil)
33       rs = @shell.record_separator unless rs
34       if @input
35         @input.each(rs){|l| yield l}
36       end
37     end
39     def < (src)
40       case src
41       when String
42         cat = Cat.new(@shell, src)
43         cat | self
44       when IO
45         self.input = src
46         self
47       else
48         Shell.Fail Error::CantApplyMethod, "<", to.class
49       end
50     end
52     def > (to)
53       case to
54       when String
55         dst = @shell.open(to, "w")
56         begin
57           each(){|l| dst << l}
58         ensure
59           dst.close
60         end
61       when IO
62         each(){|l| to << l}
63       else
64         Shell.Fail Error::CantApplyMethod, ">", to.class
65       end
66       self
67     end
69     def >> (to)
70       begin
71         Shell.cd(@shell.pwd).append(to, self)
72       rescue CantApplyMethod
73         Shell.Fail Error::CantApplyMethod, ">>", to.class
74       end
75     end
77     def | (filter)
78       filter.input = self
79       if active?
80         @shell.process_controller.start_job filter
81       end
82       filter
83     end
85     def + (filter)
86       Join.new(@shell, self, filter)
87     end
89     def to_a
90       ary = []
91       each(){|l| ary.push l}
92       ary
93     end
95     def to_s
96       str = ""
97       each(){|l| str.concat l}
98       str
99     end
101     def inspect
102       if @shell.debug.kind_of?(Integer) && @shell.debug > 2
103         super
104       else
105         to_s
106       end
107     end
108   end