class DNote::Note

The Note class encapsulates a single note made in a source file.

Each note instance holds a reference, notes, to the set of notes being generated for a given session. This allows the note to access general options applicable to all notes.

Attributes

capture[R]

Contextual lines of code.

file[R]

The file in which the note is made.

label[R]

The type of note.

line[R]

The line number of the note.

mark[R]

Remark marker used in parsing the note.

notes[R]

Set of notes to which this note belongs.

text[R]

The verbatim text of the note.

Public Class Methods

new(notes, file, label, line, text, mark) click to toggle source

Initialize new Note instance.

# File lib/dnote/note.rb, line 32
def initialize(notes, file, label, line, text, mark)
  @notes = notes
  @file = file
  @label = label
  @line = line
  @text = text.rstrip
  @mark = mark
  @capture = []
end

Public Instance Methods

<=>(other) click to toggle source

Sort by file name and line number.

# File lib/dnote/note.rb, line 58
def <=>(other)
  s = file <=> other.file
  return s unless s == 0

  line <=> other.line
end
code() click to toggle source
# File lib/dnote/note.rb, line 101
def code
  unindent(capture).join
end
code?() click to toggle source

Is there code to show?

# File lib/dnote/note.rb, line 106
def code?
  !capture.empty?
end
textline() click to toggle source

Remove newlines from note text.

# File lib/dnote/note.rb, line 53
def textline
  text.tr("\n", " ")
end
to_h() click to toggle source

Convert to Hash.

# File lib/dnote/note.rb, line 70
def to_h
  {"label" => label, "text" => textline, "file" => file, "line" => line}
end
to_h_raw() click to toggle source

Convert to Hash, leaving the note text verbatim.

# File lib/dnote/note.rb, line 75
def to_h_raw
  {"label" => label, "text" => text, "file" => file, "line" => line, "code" => code}
end
to_json(*args) click to toggle source

Convert to JSON.

# File lib/dnote/note.rb, line 80
def to_json(*args)
  to_h_raw.to_json(*args)
end
to_s() click to toggle source

Convert to string representation.

# File lib/dnote/note.rb, line 43
def to_s
  "#{label}: #{text}"
end
to_str() click to toggle source

Convert to string representation.

# File lib/dnote/note.rb, line 48
def to_str
  "#{label}: #{text}"
end
to_yaml(*args) click to toggle source

Convert to YAML.

# File lib/dnote/note.rb, line 85
def to_yaml(*args)
  to_h_raw.to_yaml(*args)
end
url() click to toggle source

Return line URL based on URL template. If no template was set, then returns the file.

FIXME: Move out of Note so we can drop the reference to notes

# File lib/dnote/note.rb, line 93
def url
  if notes.url
    format(notes.url, file, line)
  else
    file
  end
end

Private Instance Methods

unindent(lines) click to toggle source

Remove blank space from lines.

# File lib/dnote/note.rb, line 113
def unindent(lines)
  dents = []
  lines.each do |line|
    if (md = /^(\ *)/.match(line))
      dents << md[1]
    end
  end
  dent = dents.min_by(&:size)
  lines.map do |line|
    line.sub(dent, "")
  end
end