Sha256: 081cef3affbb5eea3b542f3c555e88dc98220c04c2394be21e2beb4c4e752472

Contents?: true

Size: 727 Bytes

Versions: 5

Compression:

Stored size: 727 Bytes

Contents

module Dcha
  module Store
    # :nodoc:
    class File < Hash
      def initialize(path)
        @path = Pathname.new(path).realdirpath
        Dir.mkdir(path) unless Dir.exist?(path)
      end

      def [](key)
        super || load_from(key)
      end

      def []=(key, value)
        save_to(key, value)
        super
      end

      def clear!
        FileUtils.rm_rf(path)
      end

      private

      def load_from(key)
        path = "#{@path}/#{key}"
        raise DataUnavailableError, key unless File.exist?(path)
        self[key] = File.read(path)
      end

      def save_to(key, value)
        path = "#{@path}/#{key}"
        File.write(path, value) unless File.exist?(path)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dcha-0.1.4 lib/dcha/store/file.rb
dcha-0.1.3 lib/dcha/store/file.rb
dcha-0.1.2 lib/dcha/store/file.rb
dcha-0.1.1 lib/dcha/store/file.rb
dcha-0.1.0 lib/dcha/store/file.rb