Class: Doing::Note

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

Overview

This class describes an item note.

Instance Method Summary collapse

Methods inherited from Array

#cap_first, #good?, #utf8

Methods included from ChronifyArray

#time_string, #to_abbr, #to_natural, #to_years

Constructor Details

#initialize(note = []) ⇒ Note

Initializes a new note

Parameters:

  • note (Array) (defaults to: [])

    Initial note, can be string or array



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

def initialize(note = [])
  super()

  add(note) if note
end

Instance Method Details

#add(note, replace: false) ⇒ Object

Add note contents, optionally replacing existing note

Parameters:

  • note (Array|String|Note)

    The note to add, can be String, Array, or Note

  • replace (Boolean) (defaults to: false)

    replace existing content



28
29
30
31
32
33
34
35
36
# File 'lib/doing/note.rb', line 28

def add(note, replace: false)
  clear if replace
  case note
  when String
    append_string(note)
  when Array
    append(note)
  end
end

#compressArray

Remove blank lines and comments (#)

Returns:

  • (Array)

    compressed array



43
44
45
# File 'lib/doing/note.rb', line 43

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

#compress!Object



47
48
49
# File 'lib/doing/note.rb', line 47

def compress!
  replace compress
end

#equal?(other) ⇒ Boolean

Test if a note is equal (compare string representations)

Parameters:

  • other (Note)

    The other Note

Returns:

  • (Boolean)

    true if equal



99
100
101
102
103
# File 'lib/doing/note.rb', line 99

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

  to_s == other.to_s
end

#strip_linesArray

Remove leading/trailing whitespace for every line of note

Returns:

  • (Array)

    Stripped note



57
58
59
# File 'lib/doing/note.rb', line 57

def strip_lines
  Note.new(map(&:strip))
end

#strip_lines!Object



61
62
63
# File 'lib/doing/note.rb', line 61

def strip_lines!
  replace strip_lines
end

#to_line(separator: ' ') ⇒ String

Returns note as a single line, newlines separated by space

Parameters:

  • separator (defaults to: ' ')

    The separator with which to join multiple lines

Returns:

  • (String)

    Line representation of the Note.



83
84
85
# File 'lib/doing/note.rb', line 83

def to_line(separator: ' ')
  compress.strip_lines.join(separator)
end

#to_s(prefix: "\t\t") ⇒ Object

Note as multi-line string

Parameters:

  • prefix (String) (defaults to: "\t\t")

    prefix for each line (default two tabs, TaskPaper format)



70
71
72
# File 'lib/doing/note.rb', line 70

def to_s(prefix: "\t\t")
  compress.strip_lines.map { |l| "#{prefix}#{l}" }.join("\n")
end