Class: FileLine
Instance Attribute Summary collapse
-
#line_number ⇒ Object
Returns the value of attribute line_number.
-
#path ⇒ Object
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(line_number, path) ⇒ FileLine
constructor
A new instance of FileLine.
- #raw_contents ⇒ Object
- #update_filesystem!(new_contents) ⇒ Object
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_number ⇒ Object
Returns the value of attribute line_number.
4 5 6 |
# File 'lib/file_line.rb', line 4 def line_number @line_number end |
#path ⇒ Object
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_contents ⇒ Object
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 |