Class: FileLine

Inherits:
Object show all
Defined in:
lib/file_line.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line_number, path) ⇒ FileLine

Returns a new instance of FileLine.



5
6
7
8
# File 'lib/file_line.rb', line 5

def initialize(line_number, path)
  @line_number = line_number
  @path = path
end

Instance Attribute Details

#line_numberObject

Returns the value of attribute line_number.



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

def line_number
  @line_number
end

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

Class Method Details

.find_all(pattern, directory_path) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/file_line.rb', line 10

def self.find_all(pattern, directory_path)
  grep_results = `grep -Irn "#{pattern}" #{directory_path} --exclude-dir="*.*.*"`
  grep_results.split("\n").map do |grep_hit|
    file_path, line_number, _ = grep_hit.split(":")
    line_number = line_number.to_i
    FileLine.new(line_number, file_path)
  end
end

Instance Method Details

#raw_contentsObject



26
27
28
# File 'lib/file_line.rb', line 26

def raw_contents
  `sed -n #{line_number}p #{path}`.chomp
end

#update_filesystem!(new_contents) ⇒ Object



19
20
21
22
23
24
# File 'lib/file_line.rb', line 19

def update_filesystem!(new_contents)
  contents = File.read(path)
  splitted = contents.split("\n")
  splitted[line_number - 1] = new_contents
  File.write(path, splitted.join("\n"))
end