Class: Test::Unit::CodeSnippetFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/test/unit/code-snippet-fetcher.rb

Instance Method Summary collapse

Constructor Details

#initializeCodeSnippetFetcher

Returns a new instance of CodeSnippetFetcher.



4
5
6
# File 'lib/test/unit/code-snippet-fetcher.rb', line 4

def initialize
  @sources = {}
end

Instance Method Details

#fetch(file, line, options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/test/unit/code-snippet-fetcher.rb', line 8

def fetch(file, line, options={})
  n_context_line = options[:n_context_line] || 3
  lines = source(file)
  return [] if lines.nil?
  min_line = [line - n_context_line, 1].max
  max_line = [line + n_context_line, lines.length].min
  window = min_line..max_line
  window.collect do |n|
    attributes = {:target_line? => (n == line)}
    [n, lines[n - 1].chomp, attributes]
  end
end

#source(file) ⇒ Object



21
22
23
# File 'lib/test/unit/code-snippet-fetcher.rb', line 21

def source(file)
  @sources[file] ||= read_source(file)
end