Class: RDoc::Stats

Inherits:
Object
  • Object
show all
Includes:
Text
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

Constant Summary

Constants included from Text

Text::MARKUP_FORMAT, Text::TO_HTML_CHARACTERS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Text

encode_fallback, #expand_tabs, #flush_left, #markup, #normalize_comment, #parse, #snippet, #strip_hashes, #strip_newlines, #strip_stars, #to_html, #wrap

Constructor Details

#initialize(store, num_files, verbosity = 1) ⇒ Stats

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
46
# File 'lib/rdoc/stats.rb', line 28

def initialize store, num_files, verbosity = 1
  @num_files = num_files
  @store     = store

  @coverage_level   = 0
  @doc_items        = nil
  @files_so_far     = 0
  @fully_documented = false
  @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

#coverage_levelObject

Output level for the coverage report



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

def coverage_level
  @coverage_level
end

#files_so_farObject (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

#num_filesObject (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

#add_alias(as) ⇒ Object

Records the parsing of an alias as.



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

def add_alias as
  @display.print_alias as
end

#add_attribute(attribute) ⇒ Object

Records the parsing of an attribute attribute



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

def add_attribute attribute
  @display.print_attribute attribute
end

#add_class(klass) ⇒ Object

Records the parsing of a class klass



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

def add_class klass
  @display.print_class klass
end

#add_constant(constant) ⇒ Object

Records the parsing of constant



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

def add_constant constant
  @display.print_constant constant
end

#add_file(file) ⇒ Object

Records the parsing of file



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

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

#add_method(method) ⇒ Object

Records the parsing of method



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

def add_method(method)
  @display.print_method method
end

#add_module(mod) ⇒ Object

Records the parsing of a module mod



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

def add_module(mod)
  @display.print_module mod
end

#begin_addingObject

Call this to mark the beginning of parsing for display purposes



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

def begin_adding
  @display.begin_adding
end

#calculateObject

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



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
145
146
147
148
# File 'lib/rdoc/stats.rb', line 109

def calculate
  return if @doc_items

  ucm = @store.unique_classes_and_modules

  classes = @store.unique_classes.reject { |cm| cm.full_name == 'Object' }

  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 classes
  @num_constants,  @undoc_constants  = doc_stats constants
  @num_methods,    @undoc_methods    = doc_stats methods
  @num_modules,    @undoc_modules    = doc_stats @store.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

#doc_stats(collection) ⇒ Object

Returns the length and number of undocumented items in collection.



166
167
168
169
# File 'lib/rdoc/stats.rb', line 166

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

#done_addingObject

Call this to mark the end of parsing for display purposes



174
175
176
# File 'lib/rdoc/stats.rb', line 174

def done_adding
  @display.done_adding
end

#fully_documented?Boolean

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

Set by calling #calculate

Returns:

  • (Boolean)


184
185
186
# File 'lib/rdoc/stats.rb', line 184

def fully_documented?
  @fully_documented
end

#great_jobObject

A report that says you did a great job!



191
192
193
194
195
196
197
198
# File 'lib/rdoc/stats.rb', line 191

def great_job
  report = RDoc::Markup::Document.new

  report << RDoc::Markup::Paragraph.new('100% documentation!')
  report << RDoc::Markup::Paragraph.new('Great Job!')

  report
end

#percent_docObject

Calculates the percentage of items documented.



203
204
205
206
207
208
209
210
211
212
# File 'lib/rdoc/stats.rb', line 203

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

#reportObject

Returns a report on which items are not documented



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
251
252
253
# File 'lib/rdoc/stats.rb', line 217

def report
  if @coverage_level > 0 then
    extend RDoc::Text
  end

  if @coverage_level.zero? then
    calculate

    return great_job if @num_items == @doc_items
  end

  ucm = @store.unique_classes_and_modules

  report = RDoc::Markup::Document.new
  report << RDoc::Markup::Paragraph.new('The following items are not documented:')
  report << RDoc::Markup::BlankLine.new

  ucm.sort.each do |cm|
    body = report_class_module(cm) {
      [
        report_constants(cm),
        report_attributes(cm),
        report_methods(cm),
      ].compact
    }

    report << body if body
  end

  if @coverage_level > 0 then
    calculate

    return great_job if @num_items == @doc_items
  end

  report
end

#report_attributes(cm) ⇒ Object

Returns a report on undocumented attributes in ClassModule cm



258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/rdoc/stats.rb', line 258

def report_attributes cm
  return if cm.attributes.empty?

  report = []

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

  report
end

#report_class_module(cm) ⇒ Object

Returns a report on undocumented items in ClassModule cm



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
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/rdoc/stats.rb', line 276

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

  report = RDoc::Markup::Document.new

  if cm.in_files.empty? then
    report << RDoc::Markup::Paragraph.new("#{cm.definition} is referenced but empty.")
    report << RDoc::Markup::Paragraph.new("It probably came from another project.  I'm sorry I'm holding it against you.")

    return report
  elsif cm.documented? then
    documented = true
    klass = RDoc::Markup::Verbatim.new("#{cm.definition} # is documented\n")
  else
    report << RDoc::Markup::Paragraph.new('In files:')

    list = RDoc::Markup::List.new :BULLET

    cm.in_files.each do |file|
      para = RDoc::Markup::Paragraph.new file.full_name
      list << RDoc::Markup::ListItem.new(nil, para)
    end

    report << list
    report << RDoc::Markup::BlankLine.new

    klass = RDoc::Markup::Verbatim.new("#{cm.definition}\n")
  end

  klass << "\n"

  body = yield.flatten # HACK remove #flatten

  if body.empty? then
    return if documented

    klass.parts.pop
  else
    klass.parts.concat body
  end

  klass << "end\n"

  report << klass

  report
end

#report_constants(cm) ⇒ Object

Returns a report on undocumented constants in ClassModule cm



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/rdoc/stats.rb', line 328

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

    line = constant.line ? ":#{constant.line}" : line
    report << "  # in file #{constant.file.full_name}#{line}\n"
    report << "  #{constant.name} = nil\n"
    report << "\n"
  end

  report
end

#report_methods(cm) ⇒ Object

Returns a report on undocumented methods in ClassModule cm



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/rdoc/stats.rb', line 350

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\n"
      end
    end

    next if method.documented? and not param_report

    line = method.line ? ":#{method.line}" : nil
    scope = method.singleton ? 'self.' : nil

    report << "  # in file #{method.file.full_name}#{line}\n"
    report << param_report if param_report
    report << "  def #{scope}#{method.name}#{method.params}; end\n"
    report << "\n"
  end

  report
end

#summaryObject

Returns a summary of the collected statistics.



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/rdoc/stats.rb', line 388

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 = RDoc::Markup::Verbatim.new

  report << "Files:      %*d\n" % [num_width, @num_files]

  report << "\n"

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

  report << "\n"

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

  report << "%6.2f%% documented\n" % percent_doc
  report << "\n"
  report << "Elapsed: %0.1fs\n" % (Time.now - @start)

  RDoc::Markup::Document.new report
end

#undoc_params(method) ⇒ Object

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



438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'lib/rdoc/stats.rb', line 438

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

  params = method.param_list

  params = params.map { |param| param.gsub(/^\*\*?/, '') }

  return 0, [] if params.empty?

  document = parse method.comment

  tts = document.accept @formatter

  undoc = params - tts

  [params.length, undoc]
end