Class: DNote::Session
- Inherits:
-
Object
- Object
- DNote::Session
- Defined in:
- lib/dnote/session.rb
Overview
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.
Constant Summary collapse
- DIR =
Directory relative to this script. This is used to lookup the available format templates.
File.dirname(__FILE__)
- DEFAULT_FORMAT =
Default format.
"text"
- DEFAULT_TITLE =
Default title.
"Developer's Notes"
Instance Attribute Summary collapse
-
#colon ⇒ Object
Selected labels can optionally do without the colon.
-
#context ⇒ Object
Number of lines of context to display.
-
#dryrun ⇒ Object
If output path given, don’t actually write to disk.
-
#exclude ⇒ Object
Paths to exclude (match by pathname).
-
#format ⇒ Object
Output format.
-
#ignore ⇒ Object
Paths to ignore (match by basename).
-
#labels ⇒ Object
Labels to lookup.
-
#marker ⇒ Object
Alternate remark marker.
-
#output ⇒ Object
Output to a file instead of STDOUT.
-
#paths ⇒ Object
Paths to include.
-
#template ⇒ Object
If custom format, specify template file.
-
#title ⇒ Object
Some format put a title at the top of the output.
-
#url ⇒ Object
String template for line URLs (mainly for HTML format).
Class Method Summary collapse
-
.main(*argv) ⇒ Object
Commandline interface.
Instance Method Summary collapse
-
#files ⇒ Object
Collect path globs and remove exclusions.
-
#glob(paths) ⇒ Object
Collect the file glob of each path given.
-
#list_templates ⇒ Object
List availble format templates.
-
#run ⇒ Object
Run session.
Instance Attribute Details
#colon ⇒ Object
Selected labels can optionally do without the colon.
40 41 42 |
# File 'lib/dnote/session.rb', line 40 def colon @colon end |
#context ⇒ Object
Number of lines of context to display. The default is zero.
69 70 71 |
# File 'lib/dnote/session.rb', line 69 def context @context end |
#dryrun ⇒ Object
If output path given, don’t actually write to disk.
59 60 61 |
# File 'lib/dnote/session.rb', line 59 def dryrun @dryrun end |
#exclude ⇒ Object
Paths to exclude (match by pathname).
30 31 32 |
# File 'lib/dnote/session.rb', line 30 def exclude @exclude end |
#format ⇒ Object
Output format.
46 47 48 |
# File 'lib/dnote/session.rb', line 46 def format @format end |
#ignore ⇒ Object
Paths to ignore (match by basename).
33 34 35 |
# File 'lib/dnote/session.rb', line 33 def ignore @ignore end |
#labels ⇒ Object
Labels to lookup. By default these are TODO, FIXME and OPTIMIZE.
37 38 39 |
# File 'lib/dnote/session.rb', line 37 def labels @labels end |
#marker ⇒ Object
Alternate remark marker. Useful to other languages besides Ruby.
43 44 45 |
# File 'lib/dnote/session.rb', line 43 def marker @marker end |
#output ⇒ Object
Output to a file instead of STDOUT.
56 57 58 |
# File 'lib/dnote/session.rb', line 56 def output @output end |
#paths ⇒ Object
Paths to include.
27 28 29 |
# File 'lib/dnote/session.rb', line 27 def paths @paths end |
#template ⇒ Object
If custom format, specify template file.
49 50 51 |
# File 'lib/dnote/session.rb', line 49 def template @template end |
#title ⇒ Object
Some format put a title at the top of the output. The default is “Developer’s Notes”.
53 54 55 |
# File 'lib/dnote/session.rb', line 53 def title @title end |
#url ⇒ Object
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"
66 67 68 |
# File 'lib/dnote/session.rb', line 66 def url @url end |
Class Method Details
Instance Method Details
#files ⇒ Object
Collect path globs and remove exclusions. This method uses #paths, #exclude and #ignore to compile the list of files.
127 128 129 130 131 132 133 134 135 |
# 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) ⇒ Object
Collect the file glob of each path given. If a path is a directory, inclue all content.
139 140 141 142 143 144 145 146 147 |
# 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 |
#list_templates ⇒ Object
List availble format templates
150 151 152 153 154 155 156 157 158 |
# 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 ⇒ Object
Run session.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# 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 |