Class: Purse::Note
- Inherits:
-
Object
- Object
- Purse::Note
- Defined in:
- lib/purse/note.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#encrypted ⇒ Object
readonly
Returns the value of attribute encrypted.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
- #decrypt(password) ⇒ Object
- #delete ⇒ Object
- #encrypt(password) ⇒ Object
- #encrypted? ⇒ Boolean
- #file_name ⇒ Object
- #file_path ⇒ Object
-
#initialize(path, name, data, options = {}) ⇒ Note
constructor
A new instance of Note.
- #save(password) ⇒ Object
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, = {}) Purse.check_for_parameter('path', path) Purse.check_for_parameter('name', name) @path = path @name = name @options = if [:encrypted] @encrypted = data else @data = data end end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
4 5 6 |
# File 'lib/purse/note.rb', line 4 def data @data end |
#encrypted ⇒ Object (readonly)
Returns the value of attribute encrypted.
3 4 5 |
# File 'lib/purse/note.rb', line 3 def encrypted @encrypted end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/purse/note.rb', line 3 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/purse/note.rb', line 3 def @options end |
#path ⇒ Object (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
19 20 21 22 23 24 |
# File 'lib/purse/note.rb', line 19 def self.load(path, name, = {}) 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, .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 |
#delete ⇒ Object
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
45 46 47 |
# File 'lib/purse/note.rb', line 45 def encrypted? @encrypted && (!@data || @data.blank?) end |
#file_name ⇒ Object
57 58 59 |
# File 'lib/purse/note.rb', line 57 def file_name "#{name}.note" end |
#file_path ⇒ Object
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 |