Class deprecated or moved

This class is deprecated or moved on the latest stable version. The last existing version (v5.2.3) is shown here.

Implements the logic behind the rake tasks for annotations like

rails notes
rails notes:optimize

and friends. See rails -T notes and railties/lib/rails/tasks/annotations.rake.

Annotation objects are triplets :line, :tag, :text that represent the line where the annotation lives, its tag, and its text. Note the filename is not stored.

Annotations are looked for in comments and modulus whitespace they have to start with the tag optionally followed by a colon. Everything up to the end of the line (or closing ERB comment tag) is considered to be their text.


Annotation = Struct.new(:line, :tag, :text) do def self.directories @@directories ||= %w(app config db lib test) + (ENV["SOURCE_ANNOTATION_DIRECTORIES"] || "").split(",") end # Registers additional directories to be included # SourceAnnotationExtractor::Annotation.register_directories("spec", "another") def self.register_directories(*dirs) directories.push(*dirs) end def self.extensions @@extensions ||= {} end # Registers new Annotations File Extensions # SourceAnnotationExtractor::Annotation.register_extensions("css", "scss", "sass", "less", "js") { |tag| /\/\/\s*(#{tag}):?\s*(.*)$/ } def self.register_extensions(*exts, &block) extensions[/\.(#{exts.join("|")})$/] = block end register_extensions("builder", "rb", "rake", "yml", "yaml", "ruby") { |tag| /#\s*(#{tag}):?\s*(.*)$/ } register_extensions("css", "js") { |tag| /\/\/\s*(#{tag}):?\s*(.*)$/ } register_extensions("erb") { |tag| /<%\s*#\s*(#{tag}):?\s*(.*?)\s*%>/ } # Returns a representation of the annotation that looks like this: # # [126] [TODO] This algorithm is simple and clearly correct, make it faster. # # If +options+ has a flag :tag the tag is shown as in the example above. # Otherwise the string contains just line and text. def to_s(options = {}) s = "[#{line.to_s.rjust(options[:indent])}] ".dup s << "[#{tag}] " if options[:tag] s << text end end


[R] tag
Show files where this class is defined (1 file)
Register or log in to add new notes.
July 23, 2008
5 thanks

Custom annotation types

For group work you may need something more than FIXME, OPTIMIZE and TODO. Just create new rake file and place it to lib/tasks:

require 'source_annotation_extractor'

task :notes do
  SourceAnnotationExtractor.enumerate "WTF|OMG", :tag => true

namespace :notes do
  desc "Enumerate all WTF annotations"
  task :wtf do
    SourceAnnotationExtractor.enumerate "WTF"

  desc "Enumerate all OMG annotations"
  task :omg do
    SourceAnnotationExtractor.enumerate "OMG"

or create an array of new types and generate tasks dynamicaly.

January 27, 2009
1 thank

Add Rspec files to the annotations

By default the annotations search the ‘test’ folder, but not the ‘spec’ folder if you are using Rspec. To get those specs involved do this:

require 'source_annotation_extractor'

class SourceAnnotationExtractor
  def find(dirs=%w(app lib spec))
    dirs.inject({}) { |h, dir| h.update(find_in(dir)) }

If you have other folders you want to check, just add them to the dirs list.