source: extensions/gsdl-video/trunk/installed/cmdline/lib/ruby/1.8/shell/filter.rb@ 18425

Last change on this file since 18425 was 18425, checked in by davidb, 15 years ago

Video extension to Greenstone

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