Class: XCPretty::Snippet
- Inherits:
-
Object
- Object
- XCPretty::Snippet
- Defined in:
- lib/xcpretty/snippet.rb
Instance Attribute Summary collapse
-
#contents ⇒ Object
readonly
Returns the value of attribute contents.
-
#file_path ⇒ Object
readonly
Returns the value of attribute file_path.
Class Method Summary collapse
- .from_filepath(filepath) ⇒ Object
- .read_snippet(file, around_line) ⇒ Object
- .readline(file) ⇒ Object
Instance Method Summary collapse
-
#initialize(contents = '', file_path = '') ⇒ Snippet
constructor
A new instance of Snippet.
Constructor Details
#initialize(contents = '', file_path = '') ⇒ Snippet
Returns a new instance of Snippet.
5 6 7 8 |
# File 'lib/xcpretty/snippet.rb', line 5 def initialize(contents = '', file_path = '') @contents = contents @file_path = file_path end |
Instance Attribute Details
#contents ⇒ Object (readonly)
Returns the value of attribute contents.
3 4 5 |
# File 'lib/xcpretty/snippet.rb', line 3 def contents @contents end |
#file_path ⇒ Object (readonly)
Returns the value of attribute file_path.
3 4 5 |
# File 'lib/xcpretty/snippet.rb', line 3 def file_path @file_path end |
Class Method Details
.from_filepath(filepath) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/xcpretty/snippet.rb', line 10 def self.from_filepath(filepath) path, line = filepath.split(':') file = File.open(path) text = read_snippet(file, line) file.close new(text, filepath) rescue new('', filepath) end |
.read_snippet(file, around_line) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/xcpretty/snippet.rb', line 23 def self.read_snippet(file, around_line) text = '' starting_position = around_line.to_i - 2 starting_position.times { file.gets } 3.times { text += readline(file) } text end |
.readline(file) ⇒ Object
31 32 33 34 |
# File 'lib/xcpretty/snippet.rb', line 31 def self.readline(file) file.gets $_ || '' end |