Sha256: f5db4b6428245260a1c245cf52f0393e7cc8ac352db408c55a16ebe48a6dcfdb

Contents?: true

Size: 1.95 KB

Versions: 2

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

require "json"

require "key_vortex/adapter"
require "key_vortex/record"
require "stashify/file"

class KeyVortex
  class Adapter
    # Implements a {https://rubydoc.info/gems/key-vortex/KeyVortex/Adapter KeyVortex Adapter}
    # for {https://rubydoc.info/gems/stashify/Stashify/Directory Stashify Directories}.
    # Sometimes file storage is all that's needed, going fancier just
    # isn't necessary. By supporting Stashify's API, development can be
    # done against local storage, but production systems can use the
    # Blob storage for whatever provider makes the most sense.
    class Stashify < KeyVortex::Adapter
      # @param stashify [Stashify::Directory] The Stashify Directory which should be used
      # @return [KeyVortex::Adapter::Stashify]
      def self.build(stashify:)
        new(stashify)
      end

      # @param stashify [Stashify::Directory] The Stashify Directory which should be used
      def initialize(stashify)
        super()
        @stashify = stashify
      end

      # Write a record to the directory with the filename record.key
      # and a json version of the record as its contents.
      # @param record [KeyVortex::Record] The record to be saved
      def save(record)
        @stashify.write(
          ::Stashify::File.new(
            name: record.key,
            contents: JSON.generate(record)
          )
        )
      end

      # Load the file with filename key, assuming that the contents of
      # the file are a json version of the record.
      # @param key [String]
      # @return [KeyVortex::Record]
      # @return [nil] if the file does not exist
      def find(key)
        return unless @stashify.exists?(key)

        JSON.parse(@stashify.find(key).contents, create_additions: true)
      end

      # Delete the file with filename key
      # @param key [String]
      def remove(key)
        @stashify.delete(key)
      end
    end
  end
end

KeyVortex.register(KeyVortex::Adapter::Stashify)

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
key_vortex-stashify-1.1.0 lib/key_vortex/adapter/stashify.rb
key_vortex-stashify-1.0.0 lib/key_vortex/adapter/stashify.rb