Class: Contributors
- Inherits:
-
Object
- Object
- Contributors
- Defined in:
- lib/contributors.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #authors_and_commits ⇒ Object
-
#initialize(path = Dir.pwd) ⇒ Contributors
constructor
A new instance of Contributors.
-
#list(criterium = :total) ⇒ Object
[“additions”, “deletions”, “total”].
- #repo ⇒ Object
- #results(sort_by = :LOC, criterium = :total) ⇒ Object
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
#path ⇒ Object (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_commits ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/contributors.rb', line 15 def hash = Hash.new { |hash, key| hash[key] = Array.new } repo.commits("master", false).reduce(hash) do |buffer, commit| key = buffer.keys.find { || .email == commit..email } key ||= commit. 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 .reduce(Hash.new) do |buffer, pair| , commits = pair buffer[] = { commits: commits.length, LOC: commits.reduce(0) do |lines, commit| lines + commit.stats.to_hash[criterium.to_s] end } buffer end end |
#repo ⇒ Object
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| , stats = pair buffer.merge( => stats) end end |