Class: Todonotes

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/todonotes.rb

Overview

Singleton definition for a fixme.

You can use Fixme#instance to set some global settings:

  • FiXme#log2file Define log file

  • FiXme#logger adapt level, outputter …

  • FiXme#codeline get Hash with counter per ToDo-locations.

  • FiXme#overview get overview text with ToDo-locations.

Constant Summary collapse

VERSION =
'0.1.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTodonotes

Define the singleton-instance.



67
68
69
70
71
72
73
74
75
76
# File 'lib/todonotes.rb', line 67

def initialize()
  # @codeline is a Hash with Filename and codeline (key). Value is the number of calls.
  @codeline = Hash.new(0)
  @logger = Log4r::Logger.new('ToDo')
  @logger.outputters = Log4r::StdoutOutputter.new('ToDo', 
                                  :level => Log4r::ALL,
                                  :formatter => Log4r::FixmeFormatter 
                                )
  #~ @logger.trace = true
end

Instance Attribute Details

#codelineObject (readonly)

Direct access to the codeline list. See also #todo_overview



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

def codeline
  @codeline
end

#loggerObject (readonly)

Get logger to define alternative outputters…



78
79
80
# File 'lib/todonotes.rb', line 78

def logger
  @logger
end

Class Method Details

See Todonotes#overview



156
157
158
# File 'lib/todonotes.rb', line 156

def print_stats()
  puts Todonotes.instance.overview()
end

Instance Method Details

#log2file(filename = File.basename($0) + '.todo', level = Log4r::ALL) ⇒ Object

Write the todo’s in a logging file.

Default filename is $0.todo



84
85
86
87
88
89
90
91
# File 'lib/todonotes.rb', line 84

def log2file(filename = File.basename($0) + '.todo', level = Log4r::ALL)
  @logger.add( Log4r::FileOutputter.new('ToDo', 
                                  :filename => filename,
                                  :level => level,
                                  :formatter => Log4r::FixmeFormatter 
                                ))
  
end

#log_todo(key, type, text, res) ⇒ Object

Report the ToDo/FixMe and count occurence.

The first occurence is reported as a warning, next occurences are informations.



117
118
119
120
121
122
123
124
125
126
# File 'lib/todonotes.rb', line 117

def log_todo( key, type, text, res )

  @codeline[key] += 1
  if @codeline[key] == 1 #First occurence?
    @logger.warn([type, "#{key} #{text} (temporary: #{res.inspect})"])
  else  #Erste auftauchen
    @logger.info([type, "#{key}(#{@codeline[key]}) #{text} (temporary: #{res.inspect})"])
  end
  
end

#overviewObject

Return a text to be printed

puts Todonotes.instance.todo_overview()

Used from Todonotes.print_stats

Example:

List of ToDos/FixMes:
fixme.rb:195:    1 call
fixme.rb:198:    2 calls
fixme.rb:199:    1 call
fixme.rb:200:    1 call


140
141
142
143
144
145
146
147
# File 'lib/todonotes.rb', line 140

def overview( )
  txt = []
  txt << "List of ToDos/FixMes:"
  @codeline.each do |key, messages|
    txt << "%s: %4i call%s" % [ key, messages, messages > 1 ? 's': ''  ]
  end
  txt.join("\n")
end

#todo(comment, type = :ToDo, &block) ⇒ Object

Report a FixMe or a ToDo.

The comment is logged, the block is evaluated to get a temporary result.



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

def todo( comment, type = :ToDo, &block)
  res = nil
  key = caller[1].split(':in').first
  if block_given?
    res = yield self
    res
  end
  log_todo(key, type, comment, res)
  #~ @logger.debug("Return #{res.inspect} instead") if @logger.debug?
  res
end