From c3caf0180ac3bf1f208ab60239eebeda2abb67de Mon Sep 17 00:00:00 2001 From: Tomasz Gieniusz Date: Sun, 22 Jun 2014 15:26:03 +0200 Subject: [PATCH] refactor near default values of parameters --- README.md | 4 ++-- lib/git_stats/cli.rb | 6 +++--- lib/git_stats/generator.rb | 9 +++++---- lib/git_stats/git_data/repo.rb | 27 ++++++++++++++++++++------- lib/git_stats/git_data/tree.rb | 4 +--- spec/git_data/generator_spec.rb | 10 ++++------ 6 files changed, 35 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 241e16e72..870e110b9 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ It browses the repository and outputs html page with statistics. Options: p, [--path=PATH] # Path to repository from which statistics should be generated. # Default: . - o, [--output=OUTPUT] # Output path where statistics should be written. + o, [--out-path=OUT_PATH] # Output path where statistics should be written. # Default: ./git_stats l, [--language=LANGUAGE] # Language of written statistics. # Default: en @@ -47,7 +47,7 @@ It browses the repository and outputs html page with statistics. d, [--tree=TREE] # Tree where statistics should be generated. # Default: . c, [--comment=COMMENT] # The string which is used for comments. - # Default: /// + # Default: // diff --git a/lib/git_stats/cli.rb b/lib/git_stats/cli.rb index bff4b5122..79f5d5cb9 100644 --- a/lib/git_stats/cli.rb +++ b/lib/git_stats/cli.rb @@ -4,18 +4,18 @@ require "thor" class GitStats::CLI < Thor option :path, :aliases => :p, :default => '.', :desc => 'Path to repository from which statistics should be generated.' - option :output, :aliases => :o, :default => './git_stats', :desc => 'Output path where statistics should be written.' + option :out_path, :aliases => :o, :default => './git_stats', :desc => 'Output path where statistics should be written.' option :language, :aliases => :l, :default => 'en', :desc => 'Language of written statistics.' option :from, :aliases => :f, :desc => 'Commit from where statistics should start.' option :to, :aliases => :t, :default => 'HEAD', :desc => 'Commit where statistics should stop.' option :silent, :aliases => :s, :type => :boolean, :desc => 'Silent mode. Don\'t output anything.' option :tree, :aliases => :d, :default => '.', :desc => 'Tree where statistics should be generated.' - option :comment, :aliases => :c, :default => '///', :desc => 'The string which is used for comments.' + option :comment, :aliases => :c, :default => '//', :desc => 'The string which is used for comments.' desc 'generate', 'Generates the statistics of a repository' def generate I18n.locale = options[:language] - GitStats::Generator.new(options[:path], options[:output], options[:from], options[:to], options[:tree], options[:comment]) { |g| + GitStats::Generator.new(options) { |g| g.add_command_observer { |command, result| puts "#{command}" } unless options[:silent] }.render_all end diff --git a/lib/git_stats/generator.rb b/lib/git_stats/generator.rb index ec6949baa..092ba6885 100644 --- a/lib/git_stats/generator.rb +++ b/lib/git_stats/generator.rb @@ -4,12 +4,13 @@ module GitStats delegate :add_command_observer, to: :@repo delegate :render_all, to: :@view - def initialize(repo_path, out_path, first_commit_sha = nil, last_commit_sha = "HEAD", tree_path = ".", comment_string = "///") - validate_repo_path(repo_path) + def initialize(options) + + validate_repo_path(options[:path]) - @repo = GitData::Repo.new(path: repo_path, first_commit_sha: first_commit_sha, last_commit_sha: last_commit_sha, tree_path: tree_path, comment_string: comment_string) + @repo = GitData::Repo.new(options) view_data = StatsView::ViewData.new(@repo) - @view = StatsView::View.new(view_data, out_path) + @view = StatsView::View.new(view_data, options[:out_path]) yield self if block_given? end diff --git a/lib/git_stats/git_data/repo.rb b/lib/git_stats/git_data/repo.rb index e57aba9fa..23fd036f4 100644 --- a/lib/git_stats/git_data/repo.rb +++ b/lib/git_stats/git_data/repo.rb @@ -6,8 +6,6 @@ module GitStats class Repo include HashInitializable - attr_reader :path, :first_commit_sha, :last_commit_sha, :tree_path, :comment_string - delegate :files, :files_by_extension, :files_by_extension_count, :lines_by_extension, :files_count, :binary_files, :text_files, :lines_count, :comments_count, to: :last_commit @@ -17,6 +15,26 @@ module GitStats @tree_path ||= "." end + def path + @path ||= '.' + end + + def first_commit_sha + @first_commit_sha + end + + def last_commit_sha + @last_commit_sha ||= 'HEAD' + end + + def tree_path + @tree_path ||= '.' + end + + def comment_string + @comment_string ||= '//' + end + def authors @authors ||= run_and_parse("git shortlog -se #{commit_range} #{tree_path}").map do |author| Author.new(repo: self, name: author[:name], email: author[:email]) @@ -81,10 +99,6 @@ module GitStats @first_commit_sha.blank? ? last_commit_sha : "#@first_commit_sha..#{last_commit_sha}" end - def last_commit_sha - @last_commit_sha ||= 'HEAD' - end - def short_stats @short_stats ||= commits.map(&:short_stat) end @@ -106,7 +120,6 @@ module GitStats end def project_name - # @project_name ||= File.basename(path) @project_name ||= (File.expand_path(File.join(path, tree_path)).sub(File.dirname(File.expand_path(path))+File::SEPARATOR,"") || File.basename(path)) end diff --git a/lib/git_stats/git_data/tree.rb b/lib/git_stats/git_data/tree.rb index a0ad1786a..651345e70 100644 --- a/lib/git_stats/git_data/tree.rb +++ b/lib/git_stats/git_data/tree.rb @@ -5,10 +5,8 @@ module GitStats module GitData class Tree include HashInitializable + attr_reader :repo, :relative_path - def initialize(params) - super(params) - end def authors @authors ||= run_and_parse("git shortlog -se #{commit_range}").map do |author| diff --git a/spec/git_data/generator_spec.rb b/spec/git_data/generator_spec.rb index a04b97a4d..b2aba9c0c 100644 --- a/spec/git_data/generator_spec.rb +++ b/spec/git_data/generator_spec.rb @@ -4,7 +4,7 @@ require 'spec_helper' describe GitStats::Generator do let(:repo_path) { 'repo_path' } let(:out_path) { 'out_path' } - let(:generator) { GitStats::Generator.new(repo_path, out_path) } + let(:generator) { GitStats::Generator.new(path: repo_path, out_path: out_path) } before { Dir.stub(:exists? => true) } @@ -15,10 +15,8 @@ describe GitStats::Generator do it 'should pass command observer to repo' do repo = double('repo') - GitStats::GitData::Repo.should_receive(:new).with(path: repo_path, first_commit_sha: nil, last_commit_sha: "HEAD", tree_path: ".", :comment_string=>"///").and_return(repo) - - generator = GitStats::Generator.new(repo_path, out_path) - + GitStats::GitData::Repo.should_receive(:new).with(path: repo_path, out_path: out_path).and_return(repo) + observer = double('observer') repo.should_receive(:add_command_observer).with(observer) @@ -27,7 +25,7 @@ describe GitStats::Generator do it 'should render all templates with view data for this repo' do repo = double('repo') - GitStats::GitData::Repo.should_receive(:new).with(path: repo_path, first_commit_sha: nil, last_commit_sha: "HEAD", tree_path: ".", :comment_string=>"///").and_return(repo) + GitStats::GitData::Repo.should_receive(:new).with(path: repo_path, out_path: out_path).and_return(repo) view_data = double('view_data') GitStats::StatsView::ViewData.should_receive(:new).with(repo).and_return(view_data)