Sha256: c05ad4c794830799335efe789cb63572419b2c6d8dc74c1b2c26708cf8da2545

Contents?: true

Size: 1.16 KB

Versions: 6

Compression:

Stored size: 1.16 KB

Contents

require 'mongo'

class StoreTasks
  def initialize(channel=nil, dispatcher=nil)
    @@mongo_db ||= Mongo::MongoClient.new("localhost", 27017)
    @@db ||= @@mongo_db.db("development")
    
    @channel = channel
    @dispatcher = dispatcher
  end
  
  def db
    @@db
  end
  
  def save(collection, data)
    # puts "Insert: #{data.inspect} on #{collection.inspect}"
    
    data = data.symbolize_keys
    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.message[/^11000[:]/]
        # Update because the id already exists
        update_data = data.dup
        update_data.delete(:_id)
        @@db[collection].update({:_id => id}, update_data)
      else
        raise
      end
    end
    
    QueryTasks.live_query_pool.updated_collection(collection, @channel)
  end

  def delete(collection, id)
    puts "DELETE: #{collection.inspect} - #{id.inspect}"
    @@db[collection].remove('_id' => id)
    
    QueryTasks.live_query_pool.updated_collection(collection, @channel)
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
volt-0.6.5 app/volt/tasks/store_tasks.rb
volt-0.6.4 app/volt/tasks/store_tasks.rb
volt-0.6.3 app/volt/tasks/store_tasks.rb
volt-0.6.2 app/volt/tasks/store_tasks.rb
volt-0.6.1 app/volt/tasks/store_tasks.rb
volt-0.6.0 app/volt/tasks/store_tasks.rb