Class: Rediska::SortedSetArgumentHandler

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ SortedSetArgumentHandler

Returns a new instance of SortedSetArgumentHandler.

Raises:

  • (Redis::CommandError)


6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/rediska/sorted_set_argument_handler.rb', line 6

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

Instance Attribute Details

#aggregateObject

Returns the value of attribute aggregate.



3
4
5
# File 'lib/rediska/sorted_set_argument_handler.rb', line 3

def aggregate
  @aggregate
end

#keysObject

Returns the value of attribute keys.



4
5
6
# File 'lib/rediska/sorted_set_argument_handler.rb', line 4

def keys
  @keys
end

#number_of_keysObject

Returns the value of attribute number_of_keys.



4
5
6
# File 'lib/rediska/sorted_set_argument_handler.rb', line 4

def number_of_keys
  @number_of_keys
end

#typeObject

Returns the value of attribute type.



4
5
6
# File 'lib/rediska/sorted_set_argument_handler.rb', line 4

def type
  @type
end

#weightsObject

Returns the value of attribute weights.



4
5
6
# File 'lib/rediska/sorted_set_argument_handler.rb', line 4

def weights
  @weights
end

Instance Method Details

#handle(item) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/rediska/sorted_set_argument_handler.rb', line 26

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

#handle_aggregate(item) ⇒ Object



46
47
48
# File 'lib/rediska/sorted_set_argument_handler.rb', line 46

def handle_aggregate(item)
  @aggregate = item
end

#handle_weights(item) ⇒ Object



42
43
44
# File 'lib/rediska/sorted_set_argument_handler.rb', line 42

def handle_weights(item)
  @weights << item
end

#inject_blockObject



50
51
52
# File 'lib/rediska/sorted_set_argument_handler.rb', line 50

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