Class: RDoc::Stats

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

Overview

RDoc statistics collector which prints a summary and report of a project's documentation totals.

Defined Under Namespace

Classes: Normal, Quiet, Verbose

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Stats) initialize(num_files, verbosity = 1)

Creates a new Stats that will have num_files. verbosity defaults to 1 which will create an RDoc::Stats::Normal outputter.



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

def initialize num_files, verbosity = 1
  @files_so_far = 0
  @num_files = num_files

  @coverage_level = 0
  @doc_items = nil
  @fully_documented = nil
  @num_params = 0
  @percent_doc = nil
  @start = Time.now
  @undoc_params = 0

  @display = case verbosity
             when 0 then Quiet.new   num_files
             when 1 then Normal.new  num_files
             else        Verbose.new num_files
             end
end

Instance Attribute Details

- (Object) coverage_level

Output level for the coverage report



12
13
14
# File 'lib/rdoc/stats.rb', line 12

def coverage_level
  @coverage_level
end

- (Object) files_so_far (readonly)

Count of files parsed during parsing



17
18
19
# File 'lib/rdoc/stats.rb', line 17

def files_so_far
  @files_so_far
end

- (Object) num_files (readonly)

Total number of files found



22
23
24
# File 'lib/rdoc/stats.rb', line 22

def num_files
  @num_files
end

Instance Method Details

- (Object) add_alias(as)

Records the parsing of an alias as.



50
51
52
# File 'lib/rdoc/stats.rb', line 50

def add_alias as
  @display.print_alias as
end

- (Object) add_attribute(attribute)

Records the parsing of an attribute attribute



57
58
59
# File 'lib/rdoc/stats.rb', line 57

def add_attribute attribute
  @display.print_attribute attribute
end

- (Object) add_class(klass)

Records the parsing of a class klass



64
65
66
# File 'lib/rdoc/stats.rb', line 64

def add_class klass
  @display.print_class klass
end

- (Object) add_constant(constant)

Records the parsing of constant



71
72
73
# File 'lib/rdoc/stats.rb', line 71

def add_constant constant
  @display.print_constant constant
end

- (Object) add_file(file)

Records the parsing of file



78
79
80
81
# File 'lib/rdoc/stats.rb', line 78

def add_file(file)
  @files_so_far += 1
  @display.print_file @files_so_far, file
end

- (Object) add_method(method)

Records the parsing of method



86
87
88
# File 'lib/rdoc/stats.rb', line 86

def add_method(method)
  @display.print_method method
end

- (Object) add_module(mod)

Records the parsing of a module mod



93
94
95
# File 'lib/rdoc/stats.rb', line 93

def add_module(mod)
  @display.print_module mod
end

- (Object) begin_adding

Call this to mark the beginning of parsing for display purposes



100
101
102
# File 'lib/rdoc/stats.rb', line 100

def begin_adding
  @display.begin_adding
end

- (Object) calculate

Calculates documentation totals and percentages for classes, modules, constants, attributes and methods.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/rdoc/stats.rb', line 108

def calculate
  return if @doc_items

  ucm = RDoc::TopLevel.unique_classes_and_modules
  constants = []
  ucm.each { |cm| constants.concat cm.constants }

  methods = []
  ucm.each { |cm| methods.concat cm.method_list }

  attributes = []
  ucm.each { |cm| attributes.concat cm.attributes }

  @num_attributes, @undoc_attributes = doc_stats attributes
  @num_classes,    @undoc_classes    = doc_stats RDoc::TopLevel.unique_classes
  @num_constants,  @undoc_constants  = doc_stats constants
  @num_methods,    @undoc_methods    = doc_stats methods
  @num_modules,    @undoc_modules    = doc_stats RDoc::TopLevel.unique_modules

  @num_items =
    @num_attributes +
    @num_classes +
    @num_constants +
    @num_methods +
    @num_modules +
    @num_params

  @undoc_items =
    @undoc_attributes +
    @undoc_classes +
    @undoc_constants +
    @undoc_methods +
    @undoc_modules +
    @undoc_params

  @doc_items = @num_items - @undoc_items
end

- (Object) doc_stats(collection)

Returns the length and number of undocumented items in collection.



162
163
164
# File 'lib/rdoc/stats.rb', line 162

def doc_stats collection
  [collection.length, collection.count { |item| not item.documented? }]
end

- (Object) done_adding

Call this to mark the end of parsing for display purposes



169
170
171
# File 'lib/rdoc/stats.rb', line 169

def done_adding
  @display.done_adding
end

- (Boolean) fully_documented?

The documentation status of this project. true when 100%, false when less than 100% and nil when unknown.

Set by calling #calculate

Returns:

  • (Boolean)


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

def fully_documented?
  @fully_documented
end

- (Object) great_job

A report that says you did a great job!



186
187
188
189
190
191
192
193
# File 'lib/rdoc/stats.rb', line 186

def great_job
  report = []
  report << '100% documentation!'
  report << nil
  report << 'Great Job!'

  report.join "\n"
end

- (Object) percent_doc

Calculates the percentage of items documented.



198
199
200
201
202
203
204
205
206
207
# File 'lib/rdoc/stats.rb', line 198

def percent_doc
  return @percent_doc if @percent_doc

  @fully_documented = (@num_items - @doc_items) == 0

  @percent_doc = @doc_items.to_f / @num_items * 100 if @num_items.nonzero?
  @percent_doc ||= 0

  @percent_doc
