Class: Doing::Note

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

Overview

This class describes an item note.

Instance Method Summary collapse

Methods inherited from Array

#highlight_tags, #log_tags, #to_tags

Constructor Details

#initialize(note = []) ⇒ Note

Returns a new instance of Note.



8
9
10
11
12
# File 'lib/doing/note.rb', line 8

def initialize(note = [])
  super()

  add(note) if note
end

Instance Method Details

#add(note, replace: false) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/doing/note.rb', line 14

def add(note, replace: false)
  clear if replace
  if note.is_a?(String)
    append_string(note)
  elsif note.is_a?(Array)
    append(note)
  end
end

#append(lines) ⇒ Object



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

def append(lines)
  concat(lines)
  replace compress
end

#append_string(input) ⇒ Object



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

def append_string(input)
  concat(input.split(/\n/).map(&:strip))
  replace compress
end

#compressObject



37
38
39
# File 'lib/doing/note.rb', line 37

def compress
  delete_if { |l| l =~ /^\s*$/ || l =~ /^#/ }
end

#compress!Object



33
34
35
# File 'lib/doing/note.rb', line 33

def compress!
  replace compress
end

#equal?(other) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
# File 'lib/doing/note.rb', line 53

def equal?(other)
  return false unless other.is_a?(Note)

  to_s == other.to_s
end

#strip_linesObject



45
46
47
# File 'lib/doing/note.rb', line 45

def strip_lines
  map(&:strip)
end

#strip_lines!Object



41
42
43
# File 'lib/doing/note.rb', line 41

def strip_lines!
  replace strip_lines
end

#to_sObject



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

def to_s
  compress.strip_lines.join("\n")
end