Permalink
Browse files

MVC refactoring. Undecided about this.

  • Loading branch information...
1 parent f17abcd commit d7dfdb3a295c185f6f00ddb660f0d2c88c7855dd @gilesbowkett committed Oct 2, 2008
Showing with 71 additions and 36 deletions.
  1. +2 −1 lib/code_base.rb
  2. +5 −0 lib/model.rb
  3. +3 −1 lib/node_analysis.rb
  4. +32 −5 lib/towelie.rb
  5. +4 −6 lib/view.rb
  6. +25 −23 spec/towelie_spec.rb
View
@@ -1,4 +1,4 @@
-module Towelie
+class Model
module CodeBase
def parse(dir)
@method_definitions = []
@@ -7,6 +7,7 @@ def parse(dir)
extract_definitions(@method_definitions, (ParseTree.translate File.read(filename)), filename)
end
end
+ alias :dir= :parse
def extract_definitions(accumulator, nodes, filename)
case nodes
when Array
View
@@ -0,0 +1,5 @@
+class Model
+ include CodeBase
+ include NodeAnalysis
+ attr_reader :method_definitions
+end
View
@@ -1,4 +1,4 @@
-module Towelie
+class Model
module NodeAnalysis
def duplication?(dir)
parse dir
@@ -39,3 +39,5 @@ def diff(threshold)
end
end
end
+
+# both uses of stepwise are actually injects. therefore I should change them.
View
@@ -3,14 +3,41 @@
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"
-module Towelie
- include CodeBase
- include NodeAnalysis
- include View
+
+class Towelie
+ def initialize
+ @model = Model.new
+ @view = View.new
+ end
+ def parse(dir)
+ @model.parse(dir)
+ end
+ alias :dir= :parse
+ def duplication?(dir)
+ @model.duplication?(dir)
+ end
+ def method_definitions
+ @model.method_definitions
+ end
+ def duplicated(dir)
+ @view.to_ruby(@model.duplicated(dir))
+ end
+ def unique(dir)
+ @view.to_ruby(@model.unique(dir))
+ end
+ def homonyms(dir)
+ @view.to_ruby(@model.homonyms(dir))
+ end
+ def diff(threshold)
+ @view.to_ruby(@model.diff(threshold))
+ end
end
# most methods need a dir loaded. therefore we should have an object which takes a dir (and probably
@@ -20,4 +47,4 @@ module Towelie
# 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.
+# code which *ensures* it always happens.
View
@@ -1,9 +1,7 @@
-module Towelie
- module View
- def to_ruby(nodes)
- nodes.inject("") do |string, node|
- string += Ruby2Ruby.new.process(node) + "\n\n"
- end
+class View
+ def to_ruby(nodes)
+ nodes.inject("") do |string, node|
+ string += Ruby2Ruby.new.process(node) + "\n\n"
end
end
end
View
@@ -1,8 +1,10 @@
require 'lib/array'
require 'lib/towelie'
-include Towelie
describe Towelie do
+ before(:each) do
+ @towelie = Towelie.new
+ end
before(:all) do
@the_nodes = [
# second_file.rb
@@ -90,43 +92,43 @@ def foo
TWO_NODE_DIFF_BLOCK
end
it "identifies duplication" do
- duplication?("spec/test_data").should be_true
- duplication?("spec/classes_modules").should be_true
+ @towelie.duplication?("spec/test_data").should be_true
+ @towelie.duplication?("spec/classes_modules").should be_true
end
it "returns no false positives when identifying duplication" do
- duplication?("spec/non_duplicating_data").should be_false
+ @towelie.duplication?("spec/non_duplicating_data").should be_false
end
it "extracts :defn nodes" do
- parse("spec/test_data")
- @method_definitions.should == @the_nodes
- parse("spec/classes_modules")
- @method_definitions.should == @the_nodes
+ @towelie.dir = "spec/test_data"
+ @towelie.method_definitions.should == @the_nodes
+ @towelie.parse("spec/classes_modules")
+ @towelie.method_definitions.should == @the_nodes
end
it "isolates duplicated blocks" do
- to_ruby(duplicated("spec/test_data")).should == @duplicated_block
- to_ruby(duplicated("spec/classes_modules")).should == @duplicated_block
+ @towelie.duplicated("spec/test_data").should == @duplicated_block
+ @towelie.duplicated("spec/classes_modules").should == @duplicated_block
end
it "reports unique code" do
- to_ruby(unique("spec/test_data")).should == @unique_block
- to_ruby(unique("spec/classes_modules")).should == @unique_block
+ @towelie.unique("spec/test_data").should == @unique_block
+ @towelie.unique("spec/classes_modules").should == @unique_block
end
it "reports distinct methods with the same name" do
- to_ruby(homonyms("spec/test_data")).should == @homonym_block
- to_ruby(homonyms("spec/classes_modules")).should == @homonym_block
+ @towelie.homonyms("spec/test_data").should == @homonym_block
+ @towelie.homonyms("spec/classes_modules").should == @homonym_block
end
it "reports methods which differ only by one node" do
- parse("spec/one_node_diff")
- to_ruby(diff(1)).should == @one_node_diff_block
- parse("spec/larger_one_node_diff")
- to_ruby(diff(1)).should == @bigger_one_node_diff_block
+ @towelie.parse("spec/one_node_diff")
+ @towelie.diff(1).should == @one_node_diff_block
+ @towelie.parse("spec/larger_one_node_diff")
+ @towelie.diff(1).should == @bigger_one_node_diff_block
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
+ @towelie.parse("spec/two_node_diff")
+ @towelie.method_definitions.should_not be_empty
+ @towelie.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"
+ @towelie.parse("spec/two_node_diff")
+ @towelie.method_definitions[0].filename.should == "spec/two_node_diff/second_file.rb"
end
end

0 comments on commit d7dfdb3

Please sign in to comment.