class Rubygrep::FileReader

Attributes

file_names[RW]
options[RW]
skip_current_file[RW]

Public Class Methods

new(file_names, options = {}) click to toggle source
# File lib/rubygrep/file_reader.rb, line 6
def initialize(file_names, options = {})
  @options = options
  @file_names = FileSearcher.new(options).search(file_names)
end

Public Instance Methods

each_line() { |{str: str, path: file_name, str_num: num+=1}| ... } click to toggle source
# File lib/rubygrep/file_reader.rb, line 15
def each_line
  file_names.each do |file_name|
    next_file = open_file(file_name)
    num = 0
    if next_file
      next_file.each_line do |str|
        str = str.encode("UTF-16be", :invalid=>:replace, :replace=>"").encode('UTF-8') unless str.valid_encoding?
        yield({str: str, path: file_name, str_num: num+=1})
        if skip_current_file
          @skip_current_file = false
          break
        end
      end
    end
  end
end
has_several_files?() click to toggle source
# File lib/rubygrep/file_reader.rb, line 11
def has_several_files?
  file_names.length > 1
end
next_file!() click to toggle source
# File lib/rubygrep/file_reader.rb, line 32
def next_file!
  @skip_current_file = true
end

Private Instance Methods

open_file(file_name) click to toggle source
# File lib/rubygrep/file_reader.rb, line 38
def open_file(file_name)
  File.open(file_name)
rescue Errno::ENOENT
  puts "No such file or directory #{file_name}"
  false
rescue Errno::EACCES
  puts "Permission denied: #{file_name}"
  false
end