Sha256: 06ebd69426f16160dd537bb65966d895e55c286222be2ee346b962bb0889f804

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

module Blobby

  # A store decorator that logs writes and deletes
  #
  class LoggingStore

    def initialize(store, store_name, logger)
      @store = store
      @store_name = store_name
      @logger = logger
    end

    def available?
      store.available?
    end

    def [](key)
      StoredObject.new(
        store[key],
        :on_write => -> { logger.info(%(wrote to #{key.inspect} in #{store_name})) },
        :on_delete => -> { logger.info(%(deleted #{key.inspect} from #{store_name})) }
      )
    end

    private

    attr_reader :logger
    attr_reader :store
    attr_reader :store_name

    class StoredObject

      def initialize(object, callbacks = {})
        @object = object
        @on_write = callbacks[:on_write] || -> {}
        @on_delete = callbacks[:on_delete] || -> {}
      end

      def exists?
        @object.exists?
      end

      def read(&block)
        @object.read(&block)
      end

      def write(*args)
        @object.write(*args)
        @on_write.call
        nil
      end

      def delete
        deleted = @object.delete
        @on_delete.call if deleted
        deleted
      end

      private

      attr_reader :object
      attr_reader :on_write

    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

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