Sha256: 135fa6385050208a02e3cb589ec2eb6fa130d49b7aef1a8820eff623e3541a87

Contents?: true

Size: 1.23 KB

Versions: 3

Compression:

Stored size: 1.23 KB

Contents

require 'mongo'
require_relative 'channel_tasks'

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}"
    # 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')}
    rescue Mongo::OperationFailure => error
      # Really mongo client?
      if error.message[/^11000[:]/]
        # Update because the id already exists
        id = {'_id' => data.delete('_id')}
        @@db[collection].update(id, data)
      else
        raise
      end
    end
    
    id = id['_id']
    # ChannelHandler.send_message_all(@channel, 'update', nil, id, data.merge('_id' => id))
    
    ChannelTasks.send_message_to_channel("#{collection}##{id}", ['update', nil, id, data.merge('_id' => id)], @channel)
  end
  
  def find(collection, scope, query=nil)
    puts "FIND: #{collection.inspect} - #{scope}"
    return @@db[collection].find(scope).to_a
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
volt-0.4.0 app/volt/tasks/store_tasks.rb
volt-0.3.9 app/volt/tasks/store_tasks.rb
volt-0.3.8 app/volt/tasks/store_tasks.rb