Sha256: 0e6a21797108775db3e92e5493288934128d22b128625cbd63a0545d6fcef9d9

Contents?: true

Size: 1.11 KB

Versions: 2

Compression:

Stored size: 1.11 KB

Contents

class Redis
  # Defines base functionality for all redis-objects.
  class BaseObject
    def initialize(key, *args)
      @key     = key.is_a?(Array) ? key.flatten.join(':') : key
      @options = args.last.is_a?(Hash) ? args.pop : {}
      @myredis = args.first
    end

    # Dynamically query the handle to enable resetting midstream
    def redis
      @myredis || ::Redis::Objects.redis
    end

    alias :inspect :to_s  # Ruby 1.9.2

    def set_expiration
      if !@options[:expiration].nil?
        redis.expire(@key, @options[:expiration]) if redis.ttl(@key) < 0
      elsif !@options[:expireat].nil?
        redis.expireat(@key, @options[:expireat].to_i) if redis.ttl(@key) < 0
      end
    end

    class << self
      def expiration_filter(*names)
        names.each do |name|
          # http://blog.jayfields.com/2006/12/ruby-alias-method-alternative.html
          bind_method = instance_method(name)

          define_method(name) do |*args, &block|
            result = bind_method.bind(self).call(*args, &block)
            set_expiration
            result
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
redis-objects-1.0.1 lib/redis/base_object.rb
redis-objects-1.0.0 lib/redis/base_object.rb