Sha256: f4c4f6ef1340e5e3f3bd796e5173239462f31ff7dc3cc05115a38d592c6296a1

Contents?: true

Size: 909 Bytes

Versions: 1

Compression:

Stored size: 909 Bytes

Contents

module Lunar
  class ResultSet
    include Enumerable

    attr :key

    def initialize(key, &block)
      @key   = key
      @block = block
    end

    def each(&block)
      all.each(&block)
    end
  end

  class SortedResultSet < ResultSet
    def all(options = {})
      start  = Integer(options[:start] || 0)
      limit  = Integer(options[:limit] || 0)
      finish = start + limit - 1
      
      puts "Getting: #{key}, #{Lunar.redis.zrange(key, start, finish)}" if $GAME
      Lunar.redis.zrevrange(key, start, finish).map(&@block)
    end

    def size
      Lunar.redis.zcard(key)
    end
  end

  class UnsortedResultSet < ResultSet
    def all(options = {})
      start  = Integer(options[:start] || 0)
      limit  = Integer(options[:limit] || 100)
      
      Lunar.redis.sort(key, :limit => [start, limit]).map(&@block)
    end

    def size
      Lunar.redis.scard(key)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lunar-0.3.0 lib/lunar/result_set.rb