forked from jscruggs/metric_fu
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wrapping LineNumbers in the MetricFu namespace
- Loading branch information
Jake Scruggs
committed
Nov 13, 2010
1 parent
4742cee
commit d0fcfaa
Showing
3 changed files
with
62 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,66 @@ | ||
require 'ruby_parser' | ||
module MetricFu | ||
class LineNumbers | ||
|
||
class LineNumbers | ||
|
||
def initialize(contents) | ||
rp = RubyParser.new | ||
file_sexp = rp.parse(contents) | ||
@locations = {} | ||
case file_sexp[0] | ||
when :class | ||
process_class(file_sexp) | ||
when :module | ||
process_module(file_sexp) | ||
when :block | ||
file_sexp.each_of_type(:class) { |sexp| process_class(sexp) } | ||
else | ||
def initialize(contents) | ||
rp = RubyParser.new | ||
file_sexp = rp.parse(contents) | ||
@locations = {} | ||
case file_sexp[0] | ||
when :class | ||
process_class(file_sexp) | ||
when :module | ||
process_module(file_sexp) | ||
when :block | ||
file_sexp.each_of_type(:class) { |sexp| process_class(sexp) } | ||
else | ||
end | ||
end | ||
end | ||
|
||
def in_method? line_number | ||
!!@locations.detect do |method_name, line_number_range| | ||
line_number_range.include?(line_number) | ||
def in_method? line_number | ||
!!@locations.detect do |method_name, line_number_range| | ||
line_number_range.include?(line_number) | ||
end | ||
end | ||
end | ||
|
||
def method_at_line line_number | ||
found_method_and_range = @locations.detect do |method_name, line_number_range| | ||
line_number_range.include?(line_number) | ||
def method_at_line line_number | ||
found_method_and_range = @locations.detect do |method_name, line_number_range| | ||
line_number_range.include?(line_number) | ||
end | ||
return nil unless found_method_and_range | ||
found_method_and_range.first | ||
end | ||
return nil unless found_method_and_range | ||
found_method_and_range.first | ||
end | ||
|
||
private | ||
private | ||
|
||
def process_module(sexp) | ||
module_name = sexp[1] | ||
sexp.each_of_type(:class) do |sexp| | ||
process_class(sexp, module_name) | ||
hide_methods_from_next_round(sexp) | ||
def process_module(sexp) | ||
module_name = sexp[1] | ||
sexp.each_of_type(:class) do |sexp| | ||
process_class(sexp, module_name) | ||
hide_methods_from_next_round(sexp) | ||
end | ||
process_class(sexp) | ||
end | ||
process_class(sexp) | ||
end | ||
|
||
def process_class(sexp, module_name=nil) | ||
class_name = sexp[1] | ||
process_class_self_blocks(sexp, class_name) | ||
module_name_string = module_name ? "#{module_name}::" : nil | ||
sexp.each_of_type(:defn) { |s| @locations["#{module_name_string}#{class_name}##{s[1]}"] = (s.line)..(s.last.line) } | ||
sexp.each_of_type(:defs) { |s| @locations["#{module_name_string}#{class_name}::#{s[2]}"] = (s.line)..(s.last.line) } | ||
end | ||
def process_class(sexp, module_name=nil) | ||
class_name = sexp[1] | ||
process_class_self_blocks(sexp, class_name) | ||
module_name_string = module_name ? "#{module_name}::" : nil | ||
sexp.each_of_type(:defn) { |s| @locations["#{module_name_string}#{class_name}##{s[1]}"] = (s.line)..(s.last.line) } | ||
sexp.each_of_type(:defs) { |s| @locations["#{module_name_string}#{class_name}::#{s[2]}"] = (s.line)..(s.last.line) } | ||
end | ||
|
||
def process_class_self_blocks(sexp, class_name) | ||
sexp.each_of_type(:sclass) do |sexp_in_class_self_block| | ||
sexp_in_class_self_block.each_of_type(:defn) { |s| @locations["#{class_name}::#{s[1]}"] = (s.line)..(s.last.line) } | ||
hide_methods_from_next_round(sexp_in_class_self_block) | ||
def process_class_self_blocks(sexp, class_name) | ||
sexp.each_of_type(:sclass) do |sexp_in_class_self_block| | ||
sexp_in_class_self_block.each_of_type(:defn) { |s| @locations["#{class_name}::#{s[1]}"] = (s.line)..(s.last.line) } | ||
hide_methods_from_next_round(sexp_in_class_self_block) | ||
end | ||
end | ||
end | ||
|
||
def hide_methods_from_next_round(sexp) | ||
sexp.find_and_replace_all(:defn, :ignore_me) | ||
sexp.find_and_replace_all(:defs, :ignore_me) | ||
end | ||
def hide_methods_from_next_round(sexp) | ||
sexp.find_and_replace_all(:defn, :ignore_me) | ||
sexp.find_and_replace_all(:defs, :ignore_me) | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters