Sha256: 6126a978d776380aee59faa05eaa9ec1c44ec38aa7ec226ac9f67449ac8ff2f3

Contents?: true

Size: 846 Bytes

Versions: 4

Compression:

Stored size: 846 Bytes

Contents

class Redis
  #
  # Class representing a simple value.  You can use standard Ruby operations on it.
  #
  class Value
    require 'redis/helpers/core_commands'
    include Redis::Helpers::CoreCommands
    require 'redis/helpers/serialize'
    include Redis::Helpers::Serialize

    attr_reader :key, :options, :redis
    def initialize(key, *args)
      @key = key
      @options = args.last.is_a?(Hash) ? args.pop : {}
      @redis = args.first || $redis
      @redis.setnx(key, @options[:default]) if @options[:default]
    end

    def value=(val)
      redis.set key, to_redis(val)
    end
    alias_method :set, :value=

    def value
      from_redis redis.get(key)
    end
    alias_method :get, :value

    def to_s;  value.to_s; end
    alias_method :to_str, :to_s

    def ==(x); value == x; end
    def nil?;  value.nil?; end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
redis-objects-0.3.2 lib/redis/value.rb
redis-objects-0.3.1 lib/redis/value.rb
redis-objects-0.3.0 lib/redis/value.rb
redis-objects-0.2.4 lib/redis/value.rb