Sha256: 688e7d0787920514b0c1dad0e3358ab3212785c71f28d6e496d6cd06c9d7c5d0

Contents?: true

Size: 1.91 KB

Versions: 12

Compression:

Stored size: 1.91 KB

Contents

begin
  require "xattr"
rescue LoadError
  puts "You need the xattr gem to use the File moneta store"
  exit
end
require "fileutils"

module Moneta
  class File
    class Expiration
      def initialize(directory)
        @directory = directory
      end
      
      def [](key)
        attrs = xattr(key)
        ret = Marshal.load(attrs.get("moneta_expires"))
      rescue Errno::ENOENT, SystemCallError
      end
      
      def []=(key, value)
        attrs = xattr(key)
        attrs.set("moneta_expires", Marshal.dump(value))
      end
      
      def delete(key)
        attrs = xattr(key)
        attrs.remove("moneta_expires")
      end

      private
      def xattr(key)
        ::Xattr.new(::File.join(@directory, key))
      end
    end
    
    def initialize(options = {})
      @directory = options[:path]
      if ::File.file?(@directory)
        raise StandardError, "The path you supplied #{@directory} is a file"
      elsif !::File.exists?(@directory)
        FileUtils.mkdir_p(@directory)
      end
      
      @expiration = Expiration.new(@directory)
    end
    
    module Implementation
      def key?(key)
        ::File.exist?(path(key))
      end
      
      alias has_key? key?
      
      def [](key)
        if ::File.exist?(path(key))
          Marshal.load(::File.read(path(key)))
        end
      end
      
      def []=(key, value)
        ::File.open(path(key), "w") do |file|
          contents = Marshal.dump(value)
          file.puts(contents)
        end
      end
            
      def delete(key)
        value = self[key]
        FileUtils.rm(path(key))
        value
      rescue Errno::ENOENT
      end
            
      def clear
        FileUtils.rm_rf(@directory)
        FileUtils.mkdir(@directory)
      end
      
      private
      def path(key)
        ::File.join(@directory, key.to_s)
      end
    end
    include Implementation
    include Defaults
    include Expires
    
  end
end

Version data entries

12 entries across 12 versions & 5 rubygems

Version Path
benschwarz-moneta-0.6.0 lib/moneta/file.rb
benschwarz-moneta-0.6.1 lib/moneta/file.rb
benschwarz-smoke-0.5.0 vendor/moneta/lib/moneta/file.rb
benschwarz-smoke-0.5.1 vendor/moneta/lib/moneta/file.rb
benschwarz-smoke-0.5.2 vendor/moneta/lib/moneta/file.rb
benschwarz-smoke-0.5.3 vendor/moneta/lib/moneta/file.rb
benschwarz-smoke-0.5.4 vendor/moneta/lib/moneta/file.rb
benschwarz-smoke-0.5.5 vendor/moneta/lib/moneta/file.rb
benschwarz-smoke-0.5.6 vendor/moneta/lib/moneta/file.rb
gotime-moneta-0.6.2 lib/moneta/file.rb
thoughtless-moneta-0.6.0.1 lib/moneta/file.rb
moneta-0.6.0 lib/moneta/file.rb