Class: Attest::ProcSourceReader

Inherits:
Object
  • Object
show all
Defined in:
lib/attest/proc/proc_source_reader.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, line) ⇒ ProcSourceReader

Returns a new instance of ProcSourceReader.



6
7
8
9
# File 'lib/attest/proc/proc_source_reader.rb', line 6

def initialize(file, line)
  @file = file
  @start_line = line
end

Class Method Details

.find(file, line) ⇒ Object



11
12
13
14
15
# File 'lib/attest/proc/proc_source_reader.rb', line 11

def self.find(file, line)
  source_reader = ProcSourceReader.new(file, line)
  source_reader.read_source

end

Instance Method Details

#read_lines_from_fileObject



40
41
42
43
44
45
46
47
# File 'lib/attest/proc/proc_source_reader.rb', line 40

def read_lines_from_file
  raise "No file for proc where does it come from" unless @file 
  begin
    File.readlines(@file)[(@start_line - 1) .. -1]
  rescue
    nil
  end
end

#read_sourceObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/attest/proc/proc_source_reader.rb', line 17

def read_source
  lines_starting_with_proc = read_lines_from_file
  return nil if lines_starting_with_proc.nil?
  lexer = RubyLex.new
  lexer.set_input(StringIO.new(lines_starting_with_proc.join))
  start_token, end_token = nil, nil
  nesting = 0
  while token = lexer.token
    if RubyToken::TkDO === token || RubyToken::TkfLBRACE === token
      nesting += 1
      start_token = token if nesting == 1
    elsif RubyToken::TkEND === token || RubyToken::TkRBRACE === token
      if nesting == 1
        end_token = token 
        break
      end
      nesting -= 1
    end
  end
  proc_lines = lines_starting_with_proc[start_token.line_no - 1 .. end_token.line_no - 1]
  proc_lines
end