end

- (Object) report

Returns a report on which items are not documented



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/rdoc/stats.rb', line 212

def report
  if @coverage_level > 0 then
    require 'rdoc/markup/to_tt_only'
    require 'rdoc/generator/markup'
    require 'rdoc/text'
    extend RDoc::Text
  end

  report = []

  if @coverage_level.zero? then
    calculate

    return great_job if @num_items == @doc_items
  end

  ucm = RDoc::TopLevel.unique_classes_and_modules

  ucm.sort.each do |cm|
    report << report_class_module(cm) {
      [
        report_constants(cm),
        report_attributes(cm),
        report_methods(cm),
      ].compact
    }
  end

  if @coverage_level > 0 then
    calculate

    return great_job if @num_items == @doc_items
  end

  report.unshift nil
  report.unshift 'The following items are not documented:'

  report.join "\n"
end

- (Object) report_attributes(cm)

Returns a report on undocumented attributes in ClassModule cm



255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/rdoc/stats.rb', line 255

def report_attributes cm
  return if cm.attributes.empty?

  report = []

  cm.each_attribute do |attr|
    next if attr.documented?
    report << "  #{attr.definition} :#{attr.name} " \
      "# in file #{attr.file.full_name}"
  end

  report
end

- (Object) report_class_module(cm)

Returns a report on undocumented items in ClassModule cm



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/rdoc/stats.rb', line 272

def report_class_module cm
  return if cm.fully_documented? and @coverage_level.zero?

  report = []

  if cm.in_files.empty? then
    report << "# #{cm.definition} is referenced but empty."
    report << '#'
    report << '# It probably came from another project.  ' \
      "I'm sorry I'm holding it against you."
    report << nil

    return report
  elsif cm.documented? then
    documented = true
    report << "#{cm.definition} # is documented"
  else
    report << '# in files:'

    cm.in_files.each do |file|
      report << "#   #{file.full_name}"
    end

    report << nil

    report << "#{cm.definition}"
  end

  body = yield.flatten # HACK remove #flatten

  return if body.empty? and documented

  report << nil << body unless body.empty?

  report << 'end'
  report << nil

  report
end

- (Object) report_constants(cm)

Returns a report on undocumented constants in ClassModule cm



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/rdoc/stats.rb', line 315

def report_constants cm
  return if cm.constants.empty?

  report = []

  cm.each_constant do |constant|
    # TODO constant aliases are listed in the summary but not reported
    # figure out what to do here
    next if constant.documented? || constant.is_alias_for
    report << "  # in file #{constant.file.full_name}"
    report << "  #{constant.name} = nil"
  end

  report
end

- (Object) report_methods(cm)

Returns a report on undocumented methods in ClassModule cm



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/rdoc/stats.rb', line 334

def report_methods cm
  return if cm.method_list.empty?

  report = []

  cm.each_method do |method|
    next if method.documented? and @coverage_level.zero?

    if @coverage_level > 0 then
      params, undoc = undoc_params method

      @num_params += params

      unless undoc.empty? then
        @undoc_params += undoc.length

        undoc = undoc.map do |param| "+#{param}+" end
        param_report = "  # #{undoc.join ', '} is not documented"
      end
    end

    next if method.documented? and not param_report
    report << "  # in file #{method.file.full_name}"
    report << param_report if param_report
    report << "  def #{method.name}#{method.params}; end"
    report << nil
  end

  report
end

- (Object) summary

Returns a summary of the collected statistics.



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'lib/rdoc/stats.rb', line 368

def summary
  calculate

  num_width = [@num_files, @num_items].max.to_s.length
  undoc_width = [
    @undoc_attributes,
    @undoc_classes,
    @undoc_constants,
    @undoc_items,
    @undoc_methods,
    @undoc_modules,
    @undoc_params,
  ].max.to_s.length

  report = []
  report << 'Files:      %*d' % [num_width, @num_files]

  report << nil

  report << 'Classes:    %*d (%*d undocumented)' % [
    num_width, @num_classes, undoc_width, @undoc_classes]
  report << 'Modules:    %*d (%*d undocumented)' % [
    num_width, @num_modules, undoc_width, @undoc_modules]
  report << 'Constants:  %*d (%*d undocumented)' % [
    num_width, @num_constants, undoc_width, @undoc_constants]
  report << 'Attributes: %*d (%*d undocumented)' % [
    num_width, @num_attributes, undoc_width, @undoc_attributes]
  report << 'Methods:    %*d (%*d undocumented)' % [
    num_width, @num_methods, undoc_width, @undoc_methods]
  report << 'Parameters: %*d (%*d undocumented)' % [
    num_width, @num_params, undoc_width, @undoc_params] if
      @coverage_level > 0

  report << nil

  report << 'Total:      %*d (%*d undocumented)' % [
    num_width, @num_items, undoc_width, @undoc_items]

  report << '%6.2f%% documented' % percent_doc
  report << nil
  report << 'Elapsed: %0.1fs' % (Time.now - @start)

  report.join "\n"
end

- (Object) undoc_params(method)

Determines which parameters in method were not documented. Returns a total parameter count and an Array of undocumented methods.



417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/rdoc/stats.rb', line 417

def undoc_params method
  @formatter ||= RDoc::Markup::ToTtOnly.new

  params = method.param_list

  return 0, [] if params.empty?

  document = parse method.comment

  tts = document.accept @formatter

  undoc = params - tts

  [params.length, undoc]
end