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(path, 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(path, line, options={})
  n_context_line = options[:n_context_line] || 3
  lines = source(path)
  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(path) ⇒ Object



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

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