class FakeRedis::SortedSetArgumentHandler

  • Multiple aggregate values given
    * Invalid aggregate value given
    * Custom weights specified, but not enough or too many given
    Handles throwing errors for various scenarios (matches redis):
    and parses them into a few attributes for the method to access.
    Takes in the variable length array of arguments for a zinterstore/zunionstore method

def aggregate=(str)

Only allows assigning a value *once* - raises Redis::CommandError if a second is given
def aggregate=(str)
  raise(Redis::CommandError, "ERR syntax error") if (defined?(@aggregate) && @aggregate)
  @aggregate = str.to_s.downcase.to_sym
end

def handle(item)

Decides how to handle an item, depending on where we are in the arguments
def handle(item)
  case item
  when "WEIGHTS"
    self.type = :weights
    self.weights = []
  when "AGGREGATE"
    self.type = :aggregate
  when nil
    # This should never be called, raise a syntax error if we manage to hit it
    raise(Redis::CommandError, "ERR syntax error")
  else
    send "handle_#{type}", item
  end
  self
end

def handle_aggregate(item)

def handle_aggregate(item)
  self.aggregate = item
end

def handle_weights(item)

def handle_weights(item)
  self.weights << item
end

def initialize args

Expects all the argments for the method to be passed as an array
def initialize args
  # Pull out known lengths of data
  self.number_of_keys = args.shift
  self.keys = args.shift(number_of_keys)
  # Handle the variable lengths of data (WEIGHTS/AGGREGATE)
  args.inject(self) {|handler, item| handler.handle(item) }
  # Defaults for unspecified things
  self.weights ||= Array.new(number_of_keys) { 1 }
  self.aggregate ||= :sum
  # Validate values
  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 inject_block

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