Sha256: dc89115bdb7998969ec00d7066d1ad9c7192fb888b6e59f27d79e4977ac07dac

Contents?: true

Size: 1.99 KB

Versions: 1

Compression:

Stored size: 1.99 KB

Contents

module Candygram
  
  # The MongoDB connection object. Creates a default connection to localhost if not explicitly told otherwise.
  def self.connection
    @connection ||= Mongo::Connection.new
  end
  
  # Accepts a new MongoDB connection, closing any current ones
  def self.connection=(val)
    @connection.close if @connection
    @connection = val
  end
  
  # The Mongo database object.  If you just want the name, use #database instead.
  def self.db
    @db ||= Mongo::DB.new(DEFAULT_DATABASE, connection)
  end
  
  # Sets the Mongo database object. Unless you want to pass specific options or bypass the 
  # Candygram connection object completely, it's probably easier to use the #database= method 
  # and give it the name.
  def self.db=(val)
    @db = val
  end
  
  # The name of the Mongo database object.
  def self.database
    db.name
  end
  
  # Creates a Mongo database object with the given name and default options.
  def self.database=(val)
    self.db = Mongo::DB.new(val, connection)
  end
  
  # The delivery queue collection. If not set, creates a capped collection with a default
  # name of 'candygram_queue' and a default cap size of 100MB.
  def self.queue
    @queue or begin
      if db.collection_names.include?(DEFAULT_QUEUE)
        @queue = db[DEFAULT_QUEUE]
      else
        @queue = create_queue
      end
    end
  end
  
  # Sets the delivery queue to an existing collection.  Assumes you know what you're doing 
  # and have made all the proper indexes and such.  If not, use the #create_queue method instead.
  def self.queue=(val)
    @queue = val
  end
  
  # Creates a new capped collection with the given name and cap size, and sets the indexes needed
  # for efficient Candygram delivery.
  def self.create_queue(name=DEFAULT_QUEUE, size=DEFAULT_QUEUE_SIZE)
    @queue = db.create_collection(name, :capped => true, :size => size)
    # Make indexes here...
    @queue.create_index('deliver_at')
    @queue.create_index('locked')
    @queue.create_index('result')
    @queue
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
candygram-0.1.0 lib/candygram/connection.rb