pickup 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/pickup.rb +15 -19
- data/lib/pickup/version.rb +1 -1
- data/spec/pickup/pickup_spec.rb +10 -10
- metadata +1 -1
data/lib/pickup.rb
CHANGED
@@ -28,7 +28,7 @@ class Pickup
|
|
28
28
|
end
|
29
29
|
|
30
30
|
class MappedList
|
31
|
-
attr_reader :list, :func, :uniq
|
31
|
+
attr_reader :list, :func, :uniq, :max
|
32
32
|
|
33
33
|
def initialize(list, func, uniq=false)
|
34
34
|
@func = func
|
@@ -36,24 +36,6 @@ class Pickup
|
|
36
36
|
@list = map_list(list)
|
37
37
|
end
|
38
38
|
|
39
|
-
def map_list(list)
|
40
|
-
n = 0
|
41
|
-
mapped_list = {}
|
42
|
-
list.each do |k, v|
|
43
|
-
n += v
|
44
|
-
mapped_list[k] = {}
|
45
|
-
mapped_list[k][:value] = n
|
46
|
-
mapped_list[k][:picked] = false if uniq
|
47
|
-
end
|
48
|
-
mapped_list
|
49
|
-
end
|
50
|
-
|
51
|
-
def max
|
52
|
-
@max ||= begin
|
53
|
-
list.inject(0){|sum, (k,v)| sum+list[k][:value] }
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
39
|
def random
|
58
40
|
num = rand(max)
|
59
41
|
get_random_item(num)
|
@@ -71,5 +53,19 @@ class Pickup
|
|
71
53
|
list[key][:picked] = true if uniq
|
72
54
|
key
|
73
55
|
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def map_list(list)
|
60
|
+
@max = 0
|
61
|
+
mapped_list = {}
|
62
|
+
list.each do |k, v|
|
63
|
+
mapped_list[k] = {}
|
64
|
+
mapped_list[k][:value] = @max
|
65
|
+
@max += v
|
66
|
+
mapped_list[k][:picked] = false if uniq
|
67
|
+
end
|
68
|
+
mapped_list
|
69
|
+
end
|
74
70
|
end
|
75
71
|
end
|
data/lib/pickup/version.rb
CHANGED
data/spec/pickup/pickup_spec.rb
CHANGED
@@ -23,7 +23,7 @@ describe Pickup do
|
|
23
23
|
|
24
24
|
describe Pickup::MappedList do
|
25
25
|
before do
|
26
|
-
@mapped_list = {"selmon"=>{:value=>
|
26
|
+
@mapped_list = {"selmon"=>{:value=>0, :picked=>false}, "carp"=>{:value=>1, :picked=>false}, "crucian"=>{:value=>5, :picked=>false}, "herring"=>{:value=>8, :picked=>false}, "sturgeon"=>{:value=>14, :picked=>false}, "gudgeon"=>{:value=>22, :picked=>false}, "minnow"=>{:value=>32, :picked=>false}}
|
27
27
|
@ml = Pickup::MappedList.new(@list, @func, true)
|
28
28
|
@ml2 = Pickup::MappedList.new(@list, @func)
|
29
29
|
end
|
@@ -33,21 +33,21 @@ describe Pickup do
|
|
33
33
|
end
|
34
34
|
|
35
35
|
it "should return selmon and then carp and then crucian for uniq pickup" do
|
36
|
-
@ml.get_random_item(
|
37
|
-
@ml.get_random_item(
|
38
|
-
@ml.get_random_item(
|
36
|
+
@ml.get_random_item(0).must_equal "selmon"
|
37
|
+
@ml.get_random_item(0).must_equal "carp"
|
38
|
+
@ml.get_random_item(0).must_equal "crucian"
|
39
39
|
end
|
40
40
|
|
41
41
|
it "should return selmon 3 times for non-uniq pickup" do
|
42
|
-
@ml2.get_random_item(
|
43
|
-
@ml2.get_random_item(
|
44
|
-
@ml2.get_random_item(
|
42
|
+
@ml2.get_random_item(0).must_equal "selmon"
|
43
|
+
@ml2.get_random_item(0).must_equal "selmon"
|
44
|
+
@ml2.get_random_item(0).must_equal "selmon"
|
45
45
|
end
|
46
46
|
|
47
47
|
it "should return item from the beginning after end of list for uniq pickup" do
|
48
|
-
@ml.get_random_item(
|
49
|
-
@ml.get_random_item(
|
50
|
-
@ml.get_random_item(
|
48
|
+
@ml.get_random_item(20).must_equal "gudgeon"
|
49
|
+
@ml.get_random_item(20).must_equal "minnow"
|
50
|
+
@ml.get_random_item(20).must_equal "selmon"
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|