Sha256: c06c09a116c00a71cb810f703b95fbe293f34c85b06c6897130af59864873ebc

Contents?: true

Size: 773 Bytes

Versions: 15

Compression:

Stored size: 773 Bytes

Contents

require "digest/md5"

module Ruco
  class FileStore
    def initialize(folder, options)
      @folder = folder
      @options = options
    end

    def set(key, value)
      `mkdir -p #{@folder}` unless File.exist? @folder
      File.write(file(key), serialize(value))
      cleanup
    end

    def get(key)
      file = file(key)
      deserialize File.read(file) if File.exist?(file)
    end

    private

    def cleanup
      delete = `ls -t #{@folder}`.split("\n")[@options[:keep]..-1] || []
      delete.each{|f| File.delete("#{@folder}/#{f}") }
    end

    def file(key)
      "#{@folder}/#{Digest::MD5.hexdigest(key)}.yml"
    end

    def serialize(value)
      Marshal.dump(value)
    end

    def deserialize(value)
      Marshal.load(value)
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
ruco-0.0.56 lib/ruco/file_store.rb
ruco-0.0.55 lib/ruco/file_store.rb
ruco-0.0.54 lib/ruco/file_store.rb
ruco-0.0.53 lib/ruco/file_store.rb
ruco-0.0.52 lib/ruco/file_store.rb
ruco-0.0.51 lib/ruco/file_store.rb
ruco-0.0.50 lib/ruco/file_store.rb
ruco-0.0.49 lib/ruco/file_store.rb
ruco-0.0.48 lib/ruco/file_store.rb
ruco-0.0.47 lib/ruco/file_store.rb
ruco-0.0.46 lib/ruco/file_store.rb
ruco-0.0.45 lib/ruco/file_store.rb
ruco-0.0.44 lib/ruco/file_store.rb
ruco-0.0.43 lib/ruco/file_store.rb
ruco-0.0.42 lib/ruco/file_store.rb