Class: Countless::Annotations::Annotation

Inherits:
Struct
  • Object
show all
Defined in:
lib/countless/annotations.rb

Overview

A single annotation representation.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#lineObject

Returns the value of attribute line

Returns:

  • (Object)

    the current value of line



138
139
140
# File 'lib/countless/annotations.rb', line 138

def line
  @line
end

#tagObject

Returns the value of attribute tag

Returns:

  • (Object)

    the current value of tag



138
139
140
# File 'lib/countless/annotations.rb', line 138

def tag
  @tag
end

#textObject

Returns the value of attribute text

Returns:

  • (Object)

    the current value of text



138
139
140
# File 'lib/countless/annotations.rb', line 138

def text
  @text
end

Class Method Details

.directoriesArray<String>

Returns the currently configured directories.

Returns:

  • (Array<String>)

    the configured directories



157
158
159
160
# File 'lib/countless/annotations.rb', line 157

def self.directories
  @directories ||=
    Countless.configuration.annotations_directories.deep_dup
end

.extensionsHash<RegExp => Proc>

Returns the currently configured file extension handlers.

Returns:

  • (Hash<RegExp => Proc>)

    the configured file extension handlers



188
189
190
191
192
193
194
195
196
197
198
# File 'lib/countless/annotations.rb', line 188

def self.extensions
  @extensions ||= begin
    patterns = Countless.configuration.annotation_patterns.values
    patterns.to_h do |conf|
      [
        extensions_regexp(conf[:extensions], conf[:files] || []),
        conf[:regex]
      ]
    end
  end
end

.extensions_regexp(exts, files = []) ⇒ RegExp

Build a new extension regexp of the given extensions.

Parameters:

  • exts (Array<String>)

    the file extensions to join

  • files (Array<String>) (defaults to: [])

    a list of dedicated files

Returns:

  • (RegExp)

    the extensions matching regexp



213
214
215
216
217
218
# File 'lib/countless/annotations.rb', line 213

def self.extensions_regexp(exts, files = [])
  exts = /\.(#{exts.join('|')})$/
  return exts if files.empty?

  Regexp.union(/^#{files.join('|')}$/, exts)
end

.filesArray<String>

Returns the currently configured files.

Returns:

  • (Array<String>)

    the configured files



142
143
144
145
# File 'lib/countless/annotations.rb', line 142

def self.files
  @files ||=
    Countless.configuration.annotations_files.deep_dup
end

.register_directories(*dirs) ⇒ Object

Registers additional directories to be included.

Parameters:

  • dirs (Array<String>)

    the additional directories to include



165
166
167
# File 'lib/countless/annotations.rb', line 165

def self.register_directories(*dirs)
  directories.push(*dirs)
end

.register_extensions(*exts, &block) ⇒ Object

Registers new annotations file extension handlers.

Parameters:

  • exts (Array<String>)

    the file extensions to match

  • block (Proc)

    the line/comment/annotation matching block



204
205
206
# File 'lib/countless/annotations.rb', line 204

def self.register_extensions(*exts, &block)
  extensions[extensions_regexp(exts)] = block
end

.register_files(*dirs) ⇒ Object

Registers additional files to be included.

Parameters:

  • dirs (Array<String>)

    the additional files to include



150
151
152
# File 'lib/countless/annotations.rb', line 150

def self.register_files(*dirs)
  files.push(*dirs)
end

.register_tags(*additional_tags) ⇒ Object

Registers additional tags.

Parameters:

  • additional_tags (Array<String>)

    the additional tags to include



181
182
183
# File 'lib/countless/annotations.rb', line 181

def self.register_tags(*additional_tags)
  tags.push(*additional_tags)
end

.tagsArray<String>

Returns the currently configured tags.

Returns:

  • (Array<String>)

    the configured tags



172
173
174
175
176
# File 'lib/countless/annotations.rb', line 172

def self.tags
  @tags ||= Countless.configuration.annotation_tags.deep_dup.map do |tag|
    "@?#{tag}"
  end
end

Instance Method Details

#to_s(options = {}) ⇒ Object

Returns a representation of the annotation that looks like this:

[126] [TODO] This algorithm is nice and simple, 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. When options has a value for :indent the line number block will be right-justified.

Parameters:

  • options (Hash{Symbol => Mixed}) (defaults to: {})

    the additional options



230
231
232
233
234
# File 'lib/countless/annotations.rb', line 230

def to_s(options = {})
  s = "[#{line.to_s.rjust(options[:indent])}] "
  s << "[#{tag}] " if options[:tag]
  s << text
end