Class: XCPretty::Snippet

Inherits:
Object
  • Object
show all
Defined in:
lib/xcpretty/snippet.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#contentsObject (readonly)

Returns the value of attribute contents.



3
4
5
# File 'lib/xcpretty/snippet.rb', line 3

def contents
  @contents
end

#file_pathObject (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