Class: RubyGrep::File

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-grep/file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fn) ⇒ File

Returns a new instance of File.



9
10
11
12
13
# File 'lib/ruby-grep/file.rb', line 9

def initialize(fn)
  @lines = ::File.readlines(fn)
  @lines.map! { |line| RubyGrep::Line.new(line, @lines.index(line), self) }
  @file_name = fn
end

Instance Attribute Details

#file_nameObject (readonly)

Returns the value of attribute file_name.



7
8
9
# File 'lib/ruby-grep/file.rb', line 7

def file_name
  @file_name
end

Instance Method Details

#colorized_filenameObject



21
22
23
24
25
# File 'lib/ruby-grep/file.rb', line 21

def colorized_filename
  filename = @file_name.scan(/\/(\.?\w+\.\w+)|(\.\w+)/).join
  colorised_filename = filename.colorize(:yellow)
  @file_name.gsub(filename, colorised_filename)
end

#match(expression) ⇒ Object

this method selects lines from the file that match the expression, which may be a string or regexp.



17
18
19
# File 'lib/ruby-grep/file.rb', line 17

def match(expression)
  @lines.select { |line| line.contents.match(expression.value) }
end

#max_line_number_lengthObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/ruby-grep/file.rb', line 27

def max_line_number_length
  return @max_line_number_length if @max_line_number_length
  @max_line_number_length = 1
  @lines.each do |line|
    if line.number.to_s.length > @max_line_number_length
      @max_line_number_length = line.number.to_s.length
    end
  end
  @max_line_number_length
end