summaryrefslogtreecommitdiff
path: root/lib/shell/filter.rb
diff options
context:
space:
mode:
authorknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2001-05-17 10:02:47 +0000
committerknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2001-05-17 10:02:47 +0000
commitd9350f56002fde6b5640b93ae7b8885cb7811d1a (patch)
tree911c962685a2c4caf03fc058a9a0afd3a3a0bdeb /lib/shell/filter.rb
parentc2fa753f7db4ea07c50ec8a182f78b1a263e0709 (diff)
Initial revision
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1418 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/shell/filter.rb')
-rw-r--r--lib/shell/filter.rb111
1 files changed, 111 insertions, 0 deletions
diff --git a/lib/shell/filter.rb b/lib/shell/filter.rb
new file mode 100644
index 0000000000..441cded221
--- /dev/null
+++ b/lib/shell/filter.rb
@@ -0,0 +1,111 @@
+#
+# shell/filter.rb -
+# $Release Version: 0.6.0 $
+# $Revision$
+# $Date$
+# by Keiju ISHITSUKA(Nihon Rational Software Co.,Ltd)
+#
+# --
+#
+#
+#
+
+class Shell
+ #
+ # Filter
+ # A method to require
+ # each()
+ #
+ class Filter
+ include Enumerable
+ include Error
+
+ def initialize(sh)
+ @shell = sh # parent shell
+ @input = nil # input filter
+ end
+
+ attr_reader :input
+
+ def input=(filter)
+ @input = filter
+ end
+
+ def each(rs = nil)
+ rs = @shell.record_separator unless rs
+ if @input
+ @input.each(rs){|l| yield l}
+ end
+ end
+
+ def < (src)
+ case src
+ when String
+ cat = Cat.new(@shell, src)
+ cat | self
+ when IO
+ self.input = src
+ self
+ else
+ Filter.Fail CanNotMethodApply, "<", to.type
+ end
+ end
+
+ def > (to)
+ case to
+ when String
+ dst = @shell.open(to, "w")
+ begin
+ each(){|l| dst << l}
+ ensure
+ dst.close
+ end
+ when IO
+ each(){|l| to << l}
+ else
+ Filter.Fail CanNotMethodApply, ">", to.type
+ end
+ self
+ end
+
+ def >> (to)
+ begin
+ Shell.cd(@shell.pwd).append(to, self)
+ rescue CanNotMethodApply
+ Shell.Fail CanNotMethodApply, ">>", to.type
+ end
+ end
+
+ def | (filter)
+ filter.input = self
+ if active?
+ @shell.process_controller.start_job filter
+ end
+ filter
+ end
+
+ def + (filter)
+ Join.new(@shell, self, filter)
+ end
+
+ def to_a
+ ary = []
+ each(){|l| ary.push l}
+ ary
+ end
+
+ def to_s
+ str = ""
+ each(){|l| str.concat l}
+ str
+ end
+
+ def inspect
+ if @shell.debug.kind_of?(Integer) && @shell.debug > 2
+ super
+ else
+ to_s
+ end
+ end
+ end
+end