Class: DNote::Session

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#colonObject

Selected labels can optionally do without the colon.



40
41
42
# File 'lib/dnote/session.rb', line 40

def colon
  @colon
end

#contextObject

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

#dryrunObject

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



59
60
61
# File 'lib/dnote/session.rb', line 59

def dryrun
  @dryrun
end

#excludeObject

Paths to exclude (match by pathname).



30
31
32
# File 'lib/dnote/session.rb', line 30

def exclude
  @exclude
end

#formatObject

Output format.



46
47
48
# File 'lib/dnote/session.rb', line 46

def format
  @format
end

#ignoreObject

Paths to ignore (match by basename).



33
34
35
# File 'lib/dnote/session.rb', line 33

def ignore
  @ignore
end

#labelsObject

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

#markerObject

Alternate remark marker. Useful to other languages besides Ruby.



43
44
45
# File 'lib/dnote/session.rb', line 43

def marker
  @marker
end

#outputObject

Output to a file instead of STDOUT.



56
57
58
# File 'lib/dnote/session.rb', line 56

def output
  @output
end

#pathsObject

Paths to include.



27
28
29
# File 'lib/dnote/session.rb', line 27

def paths
  @paths
end

#templateObject

If custom format, specify template file.



49
50
51
# File 'lib/dnote/session.rb', line 49

def template
  @template
end

#titleObject

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

#urlObject

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

.main(*argv) ⇒ Object

Commandline interface.



161
162
163
164
# File 'lib/dnote/session.rb', line 161

def self.main(*argv)
  session = Options.parse(*argv)
  session.run
end

Instance Method Details

#filesObject

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_templatesObject

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

#runObject

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