Class: FakeRedis::SortedSetArgumentHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/fakeredis/sorted_set_argument_handler.rb

Overview

Takes in the variable length array of arguments for a zinterstore/zunionstore method and parses them into a few attributes for the method to access.

Handles throwing errors for various scenarios (matches redis):

* Custom weights specified, but not enough or too many given
* Invalid aggregate value given
* Multiple aggregate values given

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ SortedSetArgumentHandler

Expects all the argments for the method to be passed as an array

Raises:

  • (Redis::CommandError)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fakeredis/sorted_set_argument_handler.rb', line 23

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

Instance Attribute Details

#aggregateObject

Symbol

The aggregate method to use for the output values. One of %w(sum min max) expected



11
12
13
# File 'lib/fakeredis/sorted_set_argument_handler.rb', line 11

def aggregate
  @aggregate
end

#keysObject

Array

The actual keys in the argument list



15
16
17
# File 'lib/fakeredis/sorted_set_argument_handler.rb', line 15

def keys
  @keys
end

#number_of_keysObject

Integer

Number of keys in the argument list



13
14
15
# File 'lib/fakeredis/sorted_set_argument_handler.rb', line 13

def number_of_keys
  @number_of_keys
end

#typeObject

Used internally



20
21
22
# File 'lib/fakeredis/sorted_set_argument_handler.rb', line 20

def type
  @type
end

#weightsObject

Array

integers for weighting the values of each key - one number per key expected



17
18
19
# File 'lib/fakeredis/sorted_set_argument_handler.rb', line 17

def weights
  @weights
end

Instance Method Details

#handle(item) ⇒ Object

Decides how to handle an item, depending on where we are in the arguments



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fakeredis/sorted_set_argument_handler.rb', line 46

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

#handle_aggregate(item) ⇒ Object



66
67
68
# File 'lib/fakeredis/sorted_set_argument_handler.rb', line 66

def handle_aggregate(item)
  self.aggregate = item
end

#handle_weights(item) ⇒ Object



62
63
64
# File 'lib/fakeredis/sorted_set_argument_handler.rb', line 62

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

#inject_blockObject



70
71
72
# File 'lib/fakeredis/sorted_set_argument_handler.rb', line 70

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