Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactor - moving different concerns into separate modules in separat…

…e files.
  • Loading branch information...
commit d76c6765902b5f5765c291981241bc34a17b1728 1 parent 6f3e40d
@gilesbowkett authored
View
39 lib/code_base.rb
@@ -0,0 +1,39 @@
+module Towelie
+ module CodeBase
+ def files(dir)
+ # Find supplies no #inject
+ accumulator = []
+ Find.find(dir) do |filename|
+ next if File.directory? filename or filename =~ /.*\.(git|svn).*/
+ accumulator << 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
+ end
+ else
+ method_definitions(accumulator, node)
+ end
+ end
+ end
+ accumulator
+ end
+ end
+end
View
41 lib/node_analysis.rb
@@ -0,0 +1,41 @@
+module Towelie
+ module NodeAnalysis
+ def duplication?(dir)
+ parse dir
+ not duplicates.empty?
+ end
+ def duplicated(dir)
+ parse dir
+ duplicates
+ end
+ def duplicates
+ (method_definitions.collect do |node|
+ node if method_definitions.duplicates? node
+ end).compact.uniq
+ end
+ def unique(dir)
+ parse dir
+ 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,
+ # and I should probably run .uniq on it also.
+ 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|
+ 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
+ end
+ end
+ diff_nodes
+ end
+ end
+end
View
93 lib/towelie.rb
@@ -3,93 +3,14 @@
require 'parse_tree'
require 'ruby2ruby'
+require "#{File.dirname(__FILE__) + "/"}code_base"
+require "#{File.dirname(__FILE__) + "/"}node_analysis"
+require "#{File.dirname(__FILE__) + "/"}view"
+
module Towelie
-
- # model
-
- def files(dir)
- # Find supplies no #inject
- accumulator = []
- Find.find(dir) do |filename|
- next if File.directory? filename or filename =~ /.*\.(git|svn).*/
- accumulator << 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
- end
- else
- method_definitions(accumulator, node)
- end
- end
- end
- accumulator
- end
-
- # controller
-
- def duplication?(dir)
- parse dir
- not duplicates.empty?
- end
- def duplicated(dir)
- parse dir
- duplicates
- end
- def duplicates
- (method_definitions.collect do |node|
- node if method_definitions.duplicates? node
- end).compact.uniq
- end
- def unique(dir)
- parse dir
- 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,
- # and I should probably run .uniq on it also.
- 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|
- 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
- end
- end
- diff_nodes
- end
-
- # view
-
- def to_ruby(nodes)
- nodes.inject("") do |string, node|
- string += Ruby2Ruby.new.process(node) + "\n\n"
- end
- end
+ include CodeBase
+ include NodeAnalysis
+ include View
end
# most methods need a dir loaded. therefore we should have an object which takes a dir (and probably
View
9 lib/view.rb
@@ -0,0 +1,9 @@
+module Towelie
+ module View
+ def to_ruby(nodes)
+ nodes.inject("") do |string, node|
+ string += Ruby2Ruby.new.process(node) + "\n\n"
+ end
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.