Class: StoreTasks
Instance Method Summary collapse
- #db ⇒ Object
- #delete(collection, id) ⇒ Object
-
#initialize(channel = nil, dispatcher = nil) ⇒ StoreTasks
constructor
A new instance of StoreTasks.
- #model_errors(collection, data) ⇒ Object
- #save(collection, data) ⇒ Object
Constructor Details
#initialize(channel = nil, dispatcher = nil) ⇒ StoreTasks
Returns a new instance of 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 |
#delete(collection, id) ⇒ Object
65 66 67 68 69 |
# File 'app/volt/tasks/store_tasks.rb', line 65 def delete(collection, id) @@db[collection].remove('_id' => id) QueryTasks.live_query_pool.updated_collection(collection, @channel) end |
#model_errors(collection, data) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'app/volt/tasks/store_tasks.rb', line 17 def model_errors(collection, data) model_name = collection[1..-1].singularize.camelize # TODO: Security check to make sure we have a valid model begin model_class = Object.send(:const_get, model_name) rescue NameError => e model_class = nil end if model_class return model_class.new(data).errors end return {} end |
#save(collection, data) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'app/volt/tasks/store_tasks.rb', line 34 def save(collection, data) data = data.symbolize_keys errors = model_errors(collection, data) if errors.size == 0 id = data[:_id] # Try to create # TODO: Seems mongo is dumb and doesn't let you upsert with custom id's begin @@db[collection].insert(data) rescue Mongo::OperationFailure => error # Really mongo client? if error.[/^11000[:]/] # Update because the id already exists update_data = data.dup update_data.delete(:_id) @@db[collection].update({:_id => id}, update_data) else return {:error => error.} end end QueryTasks.live_query_pool.updated_collection(collection, @channel) return {} else return errors end end |