activity fix and tests

This commit is contained in:
Tomasz Gieniusz 2012-10-13 11:49:02 +02:00
parent 00e99ea4af
commit 6a846c5179
4 changed files with 74 additions and 16 deletions

View file

@ -7,9 +7,5 @@ $LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib)
# start up the CLI # start up the CLI
require "git_stats/cli" require "git_stats/cli"
begin cli = GitStats::CLI.new
cli = GitStats::CLI.new cli.start(*ARGV)
cli.start(*ARGV)
rescue ArgumentError => e
puts e.message
end

View file

@ -5,15 +5,37 @@ module GitStats
attr_reader :by_hour, :by_wday, :by_wday_hour, :by_month, :by_year, :by_year_week attr_reader :by_hour, :by_wday, :by_wday_hour, :by_month, :by_year, :by_year_week
def initialize(commits) def initialize(commits)
@by_hour = @by_wday = @by_month = @by_year = Hash.new(0)
@by_wday_hour = @by_year_week = Hash.new { |h, k| h[k] = Hash.new(0) }
add_commits(commits) add_commits(commits)
end end
def by_hour
@by_hour ||= default_hash
end
def by_wday
@by_wday ||= default_hash
end
def by_wday_hour
@by_wday_hour ||= default_double_hash
end
def by_month
@by_month ||= default_hash
end
def by_year
@by_year ||= default_hash
end
def by_year_month
@by_year_week ||= default_double_hash
end
private private
def add_commits(commits) def add_commits(commits)
commits.values.each do |commit| commits = commits.values if commits.is_a? Hash
commits.each do |commit|
add_commit_at(commit.date) add_commit_at(commit.date)
end end
end end
@ -24,7 +46,15 @@ module GitStats
self.by_wday_hour[date.wday][date.hour] += 1 self.by_wday_hour[date.wday][date.hour] += 1
self.by_month[date.month] += 1 self.by_month[date.month] += 1
self.by_year[date.year] += 1 self.by_year[date.year] += 1
self.by_year_week[date.year][date.cweek] += 1 self.by_year_month[date.year][date.month] += 1
end
def default_hash
Hash.new(0)
end
def default_double_hash
Hash.new { |h, k| h[k] = Hash.new(0) }
end end
end end

View file

@ -7,11 +7,6 @@ module GitStats
attr_reader :repo, :hash, :stamp, :date, :author attr_reader :repo, :hash, :stamp, :date, :author
def gather_all_data
files_count
short_stat
end
def files_count def files_count
@files_count ||= Command.new(repo, "git ls-tree -r --name-only #{self.hash} | wc -l").run.to_i @files_count ||= Command.new(repo, "git ls-tree -r --name-only #{self.hash} | wc -l").run.to_i
end end

37
spec/activity_spec.rb Normal file
View file

@ -0,0 +1,37 @@
require 'spec_helper'
describe GitStats::GitData::Activity do
let(:dates) { [
'10.05.2012 12:37',
'10.05.2012 13:53',
'06.05.2012 13:23',
'15.06.2011 15:02',
'27.09.2011 15:34'
] }
let(:commits) { dates.map { |d| double(:date => DateTime.parse(d)) } }
let(:activity) { GitStats::GitData::Activity.new(commits) }
it 'by_hour should count commits by hour' do
activity.by_hour.should == {12 => 1, 13 => 2, 15 => 2}
end
it 'by_wday should count commits by day of week where 0 = sunday, 1 = monday, ...' do
activity.by_wday.should == {0 => 1, 2 => 1, 3 => 1, 4 => 2}
end
it 'by_wday_hour should count commits by day of week and by hour' do
activity.by_wday_hour.should == {0 => {13 => 1}, 2 => {15 => 1}, 3 => {15 => 1}, 4 => {12 => 1, 13 => 1}}
end
it 'by_month should count commits by month' do
activity.by_month.should == {5 => 3, 6 => 1, 9 => 1}
end
it 'by_year should count commits by year' do
activity.by_year.should == {2011 => 2, 2012 => 3}
end
it 'by_year_month should count commits by day of year and by month' do
activity.by_year_month.should == {2011 => {6 => 1, 9 => 1}, 2012 => {5 => 3}}
end
end