Sha256: 0e3e0ea78a1b2116c85fc399cb09d4548569b25b6a0cfdbfbecfbe8758604ccc

Contents?: true

Size: 987 Bytes

Versions: 10

Compression:

Stored size: 987 Bytes

Contents

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

10 entries across 10 versions & 2 rubygems

Version Path
copy_no_dependency-0.2.0 lib/copy/storage/mongodb.rb
copy-0.2.0 lib/copy/storage/mongodb.rb
copy-0.1.7 lib/copy/storage/mongodb.rb
copy-0.1.6 lib/copy/storage/mongodb.rb
copy-0.1.5 lib/copy/storage/mongodb.rb
copy-0.1.4 lib/copy/storage/mongodb.rb
copy-0.1.3 lib/copy/storage/mongodb.rb
copy-0.1.2 lib/copy/storage/mongodb.rb
copy-0.1.1 lib/copy/storage/mongodb.rb
copy-0.1.0 lib/copy/storage/mongodb.rb