Class: LOCat::Counter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(matcher, options) ⇒ Counter

Returns a new instance of Counter.



7
8
9
10
11
12
13
# File 'lib/locat/counter.rb', line 7

def initialize(matcher, options)
  @matcher = matcher

  options.each do |k,v|
    send("#{k}=", v)
  end
end

Instance Attribute Details

#filesObject

Returns the value of attribute files.



19
20
21
# File 'lib/locat/counter.rb', line 19

def files
  @files
end

#matcherObject (readonly)

Returns the value of attribute matcher.



16
17
18
# File 'lib/locat/counter.rb', line 16

def matcher
  @matcher
end

Instance Method Details

#countsObject



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

def counts
  @counts ||= (
    table = Hash.new{ |h,k| h[k] = 0 }
    matcher.each do |pattern, block|
      files = resolve_files(pattern)
      files.each do |file|
        File.readlines(file).each do |line|
          line = line.chomp("\n")
          group = block.call(file, line)
          if group
            table[group.to_s] += 1
          end
        end
      end
    end
    table
  )
end

#gitlocObject



113
114
115
# File 'lib/locat/counter.rb', line 113

def gitloc
  @gitloc ||= GitLOC.new(matcher)
end

#locObject



52
53
54
55
56
57
58
59
# File 'lib/locat/counter.rb', line 52

def loc
  @loc ||= (
    a = []
    a << [nil  , *counts.keys]
    a << ['loc', *counts.values]
    a
  )
end

#percentObject

Calculate the percentages.



88
89
90
91
92
93
94
95
# File 'lib/locat/counter.rb', line 88

def percent
  @percent ||= (
    a = []
    a << [nil  ,  *counts.keys]
    a << ['%', *counts.values.map{ |f| 100 * f / total }]
    a
  )
end

#ratioObject

Compute the ratios between each group, returning a two-dimensional array.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/locat/counter.rb', line 63

def ratio
  @ratio ||= (
    rcounts = counts.dup
    rcounts['Total'] = total
    r = Array.new(rcounts.size+1){ [] }
    x = 1
    rcounts.each do |type1, count1|
      r[x][0] = type1
      y = 1
      rcounts.each do |type2, count2|
        r[0][y] = type2
        if count2 == 0 or count2 == 0
          r[x][y] = 0
        else
          r[x][y] = (1000 * (count2.to_f / count1.to_f)).round.to_f / 1000.0
        end
        y += 1
      end
      x += 1
    end
    r
  )
end

#scmObject



123
124
125
126
127
128
129
130
131
# File 'lib/locat/counter.rb', line 123

def scm
  return nil unless scm?
  begin
    gitloc.timeline_table
  rescue
    warn "Can't generate git timeline. Skipping this portion."
    nil
  end
end

#scm?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/locat/counter.rb', line 118

def scm?
  File.directory?('.git')
end

#to_hObject



98
99
100
101
102
103
104
105
# File 'lib/locat/counter.rb', line 98

def to_h
  h = {}
  h['loc']   = loc
  h['pcnt']  = percent
  h['ratio'] = ratio
  h['scm']   = scm if scm?
  h
end

#totalObject



47
48
49
# File 'lib/locat/counter.rb', line 47

def total
  @total = counts.values.inject(0){ |s,v| s+=v; s }
end