Class: Note

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Note

Returns a new instance of Note.



6
7
8
9
10
# File 'lib/qv/note.rb', line 6

def initialize(args = {})
  @filename = validate(args[:file])
  @title = title
  @path = path
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



4
5
6
# File 'lib/qv/note.rb', line 4

def filename
  @filename
end

Class Method Details

.get_notesObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/qv/note.rb', line 68

def self.get_notes
  note_names = Dir.entries(QV.notes_dir).reject do |note_filename|
    exceptions = [
      "..",
      ".",
      "Interim Note-Changes",
      "Notes & Settings"
    ]
    exceptions.include?(note_filename) || note_filename.start_with?(".") || File.directory?(File.join(QV.notes_dir,note_filename))
  end
  note_names.map do |file|
    Note.new(:file => file)
  end
end

.sort_notes_by_date(notes) ⇒ Object



62
63
64
65
66
# File 'lib/qv/note.rb', line 62

def self.sort_notes_by_date(notes)
  notes.sort_by! { |note|
    File.mtime(note.path)}
  notes.reverse!
end

Instance Method Details

#bodyObject



30
31
32
33
34
35
36
37
38
# File 'lib/qv/note.rb', line 30

def body
  begin
    get_io.read.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
  rescue => e
    # TODO write logger
    # this seems hacky, but definitely works for now
    ""
  end
end

#editObject



56
57
58
59
60
# File 'lib/qv/note.rb', line 56

def edit
  editor = ENV["EDITOR"] || '/usr/bin/vim'
  exec "#{editor} \"#{path}\";clear"
  system "clear"
end

#get_ioObject



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

def get_io
  File.open(@path)
end

#matches?(search_term) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
# File 'lib/qv/note.rb', line 40

def matches?(search_term)
  begin
    body.match(/^.*#{search_term}.*$/i)
  rescue ArgumentError => e
    raise ArgumentError, "#{filename}: #{e}"
  end
end

#pathObject



22
23
24
# File 'lib/qv/note.rb', line 22

def path
  @path || File.join(QV.notes_dir,@filename)
end

#titleObject



18
19
20
# File 'lib/qv/note.rb', line 18

def title
  @title || File.basename(@filename, File.extname(@filename))
end

#title_matches?(search_term) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
# File 'lib/qv/note.rb', line 48

def title_matches?(search_term)
  begin
    title.match(/^.*#{search_term}.*$/i)
  rescue ArgumentError => e
    puts "ERR: Filename of #{@filename}\n#{e}"
  end
end

#validate(filename) ⇒ Object

Raises:

  • (ArgumentError)


12
13
14
15
16
# File 'lib/qv/note.rb', line 12

def validate(filename)
  raise ArgumentError, "Note requires a :file argument" unless filename
  raise ArgumentError, "#{filename} contains an invalid character" unless filename.valid_encoding?
  filename
end