Class: Countless::Annotations
- Inherits:
-
Object
- Object
- Countless::Annotations
- Defined in:
- lib/countless/annotations.rb
Overview
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.
Heavily stolen from: bit.ly/3nBS0aj
rubocop:disable Metrics/ClassLength – because of the nested Annotation
class
Defined Under Namespace
Classes: Annotation
Instance Attribute Summary collapse
-
#annotations ⇒ Object
readonly
Returns the value of attribute annotations.
-
#dirs ⇒ Object
readonly
Returns the value of attribute dirs.
-
#files ⇒ Object
readonly
Returns the value of attribute files.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#tag ⇒ Object
readonly
Returns the value of attribute tag.
Instance Method Summary collapse
-
#annotations_in(file) ⇒ Hash{String => Array<Annotation>}
Returns a hash that maps filenames under
file
(de-glob-bed) to arrays with their annotations. -
#extract_annotations_from(file, pattern) ⇒ Hash{String => Annotation}
If
file
is the filename of a file that contains annotations this method returns a hash with a single entry that mapsfile
to an array of its annotations. -
#find(files: [], dirs: []) ⇒ Hash{String => Array<Annotation>}
Returns a hash that maps filenames under
dirs
(recursively) to arrays with their annotations. -
#find_in(dir) ⇒ Hash{String => Array<Annotation>}
Returns a hash that maps filenames under
dir
(recursively) to arrays with their annotations. -
#initialize(tag = nil, options = {}) ⇒ Countless::SourceAnnotationExtractor
constructor
Setup a new instance of the source annotation extractor.
-
#to_s ⇒ String
Formats the found annotations.
Constructor Details
#initialize(tag = nil, options = {}) ⇒ Countless::SourceAnnotationExtractor
Setup a new instance of the source annotation extractor.
If tag
is nil
, annotations with either default or registered tags are printed. Specific directories can be explicitly set using the :dirs
key in options
.
Countless::SourceAnnotationExtractor.enumerate(
'TODO|FIXME', dirs: %w(app lib), tag: true
)
If options
has a :tag
flag, it will be passed to each annotation’s to_s
. See #find_in
for a list of file extensions that will be taken into account.
36 37 38 39 40 41 42 |
# File 'lib/countless/annotations.rb', line 36 def initialize(tag = nil, = {}) @tag = tag || Annotation..join('|') @dirs = .delete(:dirs) || Annotation.directories @files = .delete(:files) || Annotation.files @options = @annotations = find(dirs: dirs, files: files) end |
Instance Attribute Details
#annotations ⇒ Object (readonly)
Returns the value of attribute annotations.
17 18 19 |
# File 'lib/countless/annotations.rb', line 17 def annotations @annotations end |
#dirs ⇒ Object (readonly)
Returns the value of attribute dirs.
17 18 19 |
# File 'lib/countless/annotations.rb', line 17 def dirs @dirs end |
#files ⇒ Object (readonly)
Returns the value of attribute files.
17 18 19 |
# File 'lib/countless/annotations.rb', line 17 def files @files end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
17 18 19 |
# File 'lib/countless/annotations.rb', line 17 def @options end |
#tag ⇒ Object (readonly)
Returns the value of attribute tag.
17 18 19 |
# File 'lib/countless/annotations.rb', line 17 def tag @tag end |
Instance Method Details
#annotations_in(file) ⇒ Hash{String => Array<Annotation>}
Returns a hash that maps filenames under file
(de-glob-bed) to arrays with their annotations. Files with extensions registered in Countless::SourceAnnotationExtractor::Annotation.extensions
are taken into account. Only files with annotations are included.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/countless/annotations.rb', line 87 def annotations_in(file) results = {} Dir.glob(file) do |item| extension = Annotation.extensions.detect { |regexp, _block| regexp.match(item) } if extension pattern = extension.last.call(tag) results.update(extract_annotations_from(item, pattern)) if pattern end end results end |
#extract_annotations_from(file, pattern) ⇒ Hash{String => Annotation}
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.
110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/countless/annotations.rb', line 110 def extract_annotations_from(file, pattern) lineno = 0 result = File.readlines( file, encoding: Encoding::BINARY ).inject([]) do |list, line| lineno += 1 next list unless line =~ pattern list << Annotation.new(lineno, Regexp.last_match(1), Regexp.last_match(2)) end result.empty? ? {} : { file => result } end |
#find(files: [], dirs: []) ⇒ Hash{String => Array<Annotation>}
Returns a hash that maps filenames under dirs
(recursively) to arrays with their annotations.
50 51 52 53 54 55 |
# File 'lib/countless/annotations.rb', line 50 def find(files: [], dirs: []) results = {} files.inject(results) { |memo, file| memo.update(annotations_in(file)) } dirs.inject(results) { |memo, dir| memo.update(find_in(dir)) } results end |
#find_in(dir) ⇒ Hash{String => Array<Annotation>}
Returns a hash that maps filenames under dir
(recursively) to arrays with their annotations. Files with extensions registered in Countless::SourceAnnotationExtractor::Annotation.extensions
are taken into account. Only files with annotations are included.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/countless/annotations.rb', line 64 def find_in(dir) results = {} Dir.glob("#{dir}/*") do |item| next if File.basename(item)[0] == '.' if File.directory?(item) results.update(find_in(item)) else results.update(annotations_in(item)) end end results end |
#to_s ⇒ String
Formats the found annotations.
rubocop:disable Metrics/AbcSize – because of the indentation logic
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/countless/annotations.rb', line 129 def to_s buf = [] [:indent] = annotations.flat_map do |_f, a| a.map(&:line) end.max.to_s.size annotations.keys.sort.each do |file| buf << "#{file}:" annotations[file].each { |note| buf << " * #{note.to_s()}" } buf << '' end buf.join("\n") end |