Sha256: ff19aaba706dd9efb30ffb7a4663c1088c46f5c482a3227164827ad8b31b88dc

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

require "blobby/key_constraint"

module Blobby

  # A BLOB store backed by a Hash.
  #
  class InMemoryStore

    def self.from_uri(_uri)
      new
    end

    def initialize(hash = {})
      @hash = hash
    end

    def available?
      true
    end

    def [](key)
      KeyConstraint.must_allow!(key)
      StoredObject.new(@hash, key)
    end

    class StoredObject

      def initialize(hash, key)
        @hash = hash
        @key = key
      end

      attr_reader :key

      def exists?
        @hash.key?(key)
      end

      def read
        content = @hash[key]
        if block_given?
          yield content
          nil
        else
          content
        end
      end

      def write(content)
        if content.respond_to?(:read)
          content = content.read
        else
          content = content.to_str.dup
        end
        content = content.force_encoding("BINARY") if content.respond_to?(:force_encoding)
        @hash[key] = content
        nil
      end

      def delete
        !@hash.delete(key).nil?
      end

    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
blobby-1.1.0 lib/blobby/in_memory_store.rb