Sha256: d9d76fb3e15f3edd02761bc44946f00e28d9747d85951128b41553e05209f370

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

module RedisUtils
  class Key < Base
    class << self

      def instance
        super
      end

      def get(key)
        if exists? key
          case instance.type key
          when "string"
            instance.get key
          when "list"
            raise "Not implemented"
          when "set"
            instance.smembers key
          when "zset"
            raise "Not implemented"
          when "hash"
            puts "Redis GET: HASH requested"
            instance.hgetall(key).transform_keys { |key| key.to_sym }
          end
        else
          nil
        end
      end

      def get_as_hash(key)
        ret = {}
        ret.store key, get(key)
      end

      def exists?(key)
        instance.exists key
      end

      def member?(group_name, member_name)
        instance.sismember group_name, member_name
      end

      def member(group_name, member_name)
        return nil unless member?(group_name, member_name)
        get "#{group_name}:#{member_name}"
      end

      def members(key)
        member_keys(key).map { |member_id| member(key, member_id) }
      end

      def members_as_hash(key)
        ret = HashWithIndifferentAccess.new
        member_keys(key).map { |member_id| ret.store(member_id, member(key, member_id)) }
        ret
      end

      def member_keys(key)
        instance.smembers(key)
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
redis_utils-0.0.1 lib/redis_utils/key.rb