Class: Chirp::EachProcessor

Inherits:
Object
  • Object
show all
Includes:
FSSugar
Defined in:
lib/chirp/application.rb

Instance Method Summary collapse

Methods included from FSSugar

#all, #bigger, #contains, #dir?, #except, #file?, #length, #named, #none, #smaller

Constructor Details

#initialize(context, options = {}, &block) ⇒ EachProcessor

Returns a new instance of EachProcessor.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/chirp/application.rb', line 141

def initialize(context, options={}, &block)
  @context = context
  @filter = PathFilter.new(options)
  @field_separator = options[:field_separator]
  @block = block
  @output_spec = options[:output]
  @create_backups = options[:create_backups]
  @redirect_output = options.key?(:redirect_output) ? options[:redirect_output] : true
  @context = context
  @before_processors = []
  @content_processors = []
  @after_processors = []
  instance_eval(&block) if block
end

Instance Method Details

#after(options = {}, &block) ⇒ Object



160
161
162
# File 'lib/chirp/application.rb', line 160

def after(options={}, &block)
  @after_processors << PathProcessor.new(@context, options, &block) 
end

#before(options = {}, &block) ⇒ Object



156
157
158
# File 'lib/chirp/application.rb', line 156

def before(options={}, &block)
  @before_processors << PathProcessor.new(@context, options, &block) 
end

#dir(options = {}, &block) ⇒ Object



174
175
176
177
178
# File 'lib/chirp/application.rb', line 174

def dir(options={}, &block)
  new_options = options.clone
  new_options[:type] = :dir
  before(new_options, &block)
end

#executeObject



184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/chirp/application.rb', line 184

def execute
    @context.paths = filtered_files(@context.dir)
    @context.paths.each do |path|
      @context.debug( "Trying out processor #{self} for #{path} and #{@filter}" )
      @context.path = path
      @context.debug( "Executing processor #{self} for #{path}" )
      @context.output_path = @context.path.pathmap(@output_spec) if @output_spec
      if @output_spec and @redirect_output
        Chirp::run_with_output_file(@context.output_path, @create_backups) {process}
      else
        process
      end
    end
end

#file(options = {}, &block) ⇒ Object



168
169
170
171
172
# File 'lib/chirp/application.rb', line 168

def file(options={}, &block)
  new_options = options.clone
  new_options[:type] = :file
  before(new_options, &block)
end

#filtered_files(dir) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/chirp/application.rb', line 223

def filtered_files(dir)
  results = Set.new
  FileUtils.chdir(dir) do
    Find.find('.') do |p|
      path = p.sub(/^\.\//, '')
      next if path == '.'
      next if path == '..'
      next unless @filter.passes?(path)
      results << path
    end
  end
  results.sort
end

#line(options = {}, &block) ⇒ Object



180
181
182
# File 'lib/chirp/application.rb', line 180

def line(options={}, &block)
  @content_processors << LineProcessor.new(@context, options, &block)
end

#path(options = {}, &block) ⇒ Object



164
165
166
# File 'lib/chirp/application.rb', line 164

def path(options={}, &block)
  before(options, &block)
end

#processObject



199
200
201
202
203
# File 'lib/chirp/application.rb', line 199

def process
  @before_processors.each {|p| p.execute}
  process_contents
  @after_processors.each {|p| p.execute}
end

#process_contentsObject



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/chirp/application.rb', line 205

def process_contents
  return if @content_processors.empty?
  return unless File.file?(@context.full_path)

  @content_processors.each {|p| p.reset}
  @context.field_separator = @field_separator

  Chirp::run_with_input_file(@context.full_path) do
    @context.line_no = 1
    while not $stdin.eof?
      @context.line = $stdin.readline.chomp
      @content_processors.each {|p| p.execute}
      @context.line_no += 1
    end
    @context.contents = nil
  end
end