Class: Shell::Filter

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/shell/filter.rb

Overview

Any result of command exection is a Filter.

This class includes Enumerable, therefore a Filter object can use all Enumerable facilities.

Direct Known Subclasses

BuiltInCommand, SystemCommand

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sh) ⇒ Filter

Returns a new instance of Filter.



22
23
24
25
# File 'lib/shell/filter.rb', line 22

def initialize(sh)
  @shell = sh         # parent shell
  @input = nil        # input filter
end

Instance Attribute Details

#inputObject

Returns the value of attribute input



27
28
29
# File 'lib/shell/filter.rb', line 27

def input
  @input
end

Instance Method Details

#+(filter) ⇒ Object

call-seq:

filter1 + filter2

Outputs filter1, and then filter2 using Join.new



113
114
115
# File 'lib/shell/filter.rb', line 113

def + (filter)
  Join.new(@shell, self, filter)
end

#<(src) ⇒ Object

call-seq:

< source

Inputs from source, which is either a string of a file name or an IO object.



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/shell/filter.rb', line 49

def < (src)
  case src
  when String
    cat = Cat.new(@shell, src)
    cat | self
  when IO
    self.input = src
    self
  else
    Shell.Fail Error::CantApplyMethod, "<", to.class
  end
end

#>(to) ⇒ Object

call-seq:

> source

Outputs from source, which is either a string of a file name or an IO object.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/shell/filter.rb', line 67

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
    Shell.Fail Error::CantApplyMethod, ">", to.class
  end
  self
end

#>>(to) ⇒ Object

call-seq:

>> source

Appends the output to source, which is either a string of a file name or an IO object.



89
90
91
92
93
94
95
# File 'lib/shell/filter.rb', line 89

def >> (to)
  begin
    Shell.cd(@shell.pwd).append(to, self)
  rescue CantApplyMethod
    Shell.Fail Error::CantApplyMethod, ">>", to.class
  end
end

#each(rs = nil) ⇒ Object

call-seq:

each(record_separator=nil) { block }

Iterates a block for each line.



37
38
39
40
41
42
# File 'lib/shell/filter.rb', line 37

def each(rs = nil)
  rs = @shell.record_separator unless rs
  if @input
    @input.each(rs){|l| yield l}
  end
end

#inspectObject



129
130
131
132
133
134
135
# File 'lib/shell/filter.rb', line 129

def inspect
  if @shell.debug.kind_of?(Integer) && @shell.debug > 2
    super
  else
    to_s
  end
end

#to_aObject



117
118
119
120
121
# File 'lib/shell/filter.rb', line 117

def to_a
  ary = []
  each(){|l| ary.push l}
  ary
end

#to_sObject



123
124
125
126
127
# File 'lib/shell/filter.rb', line 123

def to_s
  str = ""
  each(){|l| str.concat l}
  str
end

#|(filter) ⇒ Object

call-seq:

| filter

Processes a pipeline.



101
102
103
104
105
106
107
# File 'lib/shell/filter.rb', line 101

def | (filter)
  filter.input = self
  if active?
    @shell.process_controller.start_job filter
  end
  filter
end