Sha256: 5c2993397ee253314f4b8ef33c980cb3fe57d11f9c4a2cf1ef26e4f9d6261379

Contents?: true

Size: 1003 Bytes

Versions: 5

Compression:

Stored size: 1003 Bytes

Contents

require 'mongo'
require 'uri'

module Copy
  module Storage
    class Mongodb
      def initialize(connection_url)
        uri         = URI.parse(connection_url)
        connection  = ::Mongo::Connection.from_uri(connection_url)
        database    = connection.db(uri.path.gsub(/^\//, ''))
        
        @collection = database['copy-content']
        @collection.ensure_index([['name', Mongo::ASCENDING]], :unique => true)
        @collection
      end
      
      def get(name)
        doc = find(name)
        doc['content'] unless doc.nil?
      end
      
      def set(name, content)
        doc = find(name)
        if doc
          doc['content'] = content
          @collection.update({ '_id' => doc['_id'] }, doc)
        else
          @collection.insert('name' => name, 'content' => content)
        end
      end
      
      private
        def find(name)
          docs = @collection.find('name' => name)
          docs.first if docs.respond_to?(:first)
        end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
copy-0.0.41 lib/copy/storage/mongodb.rb
copy-0.0.4 lib/copy/storage/mongodb.rb
copy-0.0.3 lib/copy/storage/mongodb.rb
copy-0.0.2 lib/copy/storage/mongodb.rb
copy-0.0.1 lib/copy/storage/mongodb.rb