Skip to content

Instantly share code, notes, and snippets.

Created January 4, 2013 01:32
Show Gist options
  • Save anonymous/4449173 to your computer and use it in GitHub Desktop.
Save anonymous/4449173 to your computer and use it in GitHub Desktop.
require './uuid'
require 'json'
class Store
def initialize(path)
@path = path
@objects = {}
end
def save(object)
object.id = UUID.create_random.to_s unless object.id
@objects[object.id] = object
file_path = File.join(@path, object.id)
File.open(file_path, 'w') do |file|
json_object = serialize(object)
json = json_object.to_json
file.write(json)
end
return object.id
end
def load(object_id)
if @objects[object_id]
@objects[object_id]
else
file_path = File.join(@path, object_id)
File.open(file_path, 'r') do |file|
return deserialize(file.read())
end
end
end
def unload(object)
@objects.delete(object.id)
end
private
def serialize(object, primary=true)
if object.is_a?(String) \
|| object.is_a?(Integer) \
|| object.is_a?(Float) \
|| object.is_a?(TrueClass) \
|| object.is_a?(FalseClass) \
|| object.is_a?(NilClass)
return object
elsif primary == false
return save(object)
end
dictionary = {}
methods = object.public_methods(false)
methods.delete_if do |method|
method == :id || !(method.to_s =~ /^[a-zA-Z0-9\-]+$/)
end
methods.each do |method|
obj = object.send(method)
dictionary[method] = serialize(obj, false)
end
return { :object => dictionary }
end
def deserialize(blob)
# TODO: Implement this crap.
end
end
class Proxy
def initialize(object_id, store)
@object_id = object_id
@store = store
end
def method_missing(name, *args)
obj = @store.load(@object_id)
obj.send(name, *args)
end
end
class Person
attr_accessor :id, :name, :age, :parent
def initialize(name, age, parent = nil)
self.name = name
self.age = age
self.parent = parent
end
end
store = Store.new('./foo')
zoidberg = Person.new('Zoidberg', 18)
store.save(zoidberg)
ell = Person.new('Ell', 2, zoidberg)
store.save(ell)
sbi = Person.new('sbi', 800)
store.save(sbi)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment