Class: Rubygrep::FileReader

Inherits:
Object
  • Object
show all
Defined in:
lib/rubygrep/file_reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_names, options = {}) ⇒ FileReader

Returns a new instance of FileReader.



6
7
8
9
# File 'lib/rubygrep/file_reader.rb', line 6

def initialize(file_names, options = {})
  @options = options
  @file_names = FileSearcher.new(options).search(file_names)
end

Instance Attribute Details

#file_namesObject

Returns the value of attribute file_names.



4
5
6
# File 'lib/rubygrep/file_reader.rb', line 4

def file_names
  @file_names
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/rubygrep/file_reader.rb', line 4

def options
  @options
end

#skip_current_fileObject

Returns the value of attribute skip_current_file.



4
5
6
# File 'lib/rubygrep/file_reader.rb', line 4

def skip_current_file
  @skip_current_file
end

Instance Method Details

#each_lineObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/rubygrep/file_reader.rb', line 11

def has_several_files?
  file_names.length > 1
end

#next_file!Object



32
33
34
# File 'lib/rubygrep/file_reader.rb', line 32

def next_file!
  @skip_current_file = true
end