class DNote::Session

User session which is used by commandline interface.

By making this a class it makes it easy for external libraries to use this library just as if they were calling the commandline, but without the need to shellout.

Constants

DEFAULT_FORMAT

Default format.

DEFAULT_TITLE

Default title.

DIR

Directory relative to this script. This is used to lookup the available format templates.

Attributes

colon[RW]

Selected labels can optionally do without the colon.

context[RW]

Number of lines of context to display. The default is zero.

dryrun[RW]

If output path given, don’t actually write to disk.

exclude[R]

Paths to exclude (match by pathname).

format[RW]

Output format.

ignore[R]

Paths to ignore (match by basename).

labels[RW]

Labels to lookup. By default these are TODO, FIXME and OPTIMIZE.

marker[RW]

Alternate remark marker. Useful to other languages besides Ruby.

output[RW]

Output to a file instead of STDOUT.

paths[RW]

Paths to include.

template[RW]

If custom format, specify template file.

title[RW]

Some format put a title at the top of the output. The default is “Developer’s Notes”.

url[RW]

String template for line URLs (mainly for HTML format). For example, DNote uses GitHub so we could use a link template:

"https://github.com/rubyworks/dnote/blob/master/%s#L%s"

Public Class Methods

main(*argv) click to toggle source

Commandline interface.

# File lib/dnote/session.rb, line 161
def self.main(*argv)
  session = Options.parse(*argv)
  session.run
end
new(options = {}) { |self| ... } click to toggle source

New Session.

# File lib/dnote/session.rb, line 74
def initialize(options = {})
  options ||= {}
  initialize_defaults
  options.each { |k, v| __send__("#{k}=", v) }
  yield(self) if block_given?
end

Public Instance Methods

exclude=(list) click to toggle source

Set exclude list ensuring that the value is an array.

# File lib/dnote/session.rb, line 98
def exclude=(list)
  @exclude = [list].compact.flatten.compact
end
files() click to toggle source

Collect path globs and remove exclusions. This method uses paths, exclude and ignore to compile the list of files.

# File lib/dnote/session.rb, line 127
def files
  list = [paths].flatten.compact
  list = ["**/*.rb"] if list.empty?
  list = glob(list)
  list -= glob(exclude)
  list.reject do |path|
    path.split("/").any? { |part| ignore.any? { |ig| File.fnmatch?(ig, part) } }
  end
end
glob(paths) click to toggle source

Collect the file glob of each path given. If a path is a directory, inclue all content.

# File lib/dnote/session.rb, line 139
def glob(paths)
  paths.map do |path|
    if File.directory?(path)
      Dir.glob(File.join(path, "**/*"))
    else
      Dir.glob(path)
    end
  end.flatten.uniq
end
ignore=(list) click to toggle source

Set ignore list ensuring that the value is an array.

# File lib/dnote/session.rb, line 103
def ignore=(list)
  @ignore = [list].compact.flatten.compact
end
list_templates() click to toggle source

List availble format templates

# File lib/dnote/session.rb, line 150
def list_templates
  tdir = File.join(DIR, "templates")
  tfiles = Dir[File.join(tdir, "**/*.erb")]
  tnames = tfiles.map { |tname| tname.sub("#{tdir}/", "").chomp(".erb") }
  groups = tnames.group_by { |tname| tname.split("/").first }
  groups.sort.each do |(_type, names)|
    puts("%-18s " * names.size % names.sort)
  end
end
run() click to toggle source

Run session.

# File lib/dnote/session.rb, line 108
def run
  notes = Notes.new(files,
                    labels: labels,
                    colon: colon,
                    marker: marker,
                    url: url,
                    context: context)
  collection = notes.notes_collection
  formatter = Format.new(collection,
                         format: format,
                         template: template,
                         title: title,
                         output: output)
  formatter.render
end

Private Instance Methods

initialize_defaults() click to toggle source

Set default values for attributes.

# File lib/dnote/session.rb, line 82
def initialize_defaults
  @paths = []
  @labels = []
  @exclude = []
  @ignore = []
  @format = DEFAULT_FORMAT
  @title = DEFAULT_TITLE
  @dryrun = false
  @marker = nil
  @url = nil
  @context = 0
end