class Note

Attributes

filename[R]

Public Class Methods

get_notes() click to toggle source
# 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
new(args = {}) click to toggle source
# File lib/qv/note.rb, line 6
def initialize(args = {})
  @filename = validate(args[:file])
  @title = title
  @path = path
end
sort_notes_by_date(notes) click to toggle source
# 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

Public Instance Methods

body() click to toggle source
# 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
edit() click to toggle source
# File lib/qv/note.rb, line 56
def edit
  editor = ENV["EDITOR"] || '/usr/bin/vim'
  exec "#{editor} \"#{path}\";clear"
  system "clear"
end
get_io() click to toggle source
# File lib/qv/note.rb, line 26
def get_io
  File.open(@path)
end
matches?(search_term) click to toggle source
# 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
path() click to toggle source
# File lib/qv/note.rb, line 22
def path
  @path || File.join(QV.notes_dir,@filename)
end
title() click to toggle source
# File lib/qv/note.rb, line 18
def title
  @title || File.basename(@filename, File.extname(@filename))
end
title_matches?(search_term) click to toggle source
# 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) click to toggle source
# 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