Sha256: 8814b907918d6ab3f4cd5a9e740337ea741366cc34aa90a34de307f23dca56c4

Contents?: true

Size: 1.97 KB

Versions: 3

Compression:

Stored size: 1.97 KB

Contents

require 'pstore'
require 'fileutils'

module Moneta
  module Adapters
    # PStore backend
    # @api public
    class PStore
      include Defaults

      supports :create, :increment
      attr_reader :backend

      # @param [Hash] options
      # @option options [String] :file PStore file
      # @option options [::PStore] :backend Use existing backend instance
      def initialize(options = {})
        @backend = options[:backend] ||
          begin
            raise ArgumentError, 'Option :file is required' unless options[:file]
            FileUtils.mkpath(::File.dirname(options[:file]))
            new_store(options)
          end
      end

      # (see Proxy#key?)
      def key?(key, options = {})
        @backend.transaction(true) { @backend.root?(key) }
      end

      # (see Proxy#load)
      def load(key, options = {})
        @backend.transaction(true) { @backend[key] }
      end

      # (see Proxy#store)
      def store(key, value, options = {})
        @backend.transaction { @backend[key] = value }
      end

      # (see Proxy#delete)
      def delete(key, options = {})
        @backend.transaction { @backend.delete(key) }
      end

      # (see Proxy#increment)
      def increment(key, amount = 1, options = {})
        @backend.transaction do
          value = Utils.to_int(@backend[key]) + amount
          @backend[key] = value.to_s
          value
        end
      end

      # (see Proxy#create)
      def create(key, value, options = {})
        @backend.transaction do
          if @backend.root?(key)
            false
          else
            @backend[key] = value
            true
          end
        end
      end

      # (see Proxy#clear)
      def clear(options = {})
        @backend.transaction do
          @backend.roots.each do |key|
            @backend.delete(key)
          end
        end
        self
      end

      protected

      def new_store(options)
        ::PStore.new(options[:file], options[:threadsafe])
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
moneta-1.0.0 lib/moneta/adapters/pstore.rb
moneta-0.8.1 lib/moneta/adapters/pstore.rb
moneta-0.8.0 lib/moneta/adapters/pstore.rb