Class: Bugwatch::MethodParser

Inherits:
SexpProcessor
  • Object
show all
Defined in:
lib/bugwatch/method_parser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line_range) ⇒ MethodParser

Returns a new instance of MethodParser.



40
41
42
43
44
45
46
# File 'lib/bugwatch/method_parser.rb', line 40

def initialize(line_range)
  super()
  @line_range = line_range
  @klasses = Hash.new {|h, k| h[k] = []}
  @current_class = []
  self.auto_shift_type = true
end

Instance Attribute Details

#klassesObject (readonly)

Returns the value of attribute klasses.



38
39
40
# File 'lib/bugwatch/method_parser.rb', line 38

def klasses
  @klasses
end

#parserObject (readonly)

Returns the value of attribute parser.



38
39
40
# File 'lib/bugwatch/method_parser.rb', line 38

def parser
  @parser
end

Class Method Details

.find(code, line_range) ⇒ Object



48
49
50
51
52
53
# File 'lib/bugwatch/method_parser.rb', line 48

def self.find(code, line_range)
  method_parser = new(line_range)
  ast = RubyParser.new.process(code)
  method_parser.process ast
  method_parser.klasses
end

Instance Method Details

#current_classObject



89
90
91
# File 'lib/bugwatch/method_parser.rb', line 89

def current_class
  @current_class.join('::')
end

#get_class_name(exp, namespace = []) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/bugwatch/method_parser.rb', line 93

def get_class_name(exp, namespace=[])
  if exp.is_a?(Sexp) && exp.first.is_a?(Sexp)
    get_class_name(exp.first, namespace + [exp.last])
  elsif exp.is_a?(Sexp) && exp[1].is_a?(Sexp)
    get_class_name(exp[1], namespace + [exp.last])
  else
    ([Array(exp).last] + namespace.reverse).join('::')
  end
end

#process_class(exp) ⇒ Object Also known as: process_module



55
56
57
58
59
60
61
62
63
# File 'lib/bugwatch/method_parser.rb', line 55

def process_class(exp)
  begin_line = exp.line
  line_range = begin_line..exp.last.line # TODO figure out last line
  class_name = get_class_name(exp.shift)
  within_class(class_name, line_range) do
    process(exp.shift) until exp.empty?
  end
  s()
end

#process_defn(exp) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/bugwatch/method_parser.rb', line 67

def process_defn(exp)
  first_line_of_method = exp.line
  name = exp.shift
  @klasses[current_class].push name.to_s if within_target?(first_line_of_method..exp.last.line)
  process(exp.shift) until exp.empty?
  s()
end

#within_class(class_name, line_range, &block) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/bugwatch/method_parser.rb', line 75

def within_class(class_name, line_range, &block)
  @current_class.push class_name
  methods_before_process = @klasses[current_class].count
  block.call
  if (methods_before_process == @klasses[current_class].count) && within_target?(line_range)
    @klasses[current_class].push nil
  end
  @current_class.shift
end

#within_target?(range) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/bugwatch/method_parser.rb', line 85

def within_target?(range)
  @line_range.any? {|num| range.cover? num}
end