Class: Purse::Note

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, name, data, options = {}) ⇒ Note

Returns a new instance of Note.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/purse/note.rb', line 6

def initialize(path, name, data, options = {})
  Purse.check_for_parameter('path', path)
  Purse.check_for_parameter('name', name)
  @path = path
  @name = name
  @options = options
  if options[:encrypted]
    @encrypted = data
  else
    @data = data
  end
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

#encryptedObject (readonly)

Returns the value of attribute encrypted.



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

def encrypted
  @encrypted
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

Class Method Details

.load(path, name, options = {}) ⇒ Object

Raises:



19
20
21
22
23
24
# File 'lib/purse/note.rb', line 19

def self.load(path, name, options = {})
  note = Note.new(path, name, nil)
  raise MissingFile, "Tried to load #{note.file_path} but it does not exist" unless File.readable?(note.file_path)
  encrypted_data = File.read(note.file_path)
  Note.new(path, name, encrypted_data, options.merge(:encrypted => true))
end

Instance Method Details

#decrypt(password) ⇒ Object



39
40
41
42
43
# File 'lib/purse/note.rb', line 39

def decrypt(password)
  Purse.check_for_parameter('password', password)
  blowfish = Crypt::Blowfish.new(password)
  @data = blowfish.decrypt_string(@encrypted)
end

#deleteObject



49
50
51
# File 'lib/purse/note.rb', line 49

def delete
  File.unlink(file_path)
end

#encrypt(password) ⇒ Object



33
34
35
36
37
# File 'lib/purse/note.rb', line 33

def encrypt(password)
  Purse.check_for_parameter('password', password)
  blowfish = Crypt::Blowfish.new(password)
  @encrypted = blowfish.encrypt_string(@data)
end

#encrypted?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/purse/note.rb', line 45

def encrypted?
  @encrypted && (!@data || @data.blank?)
end

#file_nameObject



57
58
59
# File 'lib/purse/note.rb', line 57

def file_name
  "#{name}.note"
end

#file_pathObject



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

def file_path
  File.join(path, file_name)
end

#save(password) ⇒ Object



26
27
28
29
30
31
# File 'lib/purse/note.rb', line 26

def save(password)
  Purse.check_for_parameter('password', password)
  encrypt(password)
  File.open(file_path, 'w') {|f| f << @encrypted }
  self
end