Sha256: 663023cda98a60a6ac882228297bd4a3da7d7c3ca433add60d1a1a53bc8afbad

Contents?: true

Size: 1.25 KB

Versions: 37

Compression:

Stored size: 1.25 KB

Contents

module Rediska
  class SortedSetArgumentHandler
    attr_reader :aggregate
    attr_accessor :number_of_keys, :keys, :weights, :type

    def initialize(args)
      @number_of_keys = args.shift
      @keys = args.shift(number_of_keys)
      args.inject(self) {|handler, item| handler.handle(item) }

      # Defaults.
      @weights ||= Array.new(number_of_keys) { 1 }
      @aggregate ||= :sum

      # Validation.
      raise Redis::CommandError, 'ERR syntax error' unless weights.size == number_of_keys
      raise Redis::CommandError, 'ERR syntax error' unless [:min, :max, :sum].include?(aggregate)
    end

    def aggregate=(str)
      raise Redis::CommandError, 'ERR syntax error' if @aggregate

      @aggregate = str.to_s.downcase.to_sym
    end

    def handle(item)
      case item
      when 'WEIGHTS'
        @type = :weights
        @weights = []
      when 'AGGREGATE'
        @type = :aggregate
      when nil
        raise Redis::CommandError, 'ERR syntax error'
      else
        send "handle_#{type}", item
      end

      self
    end

    def handle_weights(item)
      @weights << item
    end

    def handle_aggregate(item)
      @aggregate = item
    end

    def inject_block
      lambda { |handler, item| handler.handle(item) }
    end
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
rediska-1.1.0 lib/rediska/sorted_set_argument_handler.rb
rediska-1.0.2 lib/rediska/sorted_set_argument_handler.rb
rediska-0.5.0 lib/rediska/sorted_set_argument_handler.rb
rediska-0.4.0 lib/rediska/sorted_set_argument_handler.rb
rediska-0.3.1 lib/rediska/sorted_set_argument_handler.rb
rediska-0.3.0 lib/rediska/sorted_set_argument_handler.rb
rediska-0.2.7 lib/rediska/sorted_set_argument_handler.rb
rediska-0.2.6 lib/rediska/sorted_set_argument_handler.rb
rediska-0.2.5.1 lib/rediska/sorted_set_argument_handler.rb
rediska-0.2.5 lib/rediska/sorted_set_argument_handler.rb
rediska-0.2.4 lib/rediska/sorted_set_argument_handler.rb
rediska-0.2.3 lib/rediska/sorted_set_argument_handler.rb
rediska-0.2.2 lib/rediska/sorted_set_argument_handler.rb
rediska-0.2.1 lib/rediska/sorted_set_argument_handler.rb
rediska-0.2.0 lib/rediska/sorted_set_argument_handler.rb
rediska-0.1.12 lib/rediska/sorted_set_argument_handler.rb
rediska-0.1.11 lib/rediska/sorted_set_argument_handler.rb
rediska-0.1.10 lib/rediska/sorted_set_argument_handler.rb
rediska-0.1.9 lib/rediska/sorted_set_argument_handler.rb
rediska-0.1.8 lib/rediska/sorted_set_argument_handler.rb