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
Selected labels can optionally do without the colon.
Number of lines of context to display. The default is zero.
If output path given, don’t actually write to disk.
Paths to exclude (match by pathname).
Output format.
Paths to ignore (match by basename).
Labels to lookup. By default these are TODO, FIXME and OPTIMIZE.
Alternate remark marker. Useful to other languages besides Ruby.
Output to a file instead of STDOUT.
Paths to include.
If custom format, specify template file.
Some format put a title at the top of the output. The default is “Developer’s Notes”.
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
Commandline interface.
# File lib/dnote/session.rb, line 161 def self.main(*argv) session = Options.parse(*argv) session.run end
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
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
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
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
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 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 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
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