Class: Contributors

Inherits:
Object
  • Object
show all
Defined in:
lib/contributors.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = Dir.pwd) ⇒ Contributors

Returns a new instance of Contributors.



7
8
9
# File 'lib/contributors.rb', line 7

def initialize(path = Dir.pwd)
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/contributors.rb', line 6

def path
  @path
end

Instance Method Details

#authors_and_commitsObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/contributors.rb', line 15

def authors_and_commits
  hash = Hash.new { |hash, key| hash[key] = Array.new }
  repo.commits("master", false).reduce(hash) do |buffer, commit|
    key = buffer.keys.find { |author| author.email == commit.author.email }
    key ||= commit.author

    buffer[key] << commit
    buffer
  end
end

#list(criterium = :total) ⇒ Object

“additions”, “deletions”, “total”


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/contributors.rb', line 27

def list(criterium = :total)
  unless [:additions, :deletions, :total].include?(criterium)
    raise ArgumentError.new("Criterium can be one of: :additions, :deletions or :total.")
  end

  authors_and_commits.reduce(Hash.new) do |buffer, pair|
    author, commits = pair
    buffer[author] = {
      commits: commits.length,
      LOC: commits.reduce(0) do |lines, commit|
        lines + commit.stats.to_hash[criterium.to_s]
      end
    }

    buffer
  end
end

#repoObject



11
12
13
# File 'lib/contributors.rb', line 11

def repo
  @repo ||= Grit::Repo.new(@path)
end

#results(sort_by = :LOC, criterium = :total) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/contributors.rb', line 45

def results(sort_by = :LOC, criterium = :total)
  unless [:commits, :LOC].include?(sort_by)
    raise "Sort_by argument can be only :commits or :LOC!"
  end

  # Note: Hash#sort_by(&block) returns an Array, even on 1.9,
  # which isn't really necessary, as hashes on 1.9 are sorted.
  sorted_array = self.list(criterium).sort_by { |_, data| data[sort_by] }
  sorted_array.reverse.reduce(Hash.new) do |buffer, pair|
    author, stats = pair
    buffer.merge(author => stats)
  end
end