| Class | SourceAnnotationExtractor |
| In: |
vendor/rails/railties/lib/source_annotation_extractor.rb
|
| Parent: | Object |
Implements the logic behind the rake tasks for annotations like
rake notes rake notes:optimize
and friends. See rake -T notes and railties/lib/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.
| tag | [R] |
Prints all annotations with tag tag under the root directories app, lib, and test (recursively). Only filenames with extension +.builder+, +.rb+, +.rxml+, +.rjs+, +.rhtml+, or +.erb+ are taken into account. The options hash is passed to each annotation‘s to_s.
This class method is the single entry point for the rake tasks.
# File vendor/rails/railties/lib/source_annotation_extractor.rb, line 37
37: def self.enumerate(tag, options={})
38: extractor = new(tag)
39: extractor.display(extractor.find, options)
40: end
# File vendor/rails/railties/lib/source_annotation_extractor.rb, line 44
44: def initialize(tag)
45: @tag = tag
46: end
Prints the mapping from filenames to annotations in results ordered by filename. The options hash is passed to each annotation‘s to_s.
# File vendor/rails/railties/lib/source_annotation_extractor.rb, line 93
93: def display(results, options={})
94: results.keys.sort.each do |file|
95: puts "#{file}:"
96: results[file].each do |note|
97: puts " * #{note.to_s(options)}"
98: end
99: puts
100: end
101: end
If file is the filename of a file that contains annotations this method returns a hash with a single entry that maps file to an array of its annotations. Otherwise it returns an empty hash.
# File vendor/rails/railties/lib/source_annotation_extractor.rb, line 81
81: def extract_annotations_from(file, pattern)
82: lineno = 0
83: result = File.readlines(file).inject([]) do |list, line|
84: lineno += 1
85: next list unless line =~ pattern
86: list << Annotation.new(lineno, $1, $2)
87: end
88: result.empty? ? {} : { file => result }
89: end
Returns a hash that maps filenames under dirs (recursively) to arrays with their annotations. Only files with annotations are included, and only those with extension +.builder+, +.rb+, +.rxml+, +.rjs+, +.rhtml+, and +.erb+ are taken into account.
# File vendor/rails/railties/lib/source_annotation_extractor.rb, line 52
52: def find(dirs=%w(app lib test))
53: dirs.inject({}) { |h, dir| h.update(find_in(dir)) }
54: end
Returns a hash that maps filenames under dir (recursively) to arrays with their annotations. Only files with annotations are included, and only those with extension +.builder+, +.rb+, +.rxml+, +.rjs+, +.rhtml+, and +.erb+ are taken into account.
# File vendor/rails/railties/lib/source_annotation_extractor.rb, line 60
60: def find_in(dir)
61: results = {}
62:
63: Dir.glob("#{dir}/*") do |item|
64: next if File.basename(item)[0] == ?.
65:
66: if File.directory?(item)
67: results.update(find_in(item))
68: elsif item =~ /\.(builder|(r(?:b|xml|js)))$/
69: results.update(extract_annotations_from(item, /#\s*(#{tag}):?\s*(.*)$/))
70: elsif item =~ /\.(rhtml|erb)$/
71: results.update(extract_annotations_from(item, /<%\s*#\s*(#{tag}):?\s*(.*?)\s*%>/))
72: end
73: end
74:
75: results
76: end