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(number, path) ⇒ FileLine

Returns a new instance of FileLine.



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

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

Instance Attribute Details

#numberObject

Returns the value of attribute number.



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

def number
  @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



23
24
25
# File 'lib/file_line.rb', line 23

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

#update_filesystem!(new_contents) ⇒ Object



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

def update_filesystem!(new_contents)
  `sed -e '#{number} s/.*/#{new_contents}/' -i '' #{path}`
end