Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tkareine/335904 to your computer and use it in GitHub Desktop.
Save tkareine/335904 to your computer and use it in GitHub Desktop.
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