Sha256: 996e237f691bbc443f55a68b9f261b9a7fa945749e66ef2ca80132e554c5567d

Contents?: true

Size: 1.1 KB

Versions: 2

Compression:

Stored size: 1.1 KB

Contents

require 'set'
require 'forwardable'
require 'mongo'
require 'flipper/adapters/mongo/document'

module Flipper
  module Adapters
    class MongoSingleDocument
      extend Forwardable

      def initialize(collection, options = {})
        @collection = collection
        @options = options
        @document_cache = false
      end

      def_delegators :document, :read, :write, :delete, :set_members, :set_add, :set_delete

      def using_document_cache?
        @document_cache == true
      end

      def document_cache=(value)
        reset_document_cache
        @document_cache = value
      end

      def use_document_cache(&block)
        original = @document_cache
        @document_cache = true
        yield
      ensure
        @document_cache = original
      end

      def reset_document_cache
        @document = nil
      end

      private

      def document
        if @document_cache == true
          @document ||= fresh_document
        else
          fresh_document
        end
      end

      def fresh_document
        Document.new(@collection, :id => @options[:id])
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
flipper-mongo-0.3.0 lib/flipper/adapters/mongo_single_document.rb
flipper-mongo-0.2.2 lib/flipper/adapters/mongo_single_document.rb