fakeredis 0.3.3 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,80 +0,0 @@
1
- module FakeRedis
2
- class SortedSetStore
3
- attr_accessor :data, :weights, :aggregate, :keys
4
-
5
- def initialize params, data
6
- self.data = data
7
- self.weights = params.weights
8
- self.aggregate = params.aggregate
9
- self.keys = params.keys
10
- end
11
-
12
- def hashes
13
- @hashes ||= keys.map do |src|
14
- case data[src]
15
- when ::Set
16
- # Every value has a score of 1
17
- Hash[data[src].map {|k,v| [k, 1]}]
18
- when Hash
19
- data[src]
20
- else
21
- {}
22
- end
23
- end
24
- end
25
-
26
- # Apply the weightings to the hashes
27
- def computed_values
28
- unless defined?(@computed_values) && @computed_values
29
- # Do nothing if all weights are 1, as n * 1 is n
30
- @computed_values = hashes if weights.all? {|weight| weight == 1 }
31
- # Otherwise, multiply the values in each hash by that hash's weighting
32
- @computed_values ||= hashes.each_with_index.map do |hash, index|
33
- weight = weights[index]
34
- Hash[hash.map {|k, v| [k, (v * weight)]}]
35
- end
36
- end
37
- @computed_values
38
- end
39
-
40
- def aggregate_sum out
41
- selected_keys.each do |key|
42
- out[key] = computed_values.inject(0) do |n, hash|
43
- n + (hash[key] || 0)
44
- end
45
- end
46
- end
47
-
48
- def aggregate_min out
49
- selected_keys.each do |key|
50
- out[key] = computed_values.map {|h| h[key] }.compact.min
51
- end
52
- end
53
-
54
- def aggregate_max out
55
- selected_keys.each do |key|
56
- out[key] = computed_values.map {|h| h[key] }.compact.max
57
- end
58
- end
59
-
60
- def selected_keys
61
- raise NotImplemented, "subclass needs to implement #selected_keys"
62
- end
63
-
64
- def call
65
- ZSet.new.tap {|out| send("aggregate_#{aggregate}", out) }
66
- end
67
- end
68
-
69
- class SortedSetIntersectStore < SortedSetStore
70
- def selected_keys
71
- @values ||= hashes.inject([]) { |r, h| r.empty? ? h.keys : (r & h.keys) }
72
- end
73
- end
74
-
75
- class SortedSetUnionStore < SortedSetStore
76
- def selected_keys
77
- @values ||= hashes.map(&:keys).flatten.uniq
78
- end
79
- end
80
- end
@@ -1,4 +0,0 @@
1
- module FakeRedis
2
- class ZSet < Hash
3
- end
4
- end
@@ -1,14 +0,0 @@
1
- require "spec_helper"
2
-
3
- # Remove memory so we test against actual redis
4
- Redis::Connection.drivers.pop
5
-
6
- RSpec.configure do |config|
7
- config.before(:each) do
8
- Redis.new.flushdb
9
- end
10
- end
11
-
12
- def fakeredis?
13
- false
14
- end