Class: Note
- Inherits:
-
Object
- Object
- Note
- Defined in:
- lib/qv/note.rb
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
Class Method Summary collapse
Instance Method Summary collapse
- #body ⇒ Object
- #edit ⇒ Object
- #get_io ⇒ Object
-
#initialize(args = {}) ⇒ Note
constructor
A new instance of Note.
- #matches?(search_term) ⇒ Boolean
- #path ⇒ Object
- #title ⇒ Object
- #title_matches?(search_term) ⇒ Boolean
- #validate(filename) ⇒ Object
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
#filename ⇒ Object (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_notes ⇒ Object
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
#body ⇒ Object
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 |
#edit ⇒ Object
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_io ⇒ Object
26 27 28 |
# File 'lib/qv/note.rb', line 26 def get_io File.open(@path) end |
#matches?(search_term) ⇒ 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 |
#path ⇒ Object
22 23 24 |
# File 'lib/qv/note.rb', line 22 def path @path || File.join(QV.notes_dir,@filename) end |
#title ⇒ Object
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
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
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 |