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 |
# File 'app/volt/tasks/store_tasks.rb', line 5 def initialize(channel=nil, dispatcher=nil) @channel = channel @dispatcher = dispatcher end |
Instance Method Details
#db ⇒ Object
10 11 12 |
# File 'app/volt/tasks/store_tasks.rb', line 10 def db @@db ||= Volt::DataStore.fetch end |
#delete(collection, id) ⇒ Object
64 65 66 67 68 |
# File 'app/volt/tasks/store_tasks.rb', line 64 def delete(collection, id) db[collection].remove('_id' => id) QueryTasks.live_query_pool.updated_collection(collection, @channel) end |
#model_errors(collection, data) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'app/volt/tasks/store_tasks.rb', line 14 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
31 32 33 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 |
# File 'app/volt/tasks/store_tasks.rb', line 31 def save(collection, data) data = data.symbolize_keys errors = model_errors(collection, data) if errors.size == 0 # id = BSON::ObjectId(data[:_id]) id = data[:_id] # Try to create # TODO: Seems mongo is dumb and doesn't let you upsert with custom id's begin # data['_id'] = BSON::ObjectId('_id') if data['_id'] 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 |