Class: Twigg::PairMatrix

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/twigg/pair_matrix.rb

Overview

A PairMatrix is initialized with a CommitSet instance and computes pairing information for those commits.

Instance Method Summary collapse

Constructor Details

#initialize(commit_set) ⇒ PairMatrix

Returns a new instance of PairMatrix.



10
11
12
# File 'lib/twigg/pair_matrix.rb', line 10

def initialize(commit_set)
  @commit_set = commit_set
end

Instance Method Details

#authorsObject

Returns a sorted array of names corresponding to the authors represented in the matrix.



44
45
46
# File 'lib/twigg/pair_matrix.rb', line 44

def authors
  @authors ||= pairs.keys.sort
end

#max_pairObject

Scan the matrix, identifying and returning the “pair” element (ie. two distinct people pairing) with the highest number of commits.



60
61
62
63
64
65
66
# File 'lib/twigg/pair_matrix.rb', line 60

def max_pair
  @max_pair ||= pairs.inject(0) do |max, (pairee, pairs)|
    [pairs.inject(0) do |max, (pairer, count)|
      [pairee == pairer ? 0 : count, max].max
    end, max].max
  end
end

#max_soloObject

Scan the matrix, identifying and returning the “solo” element (ie. one person working alone) with the highest number of commits.



50
51
52
53
54
55
56
# File 'lib/twigg/pair_matrix.rb', line 50

def max_solo
  @max_solo ||= pairs.inject(0) do |max, (pairee, pairs)|
    [pairs.inject(0) do |max, (pairer, count)|
      [pairee == pairer ? count : 0, max].max
    end, max].max
  end
end

#pairsObject

Returns a sparse matrix representing the pairing permutations, and commit counts for each, in the receiver.

The returned matrix is a Hash data structure and can be queried like so:

pm['Joe Lencioni']['Noah Silas']   #=> 3 (commit count by the pair)
pm['Tony Wooster']['Tony Wooster'] #=> 9 (commit count as solo author)
pm['Joe Lencioni']['Tony Wooster'] #=> 0 (no commits, no pairing)

Note that the #[] method is forwarded to the underlying Hash, which means that the above examples work equally well whether ‘pm` is an instance of a Twigg::PairMatrix or the result of a call to the the #pairs method on a Twigg::PairMatrix instance.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/twigg/pair_matrix.rb', line 27

def pairs
  @pairs ||= sparse_matrix.tap do |matrix|
    @commit_set.each do |commit|
      authors = commit.author_names

      # if you're solo, that's equivalent to pairing with yourself
      authors *= 2 if authors.size == 1

      authors.permutation(2).to_a.uniq.each do |pairer, pairee|
        matrix[pairer][pairee] += 1
      end
    end
  end
end