Browse files

Refactoring; and Towelie now collects metadata of the most basic sort…

…, namely, filenames.
  • Loading branch information...
1 parent d76c676 commit f17abcd0fd28dbc10e13cf19c3852021299f217c @gilesbowkett committed Sep 30, 2008
Showing with 35 additions and 43 deletions.
  1. +20 −26 lib/code_base.rb
  2. +6 −6 lib/node_analysis.rb
  3. +2 −5 lib/towelie.rb
  4. +7 −6 spec/towelie_spec.rb
View
46 lib/code_base.rb
@@ -1,36 +1,30 @@
module Towelie
module CodeBase
- def files(dir)
- # Find supplies no #inject
- accumulator = []
+ def parse(dir)
+ @method_definitions = []
Find.find(dir) do |filename|
next if File.directory? filename or filename =~ /.*\.(git|svn).*/
- accumulator << filename
+ extract_definitions(@method_definitions, (ParseTree.translate File.read(filename)), filename)
end
- accumulator
end
- def parse(dir)
- @nodes = files(dir).inject([]) do |array, filename|
- array << (ParseTree.translate File.read(filename)) ; array
- end
- end
- def method_definitions(accumulator = [], nodes = @nodes)
- nodes.each do |node|
- case node
- when Array
- if node[0] == :defn
- accumulator << node
- class << node
- def name
- self[1]
- end
- def body
- self[2]
- end
+ def extract_definitions(accumulator, nodes, filename)
+ case nodes
+ when Array
+ if nodes[0] == :defn
+ accumulator << nodes
+ nodes.instance_eval <<-ACCESSORS
+ def name
+ self[1]
+ end
+ def body
+ self[2]
+ end
+ def filename
+ "#{filename}"
end
- else
- method_definitions(accumulator, node)
- end
+ ACCESSORS
+ else
+ nodes.each {|node| extract_definitions(accumulator, node, filename)}
end
end
accumulator
View
12 lib/node_analysis.rb
@@ -9,27 +9,27 @@ def duplicated(dir)
duplicates
end
def duplicates
- (method_definitions.collect do |node|
- node if method_definitions.duplicates? node
+ (@method_definitions.collect do |node|
+ node if @method_definitions.duplicates? node
end).compact.uniq
end
def unique(dir)
parse dir
- method_definitions - duplicates
+ @method_definitions - duplicates
end
def homonyms(dir)
parse dir
homonyms = []
- # this picks up duplicates as well as homonyms, since a duplicate is technically also a homonym,
+ # this picks up duplicates as well as homonyms, since a duplicate is technically also a homonym;
# and I should probably run .uniq on it also.
- method_definitions.stepwise do |method_definition_1, method_definition_2|
+ @method_definitions.stepwise do |method_definition_1, method_definition_2|
homonyms << method_definition_1 if method_definition_1.name == method_definition_2.name
end
homonyms
end
def diff(threshold)
diff_nodes = []
- method_definitions.stepwise do |method_definition_1, method_definition_2|
+ @method_definitions.stepwise do |method_definition_1, method_definition_2|
if threshold >= (method_definition_1.body - method_definition_2.body).size
diff_nodes << method_definition_1
# note this hash approach fails to record multiple one-node-diff methods with the same name
View
7 lib/towelie.rb
@@ -19,8 +19,5 @@ module Towelie
# ironically, Towelie itself is very not-DRY. lots of "parse dir".
# one thing I've been doing consistently is parsing the dir and collecting the method definitions.
-# further, everything here assumes that this has happened. therefore! I think A) I should write some
-# code which *ensures* it always happens and B) I think I should incorporate method definition
-# collecting into the process where I loop through filenames. the reason, of course, is that it
-# allows me to trivially collect filenames in the process, and thereby obtain at least the most
-# basic metadata.
+# further, everything here assumes that this has happened. therefore! I think I should write some
+# code which *ensures* it always happens.
View
13 spec/towelie_spec.rb
@@ -89,10 +89,6 @@ def foo
TWO_NODE_DIFF_BLOCK
end
- it "scans a directory, returning a list of files" do
- files("spec/test_data").sort.should == ["spec/test_data/first_file.rb",
- "spec/test_data/second_file.rb"]
- end
it "identifies duplication" do
duplication?("spec/test_data").should be_true
duplication?("spec/classes_modules").should be_true
@@ -102,9 +98,9 @@ def foo
end
it "extracts :defn nodes" do
parse("spec/test_data")
- method_definitions.should == @the_nodes
+ @method_definitions.should == @the_nodes
parse("spec/classes_modules")
- method_definitions.should == @the_nodes
+ @method_definitions.should == @the_nodes
end
it "isolates duplicated blocks" do
to_ruby(duplicated("spec/test_data")).should == @duplicated_block
@@ -126,6 +122,11 @@ def foo
end
it "reports methods which differ by arbitrary numbers of nodes" do
parse("spec/two_node_diff")
+ @method_definitions.should_not be_empty
to_ruby(diff(2)).should == @two_node_diff_block
end
+ it "attaches filenames to individual nodes" do
+ parse("spec/two_node_diff")
+ @method_definitions[0].filename.should == "spec/two_node_diff/second_file.rb"
+ end
end

0 comments on commit f17abcd

Please sign in to comment.