Class: StoreTasks
Instance Method Summary collapse
- #db ⇒ Object
- #find(collection, scope, query = nil) ⇒ Object
-
#initialize(channel = nil, dispatcher = nil) ⇒ StoreTasks
constructor
A new instance of StoreTasks.
- #save(collection, data) ⇒ Object
Constructor Details
#initialize(channel = nil, dispatcher = nil) ⇒ StoreTasks
5 6 7 8 9 10 11 |
# File 'app/volt/tasks/store_tasks.rb', line 5 def initialize(channel=nil, dispatcher=nil) @@mongo_db ||= Mongo::MongoClient.new("localhost", 27017) @@db ||= @@mongo_db.db("development") @channel = channel @dispatcher = dispatcher end |
Instance Method Details
#db ⇒ Object
13 14 15 |
# File 'app/volt/tasks/store_tasks.rb', line 13 def db @@db end |
#find(collection, scope, query = nil) ⇒ Object
43 44 45 46 47 48 |
# File 'app/volt/tasks/store_tasks.rb', line 43 def find(collection, scope, query=nil) results = @@db[collection].find(scope).to_a.map {|item| item.symbolize_keys } puts "FIND: #{collection.inspect} - #{scope} - #{results.inspect}" return results end |
#save(collection, data) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'app/volt/tasks/store_tasks.rb', line 17 def save(collection, data) puts "Insert: #{data.inspect} on #{collection.inspect}" # Try to create # TODO: Seems mongo is dumb and doesn't let you upsert with custom id's begin @@db[collection].insert(data) id = {'_id' => data.delete('_id')} # Message that we inserted a new item ChannelTasks.("#{collection}", ['added', nil, collection, data.merge('_id' => id).symbolize_keys], @channel) rescue Mongo::OperationFailure => error # Really mongo client? if error.[/^11000[:]/] # Update because the id already exists id = {'_id' => data.delete('_id')} @@db[collection].update(id, data) else raise end end id = id['_id'] ChannelTasks.("#{collection}##{id}", ['changed', nil, id, data.merge('_id' => id).symbolize_keys], @channel) end |