Class: Dotnotes::Application

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

Instance Method Summary collapse

Constructor Details

#initializeApplication



10
11
12
# File 'lib/dotnotes.rb', line 10

def initialize
  @notes_path = ENV['HOME'] + '/.notes'
end

Instance Method Details

#delete(filename) ⇒ Object

Delete a note filename is the name of the note

note.delete(name)



75
76
77
78
79
80
81
82
# File 'lib/dotnotes.rb', line 75

def delete filename
  file_path = @notes_path + "/" + filename
  if initialized?(file: filename)
    system("rm #{file_path}")
  else
    raise NoteNotExist
  end
end

#edit(filename) ⇒ Object

Edit a note filename is the name of the note

note.show(name)



56
57
58
59
60
61
62
63
# File 'lib/dotnotes.rb', line 56

def edit filename
  file_path = @notes_path + "/" + filename
  if initialized?(file: filename)
    system("vi #{file_path}")
  else
    raise NoteNotExist
  end
end

#find(keyword) ⇒ Object

Find a keyword in a note



85
86
87
# File 'lib/dotnotes.rb', line 85

def find keyword
  system("find #{@notes_path} -type f -print0 | xargs -0 grep -li #{keyword}")
end

#helpObject



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/dotnotes.rb', line 89

def help
  puts ""
  puts "Commands:"
  puts "  note new {filename}    # Create a new note with filename"
  puts "  note show {filename}   # Display a note"
  puts "  note edit {filename}   # Edit a note"
  puts "  note delete {filename} # Delete a note"
  puts "  note list              # List all notes"
  puts "  note find {keyword}      # Check which files contain the keyword"
  puts "  note help              # Display this help"
  puts ""
end

#initialized?(p = {}) ⇒ Boolean



14
15
16
17
# File 'lib/dotnotes.rb', line 14

def initialized? p={}
  path = @notes_path + "/" + p[:file].to_s
  File.exists?(path)
end

#listObject

Show all notes



66
67
68
# File 'lib/dotnotes.rb', line 66

def list
  system("stat #{@notes_path}/* -c \"%y %s %n\"") unless Dir[@notes_path + "/*"].empty?
end

#new(filename) ⇒ Object

Create a new note in notes path filename is the name of the note

begin

note.new(name)

rescue NoteAlreadyExist

puts "the note already exist

rescue UninitializedNotesPath

puts "note.init must be run first"

end



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

def new filename
  Dir.mkdir @notes_path unless initialized?

  if initialized?(file: filename)
    raise NoteAlreadyExist
  else
    file = File.open(@notes_path + "/#{filename}", "w")
    system("vi #{file.path}")
  end
end

#show(filename) ⇒ Object

Display contents of an existing note filename is the name of the note

note.show(name)



46
47
48
49
# File 'lib/dotnotes.rb', line 46

def show filename
  file_path = @notes_path + "/" + filename
  system("cat #{file_path}") if initialized?(file: filename)
end