Class: DNote::Note

Inherits:
Object
  • Object
show all
Defined in:
lib/dnote/note.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(notes, file, label, line, text, mark) ⇒ Note

Initialize new Note instance.



32
33
34
35
36
37
38
39
40
41
# 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

Instance Attribute Details

#captureObject (readonly)

Contextual lines of code.



29
30
31
# File 'lib/dnote/note.rb', line 29

def capture
  @capture
end

#fileObject (readonly)

The file in which the note is made.



14
15
16
# File 'lib/dnote/note.rb', line 14

def file
  @file
end

#labelObject (readonly)

The type of note.



17
18
19
# File 'lib/dnote/note.rb', line 17

def label
  @label
end

#lineObject (readonly)

The line number of the note.



20
21
22
# File 'lib/dnote/note.rb', line 20

def line
  @line
end

#markObject (readonly)

Remark marker used in parsing the note.



26
27
28
# File 'lib/dnote/note.rb', line 26

def mark
  @mark
end

#notesObject (readonly)

Set of notes to which this note belongs.



11
12
13
# File 'lib/dnote/note.rb', line 11

def notes
  @notes
end

#textObject (readonly)

The verbatim text of the note.



23
24
25
# File 'lib/dnote/note.rb', line 23

def text
  @text
end

Instance Method Details

#<=>(other) ⇒ Object

Sort by file name and line number.



59
60
61
62
63
64
# File 'lib/dnote/note.rb', line 59

def <=>(other)
  s = file <=> other.file
  return s unless s.zero?

  line <=> other.line
end

#codeObject



100
101
102
# File 'lib/dnote/note.rb', line 100

def code
  unindent(capture).join
end

#code?Boolean

Is there code to show?

Returns:

  • (Boolean)


105
106
107
# File 'lib/dnote/note.rb', line 105

def code?
  !capture.empty?
end

#textlineObject

Remove newlines from note text.



54
55
56
# File 'lib/dnote/note.rb', line 54

def textline
  text.tr("\n", ' ')
end

#to_hObject

Convert to Hash. – TODO: Add url? TODO: Add code? Problem is that xml needs code in CDATA. ++



71
72
73
# File 'lib/dnote/note.rb', line 71

def to_h
  { 'label' => label, 'text' => textline, 'file' => file, 'line' => line }
end

#to_h_rawObject

Convert to Hash, leaving the note text verbatim.



76
77
78
# File 'lib/dnote/note.rb', line 76

def to_h_raw
  { 'label' => label, 'text' => text, 'file' => file, 'line' => line, 'code' => code }
end

#to_json(*args) ⇒ Object

Convert to JSON.



81
82
83
# File 'lib/dnote/note.rb', line 81

def to_json(*args)
  to_h_raw.to_json(*args)
end

#to_sObject

Convert to string representation.



44
45
46
# File 'lib/dnote/note.rb', line 44

def to_s
  "#{label}: #{text}"
end

#to_strObject

Convert to string representation.



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

def to_str
  "#{label}: #{text}"
end

#to_yaml(*args) ⇒ Object

Convert to YAML.



86
87
88
# File 'lib/dnote/note.rb', line 86

def to_yaml(*args)
  to_h_raw.to_yaml(*args)
end

#urlObject

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



92
93
94
95
96
97
98
# File 'lib/dnote/note.rb', line 92

def url
  if notes.url
    format(notes.url, file, line)
  else
    file
  end
end