class Gpr::Commands::Contrib

Public Class Methods

new(thor) click to toggle source
# File lib/gpr/commands/contrib.rb, line 8
def initialize(thor)
  thor.class_eval do
    include UtilsDrawer
    include ::Gpr::Actions::Contrib

    desc 'contrib', 'Show your contributions of registered repositories'
    def contrib(path = nil)
      user = detect_git_user || ENV['USER']

      if path.nil?
        repositories = repository_list
        dates = repositories.each_with_object([]) { |repository, ary|
          ary << log_by_date(repository, user)
        }.flatten!
      else
        dates = log_by_date(path, user)
      end

      first_date = Time.parse(dates.sort.first)
      diff = ((Time.now - first_date).to_i / 86400) + 1

      # Initialize a hash
      commit_counts = diff.times.each_with_object({}) do |index, hash|
        date = Time.at(first_date + (86400 * index)).strftime('%Y-%m-%d')
        hash[date] = 0
      end

      # Count commits
      dates.each do |date|
        commit_counts[date] += 1 if commit_counts[date]
      end

      graph do
        commit_counts.sort.each do |date, value|
          data(date, value)
        end
      end
    end
  end
end

Public Instance Methods

contrib(path = nil) click to toggle source
# File lib/gpr/commands/contrib.rb, line 14
def contrib(path = nil)
  user = detect_git_user || ENV['USER']

  if path.nil?
    repositories = repository_list
    dates = repositories.each_with_object([]) { |repository, ary|
      ary << log_by_date(repository, user)
    }.flatten!
  else
    dates = log_by_date(path, user)
  end

  first_date = Time.parse(dates.sort.first)
  diff = ((Time.now - first_date).to_i / 86400) + 1

  # Initialize a hash
  commit_counts = diff.times.each_with_object({}) do |index, hash|
    date = Time.at(first_date + (86400 * index)).strftime('%Y-%m-%d')
    hash[date] = 0
  end

  # Count commits
  dates.each do |date|
    commit_counts[date] += 1 if commit_counts[date]
  end

  graph do
    commit_counts.sort.each do |date, value|
      data(date, value)
    end
  end
end