Class: Xmvc::Stats::Statistics

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

Instance Method Summary collapse

Instance Method Details

#column_headingsObject

Mappings between method and human names for column headings



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/xmvc/stats.rb', line 159

def column_headings
  {
    :line_count      => "Lines",
    :loc_count       => "LOC",
    :file_count      => "Classes",
    :method_count    => "Methods",
    :average_methods => "M/C",
    :method_length   => "LOC/M",
    :class_length    => "LOC/C"
  }
end

#column_orderObject

The statistics columns to show, in order



125
126
127
# File 'lib/xmvc/stats.rb', line 125

def column_order
  [:line_count, :loc_count, :file_count, :class_length]
end

#column_widthObject

Calculates the width of all columns but the first. Elements will be padded to this width



120
121
122
# File 'lib/xmvc/stats.rb', line 120

def column_width
  column_headings.values.collect {|l| l.to_s.length}.max + 2
end

#controller_filesObject



171
172
173
# File 'lib/xmvc/stats.rb', line 171

def controller_files
  files_in('app/controllers') + ['app/App.js']
end

#controller_specsObject



191
192
193
# File 'lib/xmvc/stats.rb', line 191

def controller_specs
  files_in('spec/controllers')
end

#lib_filesObject



183
184
185
# File 'lib/xmvc/stats.rb', line 183

def lib_files
  files_in('lib')
end

#line_headingsObject

Mappings between method and human names for line headings



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/xmvc/stats.rb', line 145

def line_headings
  {
    :controller_files => "Controllers",
    :model_files      => "Models",
    :view_files       => "Views",
    :lib_files        => "Libraries",
    :plugin_files     => "Plugins",
    :controller_specs => "Controller Specs",
    :model_specs      => "Model Specs",
    :view_specs       => "View Specs"
  }
end

#line_orderObject

The lines to show, in order



130
131
132
# File 'lib/xmvc/stats.rb', line 130

def line_order
  [:controller_files, :model_files, :view_files, :lib_files, :controller_specs, :model_specs, :view_specs]
end

#model_filesObject



175
176
177
# File 'lib/xmvc/stats.rb', line 175

def model_files
  files_in('app/models')
end

#model_specsObject



195
196
197
# File 'lib/xmvc/stats.rb', line 195

def model_specs
  files_in('spec/models')        
end

#outputObject

Prints statistics to the console



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

def output
  print_headings

  line_order.each do |line_name|
    stats = statistics[line_name]
  
    arr = [line_headings[line_name]] + column_order.collect {|col| stats[col]}
    print_line(arr)
  end

  print_separator
  print_summary
end

#plugin_filesObject



187
188
189
# File 'lib/xmvc/stats.rb', line 187

def plugin_files
  files_in('vendor/plugins')
end

Prints a headings line based on column_order



61
62
63
64
65
66
67
68
69
70
# File 'lib/xmvc/stats.rb', line 61

def print_headings
  puts
  print_separator

  columns = ["Name"];
  column_order.each {|heading| columns.push(column_headings[heading])}
  print_line(columns)

  print_separator
end

Prints an array of string as a line



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/xmvc/stats.rb', line 100

def print_line(line_elements)
  str = "| "

  line_elements.each_with_index do |element, index|
    if index == 0
      str += element.to_s.ljust(title_width)  + " | "
    else
      str += element.to_s.rjust(column_width) + " | "
    end
  end

  puts str
end

Prints a separator line



89
90
91
92
93
94
95
96
97
# File 'lib/xmvc/stats.rb', line 89

def print_separator
  str = "+" + "-" * (title_width + 2)

  column_order.length.times do
    str += "+" + "-" * (column_width + 2)
  end

  puts str + '+'
end

Prints a summary line with total LOC



80
81
82
83
84
85
86
# File 'lib/xmvc/stats.rb', line 80

def print_summary
  stats = [statistics[:total_code_loc], statistics[:total_spec_loc]]
  stats.push(stats[1].to_f / stats[0].to_f)

  puts "  Code LOC: %s     Test LOC: %s     Code to Test Ratio: 1:%1.1f" % stats
  puts
end


72
73
74
75
76
77
# File 'lib/xmvc/stats.rb', line 72

def print_totals
  # columns = ["Totals"]
  # column_order.collect {|c| statistics}

  # print_line(columns)
end

#project_code_arraysObject

The arrays to use when calculating totals for project LOC etc



135
136
137
# File 'lib/xmvc/stats.rb', line 135

def project_code_arrays
  [:controller_files, :model_files, :view_files, :lib_files]
end

#project_spec_arraysObject

The arrays to use when calculating totals for project spec LOC etc



140
141
142
# File 'lib/xmvc/stats.rb', line 140

def project_spec_arrays
  [:controller_specs, :model_specs, :view_specs]
end

#statisticsObject

Gathers and aggregates all project statistics



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

def statistics
  @statistics ||= begin
    stats = {}

    # Calculate requested stats
    line_order.each do |line|
      stats[line] = array_statistics(self.send(line))
    end

    # Calculate totals
    stats[:total_code_loc] = project_code_arrays.inject(0) {|sum, e| sum + stats[e][:loc_count]}
    stats[:total_spec_loc] = project_spec_arrays.inject(0) {|sum, e| sum + stats[e][:loc_count]}
  
    stats
  end
end

#title_widthObject

Calculates the width of the first column. Elements will be padded to this width



115
116
117
# File 'lib/xmvc/stats.rb', line 115

def title_width
  line_headings.values.collect {|l| l.to_s.length}.max + 8
end

#view_filesObject



179
180
181
# File 'lib/xmvc/stats.rb', line 179

def view_files
  files_in('app/views')
end

#view_specsObject



199
200
201
# File 'lib/xmvc/stats.rb', line 199

def view_specs
  files_in('spec/views')
end