Sha256: 0c80180186138aef52831ad97c41c20e72451133fe2f30ca53091a02f09cc77b

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

module Hari
  module Keys
    class Set < Key

      def set(name)
        @name = name
        self
      end

      def set!(name)
        @name = name
        members
      end

      def members
        desserialize Hari.redis.smembers(key)
      end

      def rand(count = 1)
        desserialize Hari.redis.srandmember(key, count)
      end

      def count
        Hari.redis.scard key
      end

      alias :size :count
      alias :length :count

      def empty?
        count == 0
      end

      def one?
        count == 1
      end

      def many?
        count > 1
      end

      def include?(member)
        Hari.redis.sismember key, serialize(member)
      end

      alias :member? :include?

      def add(*members)
        Hari.redis.sadd key, serialize(members)
      end

      def <<(member)
        add member
      end

      def delete(*members)
        Hari.redis.srem key, serialize(members)
      end

      def pop
        desserialize Hari.redis.spop(key)
      end

      def intersect(*set_queries)
        desserialize Hari.redis.sinter(key, set_query_keys(set_queries))
      end

      def &(other_set_query)
        intersect other_set_query
      end

      def diff(*set_queries)
        desserialize Hari.redis.sdiff(key, set_query_keys(set_queries))
      end

      def -(other_set_query)
        diff other_set_query
      end

      private

      def set_query_keys(set_queries)
        keys = set_queries.map do |query|
          ensure_set_query! query
          query.key
        end

        fail 'no query keys' if keys.empty?

        keys
      end

      def ensure_set_query!(query)
        unless query.kind_of?(Hari::Keys::Set)
          fail 'not a set query'
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hari-0.0.5 lib/hari/keys/set.rb