Class: Purse::Pocket

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Pocket

Returns a new instance of Pocket.



5
6
7
8
# File 'lib/purse/pocket.rb', line 5

def initialize(path)
  Purse.check_for_parameter('path', path)
  @path = File.expand_path(path)
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/purse/pocket.rb', line 3

def path
  @path
end

Instance Method Details

#commitObject



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

def commit
  git.add('.')
  # return if git.status.changed.empty?
  git.commit_all("Changes via Purse #{Time.now}")
rescue Git::GitExecuteError => e
  puts e
end

#deleteObject



19
20
21
# File 'lib/purse/pocket.rb', line 19

def delete
  FileUtils.rm_rf(@path)
end

#edit(name) {|note| ... } ⇒ Object

Yields:

  • (note)


29
30
31
32
33
34
35
36
37
38
# File 'lib/purse/pocket.rb', line 29

def edit(name)
  Purse.check_for_parameter('name', name)
  begin
    note = find(name)
  rescue Purse::MissingFile
    note = Note.new(path, name, nil)
  end
  yield(note)
  @notes = load_notes
end

#exists?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/purse/pocket.rb', line 15

def exists?
  File.readable?(@path) && File.directory?(@path)
end

#find(name) ⇒ Object



23
24
25
26
27
# File 'lib/purse/pocket.rb', line 23

def find(name)
  Purse.check_for_parameter('name', name)
  note = notes.find {|note| note.name == name }
  note ? note : raise(Purse::MissingFile, "Could note find the note named #{note}")
end

#initObject



10
11
12
13
# File 'lib/purse/pocket.rb', line 10

def init
  FileUtils.mkdir_p(@path) unless exists?
  git
end

#load_notesObject



48
49
50
# File 'lib/purse/pocket.rb', line 48

def load_notes
  notes_paths.collect {|note_path| Note.load(File.dirname(note_path), File.basename(note_path, '.note'))}
end

#notesObject



40
41
42
# File 'lib/purse/pocket.rb', line 40

def notes
  @notes ||= load_notes
end

#notes_pathsObject



44
45
46
# File 'lib/purse/pocket.rb', line 44

def notes_paths
  Dir[File.join(@path, '*.note')]
end

#pullObject



77
78
79
# File 'lib/purse/pocket.rb', line 77

def pull
  git.pull('origin')
end

#pushObject



73
74
75
# File 'lib/purse/pocket.rb', line 73

def push
  git.push('origin')
end

#re_encrypt(password) ⇒ Object



52
53
54
55
# File 'lib/purse/pocket.rb', line 52

def re_encrypt(password)
  Purse.check_for_parameter('password', password)
  notes.collect {|n| n.save(password) }
end

#remoteObject



65
66
67
# File 'lib/purse/pocket.rb', line 65

def remote
  git.remotes.first
end

#set_remote(remote_url) ⇒ Object



69
70
71
# File 'lib/purse/pocket.rb', line 69

def set_remote(remote_url)
  git.add_remote('origin', remote_url)
end