Created
April 12, 2012 20:16
-
-
Save mrgenixus/2370699 to your computer and use it in GitHub Desktop.
how do you solve this
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
#!/usr/bin/env ruby | |
require 'mongo' | |
require 'bson' | |
require 'json' | |
class Datastore | |
@db = nil | |
def initialize (database) | |
@dbname = database | |
end | |
def get_object(type, id) | |
{:id => id} | |
end | |
def get_object_list(type,spec = {}) | |
Hash.new(:info => Hash.new ) | |
end | |
def create_object(type) | |
{} | |
end | |
def update_object(type, object_hash, keys = []) | |
{} | |
end | |
end | |
class MongoDatastore < Datastore | |
def get_object(type, id) | |
@db.collection(type).find_one(:_id =>id). | |
end | |
def get_object_list(type,spec = {} ) | |
@db.collection(type).find(spec) | |
end | |
def create_object(type) | |
get_object(type,@db.collection(type).insert({})) | |
end | |
def update_object(type,object_hash,keys = [:_id] ) | |
search_hash = {} | |
keys.each { |key| | |
{key => object_hash[key]} if object_hash[key] | |
puts key, object_hash[key] | |
} | |
object_hash = @db.collection(type).update(search_hash,object_hash) | |
create_object(type).merge!(object_hash) unless object_hash[:_id] | |
end | |
def initialize(database) | |
super(database) | |
@db = Mongo::Connection.new.db(@dbname) | |
end | |
end | |
end | |
class User < Hash | |
def initialize(uniq_id) | |
self[:uniq_id] = uniq_id | |
end | |
def attend_event(event) | |
self[:attending] = event | |
end | |
end | |
class Event < Hash | |
def initialize (user) | |
self[:creator] = user | |
user.attend_event(self) | |
end | |
end | |
bob = User.new('bob@email.com') | |
myfavoritesportsteam = Event.new(bob) | |
store = MongoDatastore.new('play') | |
store.update_object('user',bob) | |
store.update_object('event',myfavoritesportsteam) | |
puts "After Store" | |
puts bob.to_json, myfavoritesportsteam.to_json | |
puts store.get_object_list('user').to_json | |
puts store.get_object_list('event').to_json |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment