Sha256: 0fdbe5fb206cea57bf08c943b76bcc6a5f42c8276a4189a509a498b83dbe96fc

Contents?: true

Size: 1.34 KB

Versions: 2

Compression:

Stored size: 1.34 KB

Contents

require 'hammerspace'

##
# Hammerspace-backed Store designed for BasicCache
module HammerStore
  class << self
    ##
    # Insert a helper .new() method for creating a new Store object

    def new(*args)
      self::Store.new(*args)
    end
  end

  ##
  # Hammerspace-backed store object
  class Store
    attr_reader :raw, :file

    ##
    # Generate an empty store

    def initialize(params = {})
      @file = params[:file] || fail('You must specify a file')
      @raw = Hammerspace.new(@file)
    end

    ##
    # Clears a specified key or the whole store

    def clear!(key = nil)
      if key.nil?
        @raw.clear && {}
      else
        key = prep(key)
        value = @raw[key]
        @raw.delete key
        parse value
      end
    end

    ##
    # Retrieve a key

    def [](key)
      parse @raw[prep(key)]
    end

    ##
    # Set a key

    def []=(key, value)
      @raw[prep(key)] = prep(value)
    end

    ##
    # Return the size of the store

    def size
      @raw.size
    end

    ##
    # Check for a key in the store

    def include?(key)
      @raw.key? prep(key)
    end

    ##
    # Array of keys in the store

    def keys
      @raw.keys.map { |x| parse x }
    end

    private

    def prep(object)
      Marshal.dump object
    end

    def parse(object)
      object.nil? ? nil : Marshal.load(object)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hammerstore-1.0.0 lib/hammerstore.rb
hammerstore-0.0.2 lib/hammerstore.rb