Module: Cuukie::CodeSnippets

Included in:
Formatter
Defined in:
lib/cuukie/code_snippets.rb

Instance Method Summary collapse

Instance Method Details

#backtrace_to_snippet(backtrace) ⇒ Object



16
17
18
19
# File 'lib/cuukie/code_snippets.rb', line 16

def backtrace_to_snippet(backtrace)
  return null_snippet unless backtrace[0] =~ /(.*):(\d+)/
  code_snippet $1, $2.to_i
end

#code_snippet(file, line) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/cuukie/code_snippets.rb', line 3

def code_snippet(file, line)
  return null_snippet unless File.exist? file

  all_lines = File.open(file) {|f| f.readlines}
  return null_snippet unless line <= all_lines.size

  first_line = [1, line - 2].max
  
  { :raw_lines   => all_lines[(first_line - 1)..line].join,
    :first_line  => first_line,
    :marked_line => line }
end