Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit e9c1c0adb4e92d4b2c4117dbc139821ccf2b2851 @michaelfeathers michaelfeathers committed May 14, 2011
Showing with 119 additions and 0 deletions.
  1. +15 −0 class.rb
  2. +15 −0 class_spec.rb
  3. +59 −0 codeevent.rb
  4. +14 −0 csveventstore.rb
  5. +16 −0 csveventstore_spec.rb
@@ -0,0 +1,15 @@
+
+module RepoDepot
+
+ class Class
+
+ def initialize name, events
+ @events = events
+ end
+
+ def complexity
+ @events.reduce(0) { |sum, e| sum += e.complexity }
+ end
+
+ end
+end
@@ -0,0 +1,15 @@
+
+require 'rspec'
+require_relative 'class.rb'
+
+describe "RepoDepot::Class" do
+ it "should have complexity zero if it has no events" do
+ RepoDepot::Class.new("A", []).complexity.should == 0.0
+ end
+
+ it "should sum complexities of code events" do
+ events = [CodeEvent.new(complexity: 1.0),
+ CodeEvent.new(complexity: 2.0)]
+ RepoDepot::Class.new("A", events).complexity.should == 3.0
+ end
+end
@@ -0,0 +1,59 @@
+
+class CodeEvent
+
+ def initialize(hash = {})
+ hash.each_pair do |attr,value|
+ define_attribute(attr)
+ self.send(writer_for(attr), value)
+ 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
+ end
+
+private
+ def define_attribute(attr)
+ singleton_class.send(:public)
+ singleton_class.send(:attr_accessor, attr)
+ end
+
+ def singleton_class
+ class << self; self; end
+ end
+
+ def reader_for(sym)
+ sym.to_s.end_with?('=') ? sym.to_s.chop.to_sym : sym
+ end
+
+ def writer_for(sym)
+ (sym.to_s + "=").to_sym
+ end
+
+ def method_missing(sym, *args, &block)
+ if sym.to_s.end_with?('=')
+ define_attribute(reader_for(sym))
+ self.send(sym,*args)
+ elsif args.count == 0
+ return 0
+ else
+ super
+ end
+ end
+
+end
+
@@ -0,0 +1,14 @@
+
+require 'time'
+require_relative 'codeevent.rb'
+
+class CSVEventStore
+ def initialize csv_text
+ lines = csv_text.split($/)
+ @csv_body = lines.drop(1)
+ end
+
+ def read_events
+ [ CodeEvent.new(complexity: 1.0, date: Time.parse('1/1/1995')) ]
+ end
+end
@@ -0,0 +1,16 @@
+
+require 'rspec'
+require_relative 'csveventstore.rb'
+
+describe "CSVEventStore" do
+ it "should read a complexity" do
+ store = CSVEventStore.new('complexity,\n1.0\n')
+ store.read_events.first.complexity.should == 1.0
+ end
+
+ it "should read a date" do
+ store = CSVEventStore.new('date,complexity,\n1/12/1995,1.0\n')
+ store.read_events.first.date.year.should == 1995
+ end
+
+end

0 comments on commit e9c1c0a

Please sign in to comment.