Permalink
Browse files

general refactoring and addition of methods collection to rrepository.

  • Loading branch information...
1 parent 7196b85 commit 255e1a1a8b930678ce02987102f6d4903a543798 @michaelfeathers committed May 15, 2011
Showing with 45 additions and 44 deletions.
  1. +5 −6 class.rb
  2. +3 −2 class_spec.rb
  3. +5 −16 codeevent.rb
  4. +3 −3 codehistory.rb
  5. +5 −8 method.rb
  6. +7 −0 plans
  7. +6 −6 repository.rb
  8. +10 −0 repository_spec.rb
  9. +1 −3 sourcefile.rb
View
@@ -2,18 +2,17 @@
require_relative 'codehistory.rb'
require_relative 'method.rb'
+# Calling the collection `methods'. If the override of
+# Object#methods is too confusing, may rename it.
+
module RepoDepot
class Class < CodeHistory
- attr_reader :declared_methods
- def initialize name, events
- super
- @declared_methods = build_collection(:method_name, Method)
- end
+ def methods; @methods ||= collection_of(:method_name, Method); end
def complexity
- complexity_of(@declared_methods)
+ complexity_of(methods)
end
end
View
@@ -14,12 +14,13 @@
end
it "should have no methods when it is given no events" do
- RepoDepot::Class.new("A", []).declared_methods.should be_empty
+ RepoDepot::Class.new("A", []).methods.should be_empty
end
it "should have two methods when given appropriate events" do
events = [CodeEvent.new(method_name: 'a'),
CodeEvent.new(method_name: 'b')]
- RepoDepot::Class.new("A", events).declared_methods.count.should == 2
+ RepoDepot::Class.new("A", events).methods.count.should == 2
end
+
end
View
@@ -8,22 +8,11 @@ def initialize(hash = {})
end
end
- def file_name
- return nil unless instance_variable_get(:@location)
- location.split(/\//).select {|seg| seg.end_with?('.rb') }.first
- end
-
- def class_name
- return nil unless instance_variable_get(:@location)
- segs = location.split(/\//)
- class_index = segs.find_index {|seg| seg.end_with?('.rb')}
- return nil unless class_index
- segs[class_index + 1]
- end
-
- def method_name
- return nil unless instance_variable_get(:@location)
- location.split(/\//).last
+ # Considering moving this to code that populates
+ # events so that this class isn't making assumptions
+ # about what it will be populated with
+ def full_method_name
+ class_name + "#" + method_name
end
private
View
@@ -2,15 +2,15 @@
module RepoDepot
- class CodeHistory < Struct.new(:name,:events)
+ class CodeHistory < Struct.new(:name, :events)
def commits
- @commits ||= events.map(&:commit)
+ @commits ||= events.map(&:commit).uniq
end
protected
- def build_collection symbol, class_object
+ def collection_of symbol, class_object
events.group_by(&symbol).map { |args| class_object.new(*args) }
end
View
@@ -1,24 +1,21 @@
module RepoDepot
- class Method
- def initialize name, events
- @events = events
- end
+ class Method < CodeHistory
def life_line
- @events.map(&:complexity)
+ events.map(&:complexity)
end
def deltas
- @events.each_cons(2).map do |first, second|
+ events.each_cons(2).map do |first, second|
second.complexity - first.complexity
end
end
def complexity
- return 0.0 if @events == []
- @events.last.complexity
+ return 0.0 if events.empty?
+ events.last.complexity
end
end
View
7 plans
@@ -1,5 +1,7 @@
+- Add in csv read and normalization for non-string typed
+ fields
- Have commits on Repository return a new Repository
which has only the CodeEvent for that commit
- Have functions on repository which give a slice of commits
@@ -16,4 +18,9 @@
delta -- needed now as we have code events for methods
just because they are in a file. May decide that
it is better to just handle this with external code.
+- Clean up CodeEvent
+- Right now, Method doesn't inherit from CodeHistory.
+ Any need to?
+- Create a Diff class with removed_methods, changed_methods, added_methods
+ working off of two histories
View
@@ -5,16 +5,16 @@
module RepoDepot
class Repository < CodeHistory
- attr_reader :classes, :source_files
- def initialize name, events
- super
- @classes = build_collection(:class_name, Class)
- @source_files = build_collection(:file_name, SourceFile)
+ def source_files; @source_files ||= collection_of(:file_name, SourceFile); end
+ def classes; @classes ||= collection_of(:class_name, Class); end
+
+ def methods
+ classes.map { |c| c.methods }.flatten
end
def complexity
- complexity_of(@classes)
+ complexity_of(classes)
end
end
View
@@ -44,4 +44,14 @@
RepoDepot::Repository.new('', events).complexity.should be_within(0.1).of(0.6)
end
+ it "should return methods for all of its classes" do
+ events = [
+ CodeEvent.new(class_name: "A", method_name: "a"),
+ CodeEvent.new(class_name: "B", method_name: "a"),
+ CodeEvent.new(class_name: "B", method_name: "b")
+ ]
+ methods = RepoDepot::Repository.new('', events).methods
+ methods.map(&:name).should == ["a", "a", "b"]
+ end
+
end
View
@@ -1,7 +1,5 @@
module RepoDepot
- class SourceFile
- def initialize name, events
- end
+ class SourceFile < CodeHistory
end
end

0 comments on commit 255e1a1

Please sign in to comment.