Sha256: 5f0b8360beb74c1e9f8ac8a62ddd9b81cb4d4b684d4df7d969a7678b190b79b7

Contents?: true

Size: 1.42 KB

Versions: 20

Compression:

Stored size: 1.42 KB

Contents

module Moneta
  module Expires
    def check_expired(key)
      if @expiration[key] && Time.now > @expiration[key]
        @expiration.delete(key)
        self.delete(key)
      end
    end

    def key?(key)
      check_expired(key)
      super
    end

    def [](key)
      check_expired(key)
      super
    end

    def fetch(key, default = nil, &blk)
      check_expired(key)
      super
    end

    def delete(key)
      check_expired(key)
      super
    end

    def update_key(key, options)
      update_options(key, options)
    end

    def store(key, value, options = {})
      ret = super(key, value)
      update_options(key, options)
      ret
    end

    private
    def update_options(key, options)
      if options[:expires_in]
        @expiration[key] = (Time.now + options[:expires_in])
      end
    end
  end

  module StringExpires
    include Expires

    def check_expired(key)
      if @expiration[key] && Time.now > Time.at(@expiration[key].to_i)
        @expiration.delete(key)
        delete(key)
      end
    end

    private
    def update_options(key, options)
      if options[:expires_in]
        @expiration[key] = (Time.now + options[:expires_in]).to_i.to_s
      end
    end
  end
  
  module Defaults
    def fetch(key, value = nil)
      value ||= block_given? ? yield(key) : default
      self[key] || value
    end
    
    def store(key, value, options = {})
      self[key] = value
    end        
  end
end

Version data entries

20 entries across 20 versions & 9 rubygems

Version Path
aeden-moneta-0.6.2 lib/moneta.rb
aeden-moneta-0.6.3 lib/moneta.rb
benschwarz-moneta-0.6.0 lib/moneta.rb
benschwarz-moneta-0.6.1 lib/moneta.rb
benschwarz-smoke-0.5.0 vendor/moneta/lib/moneta.rb
benschwarz-smoke-0.5.1 vendor/moneta/lib/moneta.rb
benschwarz-smoke-0.5.2 vendor/moneta/lib/moneta.rb
benschwarz-smoke-0.5.3 vendor/moneta/lib/moneta.rb
benschwarz-smoke-0.5.4 vendor/moneta/lib/moneta.rb
benschwarz-smoke-0.5.5 vendor/moneta/lib/moneta.rb
benschwarz-smoke-0.5.6 vendor/moneta/lib/moneta.rb
benschwarz-smoke-0.5.7 vendor/gems/gems/wycats-moneta-0.6.0/lib/moneta.rb
jferris-moneta-0.6.2 lib/moneta.rb
jsl-moneta-0.6.1 lib/moneta.rb
jsl-moneta-0.6.2 lib/moneta.rb
jsl-moneta-0.6.3 lib/moneta.rb
smurphy-moneta-0.6.0 lib/moneta.rb
wycats-moneta-0.6.0 lib/moneta.rb
gotime-moneta-0.6.2 lib/moneta.rb
moneta-0.6.0 lib/moneta.rb