Skip to content

Instantly share code, notes, and snippets.

@ybur-yug
Created June 16, 2015 21:27
Show Gist options
  • Save ybur-yug/03da94f54f8df7244c3b to your computer and use it in GitHub Desktop.
Save ybur-yug/03da94f54f8df7244c3b to your computer and use it in GitHub Desktop.
module Gkv
class Database
attr_accessor :items
def initialize
`git init`
@items = {}
end
def set(key, value)
@items[key] = hash_object(value)
#@items[key] = [hash_object(value)]
end
def get(key)
cat_file(@items[key])
#cat_file(@items[key.to_s]).strip!
end
def hash_object(data)
write_tmpfile(data)
hash = `git hash-object -w tmp.txt`.strip!
File.delete('tmp.txt')
hash
end
def write_tmpfile(data)
f = File.open("tmp.txt", "w+")
f.write(data.to_s)
f.close
end
def cat_file(hash)
`git cat-file -p #{hash}`
end
end
end
db = Gkv::Database.new
db.set("Apples", "10")
puts db.get("Apples")
# => 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment