Sha256: 8a024c023724e6745c25a183d671d036cde6e5ea23b7ff8fd6bab9b725ee5654

Contents?: true

Size: 853 Bytes

Versions: 5

Compression:

Stored size: 853 Bytes

Contents

require 'adapter'
require 'mongo'

module Adapter
  module Mongo
    NonHashValueKeyName = '_value'

    def read(key)
      if doc = client.find_one('_id' => key_for(key))
        decode(doc)
      end
    end

    def write(key, value)
      client.save({'_id' => key_for(key)}.merge(encode(value)), {:safe => options[:safe]})
    end

    def delete(key)
      read(key).tap { client.remove({'_id' => key_for(key)}, {:safe => options[:safe]}) }
    end

    def clear
      client.remove
    end

    def key_for(key)
      key.is_a?(BSON::ObjectId) ? key : super
    end

    def encode(value)
      value.is_a?(Hash) ? value : {NonHashValueKeyName => value}
    end

    def decode(value)
      return if value.nil?
      value.key?(NonHashValueKeyName) ? value[NonHashValueKeyName] : value
    end
  end
end

Adapter.define(:mongo, Adapter::Mongo)

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
adapter-mongo-0.5.5 lib/adapter/mongo.rb
adapter-mongo-0.5.4 lib/adapter/mongo.rb
adapter-mongo-0.5.3 lib/adapter/mongo.rb
adapter-mongo-0.5.2 lib/adapter/mongo.rb
adapter-mongo-0.5.1 lib/adapter/mongo.rb