Permalink
Browse files

Nice template for duplicates. Refactoring. Nicer design, possibly.

  • Loading branch information...
1 parent 3e4987c commit 3e7dfcd091c3258a98833c3ea0623d0deba159cb @gilesbowkett committed Oct 4, 2008
View
@@ -1,4 +1,9 @@
-found in:<% nodes.each do |node| %>
- <%= node.filename %><% end %>
+<% options[:unique_nodes].each do |unique_node| %>
+found in:
+<% options[:nodes].each do |node| %>
+<%= " " + node.filename + "\n" if node.body == unique_node.body %>
+<% end %>
-<%= to_ruby(nodes[0]) %>
+<%= to_ruby(unique_node) %>
+
+<% end %>
View
@@ -4,7 +4,7 @@ def delegate_thru_view(*method_names)
method_names.each do |method_name|
class_eval <<-METHOD
def #{method_name}(*args)
- @view.render(@model.#{method_name}(*args))
+ @view.render(:nodes => @model.#{method_name}(*args))
end
METHOD
end
@@ -13,7 +13,7 @@ def delegate_thru_model(*method_names)
method_names.each do |method_name|
class_eval <<-METHOD
def #{method_name}(*args)
- @model.#{method_name} *args
+ @model.#{method_name}(*args)
end
METHOD
end
View
@@ -1,38 +1,18 @@
-require 'find'
-require 'erb'
-require 'rubygems'
-require 'parse_tree'
-require 'ruby2ruby'
-
-require "#{File.dirname(__FILE__) + "/"}array"
-
-require "#{File.dirname(__FILE__) + "/"}code_base"
-require "#{File.dirname(__FILE__) + "/"}node_analysis"
-require "#{File.dirname(__FILE__) + "/"}model"
-require "#{File.dirname(__FILE__) + "/"}view"
-require "#{File.dirname(__FILE__) + "/"}controller"
-
+%w(find erb rubygems parse_tree ruby2ruby).each {|lib| require lib}
+%w(array code_base node_analysis model view controller).each do |lib|
+ require "#{File.dirname(__FILE__) + "/" + lib}"
+end
class Towelie
- def initialize(view_format)
+ def initialize
@model = Model.new
- template = case view_format
- when :text
- "text.erb"
- when :console
- "console.erb"
- end
- @view = View.new(template)
+ @view = View.new("console.erb")
+ end
+ def parse(dir)
+ @model.parse(dir)
+ end
+ delegate_thru_view :duplicated
+ def duplicates
+ @view.render(:nodes => @model.duplicates, :unique_nodes => @model.duplicates.uniq)
end
- delegate_thru_model :parse, :duplication?, :method_definitions
- delegate_thru_view :duplicated, :unique, :homonyms, :diff
end
-
-# most methods need a dir loaded. therefore we should have an object which takes a dir (and probably
-# loads it) on init. also a new Ruby2Ruby might belong in the initializer, who knows.
-
-# 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 I should write some
-# code which *ensures* it always happens.
View
@@ -5,7 +5,7 @@ def initialize(template_name)
def to_ruby(nodes)
Ruby2Ruby.new.process(nodes) + "\n"
end
- def render(nodes)
- ERB.new(File.read("#{File.dirname(__FILE__) + "/" + @template_name}")).result(binding)
+ def render(options = {})
+ ERB.new(File.read("#{File.dirname(__FILE__) + "/" + @template_name}"), nil, ">").result(binding)
end
end
View
@@ -2,10 +2,11 @@
describe Towelie do
before(:each) do
- @towelie = Towelie.new(:console)
+ @towelie = Towelie.new
end
- before (:all) do
- @duplicate_block =<<DUPLICATE_BLOCK
+
+ it "isolates raw duplicated blocks, showing metadata, given one duplicate" do
+ duplicate_block =<<DUPLICATE_BLOCK
found in:
spec/test_data/second_file.rb
spec/test_data/first_file.rb
@@ -15,15 +16,32 @@ def bar
end
DUPLICATE_BLOCK
- end
- it "isolates raw duplicated blocks, showing metadata, given one duplicate" do
@towelie.parse("spec/test_data")
- @towelie.duplicated.should == @duplicate_block
+ @towelie.duplicates.should == duplicate_block
end
+
it "isolates raw duplicated blocks, showing metadata, given multiple duplicates" do
- # @towelie.parse("spec/multiple_duplicates")
- # @towelie.duplicated.should == @duplicate_block
- pending
+ duplicate_block =<<DUPLICATE_BLOCK
+found in:
+ spec/multiple_duplicates/third_file.rb
+ spec/multiple_duplicates/first_file.rb
+
+def foo
+ "something also non-unique"
+end
+
+found in:
+ spec/multiple_duplicates/second_file.rb
+ spec/multiple_duplicates/first_file.rb
+
+def bar
+ "something non-unique"
+end
+
+DUPLICATE_BLOCK
+
+ @towelie.parse("spec/multiple_duplicates")
+ @towelie.duplicates.should == duplicate_block
end
end
@@ -0,0 +1,7 @@
+def foo
+ "something also non-unique"
+end
+
+def bar
+ "something non-unique"
+end
@@ -0,0 +1,11 @@
+def foo
+ "still unique"
+end
+
+def bar
+ "something non-unique"
+end
+
+def baz
+ "also unique"
+end
@@ -0,0 +1,3 @@
+def foo
+ "something also non-unique"
+end

0 comments on commit 3e7dfcd

Please sign in to comment.