From ff62823dd2c5e0cfaf10ba3a9db77fc7d8b05cec Mon Sep 17 00:00:00 2001 From: Tomasz Gieniusz Date: Sun, 21 Oct 2012 23:11:37 +0200 Subject: [PATCH] refactor --- lib/git_stats/git_data/commit.rb | 2 +- lib/git_stats/git_data/repo.rb | 2 +- .../stats_view/charts/repo_charts.rb | 52 ++++++------------- spec/git_data/commit_spec.rb | 2 +- spec/integration/repo_spec.rb | 2 +- 5 files changed, 21 insertions(+), 39 deletions(-) diff --git a/lib/git_stats/git_data/commit.rb b/lib/git_stats/git_data/commit.rb index 7ffa7abcc..61b0aef3f 100644 --- a/lib/git_stats/git_data/commit.rb +++ b/lib/git_stats/git_data/commit.rb @@ -29,7 +29,7 @@ module GitStats @files_by_extension_count ||= Hash[files_by_extension.map { |ext, files| [ext, files.count] }] end - def lines_by_extension + def lines_by_extension_count @lines_by_extension ||= Hash[files_by_extension.map { |ext, files| [ext, files.map(&:lines_count).sum] }.delete_if { |ext, lines_count| lines_count == 0 }] diff --git a/lib/git_stats/git_data/repo.rb b/lib/git_stats/git_data/repo.rb index 88c530f26..d7dbba88e 100644 --- a/lib/git_stats/git_data/repo.rb +++ b/lib/git_stats/git_data/repo.rb @@ -7,7 +7,7 @@ module GitStats attr_reader :path - delegate :files, :files_by_extension, :files_by_extension_count, :lines_by_extension, + delegate :files, :files_by_extension, :files_by_extension_count, :lines_by_extension_count, :files_count, :binary_files, :text_files, :lines_count, to: :last_commit def initialize(params) diff --git a/lib/git_stats/stats_view/charts/repo_charts.rb b/lib/git_stats/stats_view/charts/repo_charts.rb index e39c5ed3c..94f3151ad 100644 --- a/lib/git_stats/stats_view/charts/repo_charts.rb +++ b/lib/git_stats/stats_view/charts/repo_charts.rb @@ -6,43 +6,25 @@ module GitStats @repo = repo end - def files_by_extension - Chart.new do |f| - f.column_hash_chart( - data: @repo.files_by_extension_count, - title: :files_by_extension.t, - y_text: :files.t - ) + [:lines, :files].each do |type| + define_method "#{type}_by_extension" do + Chart.new do |f| + f.column_hash_chart( + data: @repo.send("#{type}_by_extension_count"), + title: "#{type}_extension".to_sym.t, + y_text: type.to_sym.t + ) + end end - end - def lines_by_extension - Chart.new do |f| - f.column_hash_chart( - data: @repo.lines_by_extension, - title: :lines_by_extension.t, - y_text: :lines.t - ) - end - end - - def files_by_date - Chart.new do |f| - f.date_chart( - data: @repo.files_count_by_date, - title: :files_by_date.t, - y_text: :files.t - ) - end - end - - def lines_by_date - Chart.new do |f| - f.date_chart( - data: @repo.lines_count_by_date, - title: :lines_by_date.t, - y_text: :lines.t - ) + define_method "#{type}_by_date" do + Chart.new do |f| + f.date_chart( + data: @repo.send("#{type}_count_by_date"), + title: "#{type}_by_date".to_sym.t, + y_text: type.to_sym.t + ) + end end end diff --git a/spec/git_data/commit_spec.rb b/spec/git_data/commit_spec.rb index 723be38ff..99fe9e2db 100644 --- a/spec/git_data/commit_spec.rb +++ b/spec/git_data/commit_spec.rb @@ -40,7 +40,7 @@ describe GitStats::GitData::Commit do double(lines_count: 0, extension: '.haml'), double(lines_count: 20, extension: '.css'), ) - commit.lines_by_extension.should == {'.rb' => 100, '.css' => 20} + commit.lines_by_extension_count.should == {'.rb' => 100, '.css' => 20} end end end diff --git a/spec/integration/repo_spec.rb b/spec/integration/repo_spec.rb index af0ea0354..d3c8e60df 100644 --- a/spec/integration/repo_spec.rb +++ b/spec/integration/repo_spec.rb @@ -46,7 +46,7 @@ describe GitStats::GitData::Repo do end it 'should count lines by extension in repo' do - repo.lines_by_extension.should == {'.haml' => 100, '.txt' => 1008, '.rb' => 6} + repo.lines_by_extension_count.should == {'.haml' => 100, '.txt' => 1008, '.rb' => 6} end it 'should count commits_count_by_author' do