Class: TestParser::SourceCode

Inherits:
Snippet
  • Object
show all
Defined in:
lib/test_parser/source_code.rb

Instance Attribute Summary

Attributes inherited from Snippet

#sexp

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Snippet

#get_block, #initialize, #to_code

Constructor Details

This class inherits a constructor from TestParser::Snippet

Class Method Details

.clear_cacheObject



21
22
23
# File 'lib/test_parser/source_code.rb', line 21

def self.clear_cache
  @source_codes = nil
end

.for(filename) ⇒ Object



25
26
27
# File 'lib/test_parser/source_code.rb', line 25

def self.for(filename)
  source_codes[File.expand_path(filename)]
end

.source_codesObject



12
13
14
15
16
17
18
19
# File 'lib/test_parser/source_code.rb', line 12

def self.source_codes
  @source_codes ||= begin
    parser = RubyParser.new
    Hash.new do |h, k|
      h[k] = new(parser.parse File.read(k))
    end
  end
end

Instance Method Details

#extract_code_from_line(line_number) ⇒ Object

Raises:



39
40
41
42
43
44
45
46
47
# File 'lib/test_parser/source_code.rb', line 39

def extract_code_from_line(line_number)
  
  code_sexp = find_method_call_with_block(line_number)
  code_sexp ||= find_method_call_without_block(line_number)

  raise NoCodeInLineError unless code_sexp

  Snippet.new(code_sexp)
end

#extract_method(name) ⇒ Object

Raises:



29
30
31
32
33
34
35
36
37
# File 'lib/test_parser/source_code.rb', line 29

def extract_method(name)
  name        = name.to_sym
  definitions = sexp.enum_for(:each_of_type, :defn)
  method_sexp = definitions.find {|def_sexp| def_sexp[1] == name }

  raise NoMethodFoundError unless method_sexp

  Snippet.new(method_sexp)
end