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



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

def line
  @line
end

#tagObject

Returns the value of attribute tag

Returns:

  • (Object)

    the current value of tag



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

def tag
  @tag
end

#textObject

Returns the value of attribute text

Returns:

  • (Object)

    the current value of text



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

def text
  @text
end

Class Method Details

.directoriesArray<String>

Returns the currently configured directories.

Returns:

  • (Array<String>)

    the configured directories



162
163
164
165
# File 'lib/countless/annotations.rb', line 162

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



193
194
195
196
197
198
199
200
201
202
203
# File 'lib/countless/annotations.rb', line 193

def self.extensions
  @extensions ||= begin
    patterns = Countless.configuration.annotation_patterns.values
    patterns.map do |conf|
      [
        extensions_regexp(conf[:extensions], conf[:files] || []),
        conf[:regex]
      ]
    end.to_h
  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



218
219
220
221
222
223
# File 'lib/countless/annotations.rb', line 218

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



147
148
149
150
# File 'lib/countless/annotations.rb', line 147

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



170
171
172
# File 'lib/countless/annotations.rb', line 170

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



209
210
211
# File 'lib/countless/annotations.rb', line 209

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



155
156
157
# File 'lib/countless/annotations.rb', line 155

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



186
187
188
# File 'lib/countless/annotations.rb', line 186

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

.tagsArray<String>

Returns the currently configured tags.

Returns:

  • (Array<String>)

    the configured tags



177
178
179
180
181
# File 'lib/countless/annotations.rb', line 177

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



235
236
237
238
239
# File 'lib/countless/annotations.rb', line 235

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