Class: DNote::Note
- Inherits:
-
Object
- Object
- DNote::Note
- 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
-
#capture ⇒ Object
readonly
Contextual lines of code.
-
#file ⇒ Object
readonly
The file in which the note is made.
-
#label ⇒ Object
readonly
The type of note.
-
#line ⇒ Object
readonly
The line number of the note.
-
#mark ⇒ Object
readonly
Remark marker used in parsing the note.
-
#notes ⇒ Object
readonly
Set of notes to which this note belongs.
-
#text ⇒ Object
readonly
The verbatim text of the note.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Sort by file name and line number.
- #code ⇒ Object
-
#code? ⇒ Boolean
Is there code to show?.
-
#initialize(notes, file, label, line, text, mark) ⇒ Note
constructor
Initialize new Note instance.
-
#textline ⇒ Object
Remove newlines from note text.
-
#to_h ⇒ Object
Convert to Hash.
-
#to_h_raw ⇒ Object
Convert to Hash, leaving the note text verbatim.
-
#to_json(*args) ⇒ Object
Convert to JSON.
-
#to_s ⇒ Object
Convert to string representation.
-
#to_str ⇒ Object
Convert to string representation.
-
#to_yaml(*args) ⇒ Object
Convert to YAML.
-
#unindent(lines) ⇒ Object
private
Remove blank space from lines.
-
#url ⇒ Object
Return line URL based on URL template.
Constructor Details
#initialize(notes, file, label, line, text, mark) ⇒ Note
Initialize new Note instance.
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/dnote/note.rb', line 35 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
#capture ⇒ Object (readonly)
Contextual lines of code.
32 33 34 |
# File 'lib/dnote/note.rb', line 32 def capture @capture end |
#file ⇒ Object (readonly)
The file in which the note is made.
17 18 19 |
# File 'lib/dnote/note.rb', line 17 def file @file end |
#label ⇒ Object (readonly)
The type of note.
20 21 22 |
# File 'lib/dnote/note.rb', line 20 def label @label end |
#line ⇒ Object (readonly)
The line number of the note.
23 24 25 |
# File 'lib/dnote/note.rb', line 23 def line @line end |
#mark ⇒ Object (readonly)
Remark marker used in parsing the note.
29 30 31 |
# File 'lib/dnote/note.rb', line 29 def mark @mark end |
#notes ⇒ Object (readonly)
Set of notes to which this note belongs.
14 15 16 |
# File 'lib/dnote/note.rb', line 14 def notes @notes end |
#text ⇒ Object (readonly)
The verbatim text of the note.
26 27 28 |
# File 'lib/dnote/note.rb', line 26 def text @text end |
Instance Method Details
#<=>(other) ⇒ Object
Sort by file name and line number.
62 63 64 65 66 |
# File 'lib/dnote/note.rb', line 62 def <=>(other) s = file <=> other.file return s unless s == 0 line <=> other.line end |
#code ⇒ Object
103 104 105 |
# File 'lib/dnote/note.rb', line 103 def code unindent(capture).join end |
#code? ⇒ Boolean
Is there code to show?
108 109 110 |
# File 'lib/dnote/note.rb', line 108 def code? !capture.empty? end |
#textline ⇒ Object
Remove newlines from note text.
57 58 59 |
# File 'lib/dnote/note.rb', line 57 def textline text.gsub("\n", " ") end |
#to_h ⇒ Object
Convert to Hash. – TODO: Add url
? TODO: Add code
? Problem is that xml needs code in CDATA. ++
73 74 75 |
# File 'lib/dnote/note.rb', line 73 def to_h { 'label'=>label, 'text'=>textline, 'file'=>file, 'line'=>line } end |
#to_h_raw ⇒ Object
Convert to Hash, leaving the note text verbatim.
78 79 80 |
# File 'lib/dnote/note.rb', line 78 def to_h_raw { 'label'=>label, 'text'=>text, 'file'=>file, 'line'=>line, 'code'=>code } end |
#to_json(*args) ⇒ Object
Convert to JSON.
83 84 85 |
# File 'lib/dnote/note.rb', line 83 def to_json(*args) to_h_raw.to_json(*args) end |
#to_s ⇒ Object
Convert to string representation.
47 48 49 |
# File 'lib/dnote/note.rb', line 47 def to_s "#{label}: #{text}" end |
#to_str ⇒ Object
Convert to string representation.
52 53 54 |
# File 'lib/dnote/note.rb', line 52 def to_str "#{label}: #{text}" end |
#to_yaml(*args) ⇒ Object
Convert to YAML.
88 89 90 |
# File 'lib/dnote/note.rb', line 88 def to_yaml(*args) to_h_raw.to_yaml(*args) end |
#unindent(lines) ⇒ Object (private)
Remove blank space from lines.
134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/dnote/note.rb', line 134 def unindent(lines) dents = [] lines.each do |line| if md = /^([\ ]*)/.match(line) size = md[1].size dents << md[1] end end dent = dents.min{ |a,b| a.size <=> b.size } lines.map do |line| line.sub(dent, '') end end |
#url ⇒ Object
Return line URL based on URL template. If no template was set, then returns the file.
94 95 96 97 98 99 100 |
# File 'lib/dnote/note.rb', line 94 def url if notes.url notes.url % [file, line] else file end end |