Created
March 18, 2010 00:20
-
-
Save tkareine/335904 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "pstore" | |
module Notes | |
class << self | |
attr_accessor :store_location | |
def put_note(id, note) | |
store.transaction do | |
store[:notes] ||= {} | |
store[:notes][id] = note | |
end | |
end | |
def delete_note(id) | |
store.transaction do | |
store[:notes] ||= {} | |
store[:notes].delete(id) | |
end | |
end | |
def notes | |
store.transaction do | |
store[:notes] ||= {} | |
end | |
end | |
def clear_notes | |
store.transaction do | |
store[:notes] = {} | |
end | |
end | |
private | |
def store | |
@store ||= begin | |
raise "Notes store location is not defined" unless store_location | |
PStore.new(store_location) | |
end | |
end | |
end | |
end | |
Notes.store_location = "notes.pstore" | |
Notes.notes #=> {} | |
Notes.put_note(1, "fooman") #=> "fooman" | |
Notes.put_note(2, "barbaz") #=> "barbaz" | |
Notes.notes #=> {1=>"fooman", 2=>"barbaz"} | |
Notes.delete_note(1) #=> "fooman" | |
Notes.notes[2] #=> "barbaz" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment