-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This feature is experimental. I am not documenting it in the README because users shouldn't be using it yet.
- Loading branch information
Showing
15 changed files
with
468 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
module Airbrake | ||
## | ||
# Represents a small hunk of code consisting of a base line and a couple lines | ||
# around | ||
class CodeHunk | ||
## | ||
# @return [Integer] the maximum length of a line | ||
MAX_LINE_LEN = 200 | ||
|
||
## | ||
# @return [Integer] how many lines should be read around the base line | ||
INTERVAL = 3 | ||
|
||
def initialize(file, line, interval = INTERVAL) | ||
@file = file | ||
@line = line | ||
|
||
@start_line = [line - interval, 1].max | ||
@end_line = line + interval | ||
|
||
@code_hash = {} | ||
end | ||
|
||
## | ||
# @return [Hash{Integer=>String}, nil] code hunk around the base line. When | ||
# an error occurrs, returns a zero key Hash | ||
def to_h | ||
return @code_hash unless @code_hash.empty? | ||
return unless File.exist?(@file) | ||
|
||
begin | ||
fetch_code | ||
rescue StandardError => ex | ||
{ 0 => ex } | ||
end | ||
|
||
@code_hash | ||
end | ||
|
||
private | ||
|
||
def fetch_code | ||
File.foreach(@file).with_index(1) do |line, i| | ||
next if i < @start_line | ||
break if i > @end_line | ||
|
||
@code_hash[i] = line[0...MAX_LINE_LEN].rstrip | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.