liquid-ext 1.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MjdmMzQ5MDUxODJkYjkyZDhlYTdmYTNmMmIzZGE5ODI5MTJkYzEwYQ==
4
+ MDFlZGZhYjMxMmQwOGU2YTk0ZTMxMzBiODRkZjEyMWRmNjA3ZTY4Mw==
5
5
  data.tar.gz: !binary |-
6
- ZWUxZGNjNWRhMzczNjRiZjhiZGViY2U4ZjFlZWUyMzEzOWQ5ZjNjOQ==
6
+ OGU1MWVmYmFlZWNmNmNmY2U1MTBiYzQ3ZWE2Y2E2OWVhOGQ4OGRkMA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- OWUyM2U0ZjdmNDRjYTkzZTkyMjMxMTJmZDlkOThlYzg0ZDFlYzRiNjE3MTk2
10
- MzNlMmM0MGE3MTAzOWQ5MzFkNTlmZWUyMDIwMmRhMzVmNTU4YjQ1NmU1NjY1
11
- M2RkYWI4ZjE5OWRkOTUwNGE4YjQ4YzBkNTE2NDE5MWM2Yjc4ZjA=
9
+ MWU5M2Y3Y2I0NWUyNTRiNWI3NmM5MmFlMmJjZDIxZGRhNGI5ZDc5MzNiNTI4
10
+ MDBjNWY3NDFhYWU2ZDg1M2I4MGE3ZGU5YzM5MDlmYzJhODA2N2UxODQ5NDQ1
11
+ MzQ5MDBhMGMxYmY2NGM0YWNjODJmYjMxMzY1OTE2MzU1ZTZjOTE=
12
12
  data.tar.gz: !binary |-
13
- NDhiNDY2ODFmNmYyMDBhMjVkMWNlYjViMzI2NjZiZTMyNjQwMmMwMGZhNTg4
14
- NmYzMTNiZTdkMjU0YzMxNTIxMDdlZTIwMmM4NzU0ZTZiMWIwNWVkMzJhZGZk
15
- YmRkNGU3ODhhZTU1YWM0OGM1MmIxZjMzNGE5MDJjMTZiZDQxODE=
13
+ YzQ1NTEyZTNjZTE5NDk3ZTIzOTFjODU0YzczMWVhZjYzMGEzZGQ0MTE5MWI1
14
+ ZTllMzU0MjZkZWI1NmRkY2JjZmVjZjViOGI2NzFlMDg5NzA1MmE2N2Y5M2Rj
15
+ OWZhMGMyMjFlZjgyZjMzM2U0Yzg2Yjg3YTEzMTY1NTM0YTM0ZDY=
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+
3
+ class WeightedSelector
4
+
5
+ def initialize
6
+ @sums = []
7
+ @elements = []
8
+ @total = 0
9
+ end
10
+
11
+ def add(element, probability)
12
+ @elements << element
13
+ @total += probability
14
+ @sums << @total
15
+ end
16
+
17
+ def delete(element)
18
+ idx = @elements.index(element)
19
+ if idx
20
+ @total -= @sums[idx]
21
+ @sums.delete_at(idx)
22
+ @elements.delete_at(idx)
23
+ end
24
+ end
25
+
26
+ def fill_up(element)
27
+ add(element, 1 - @total) if @total < 1
28
+ end
29
+
30
+ def empty?
31
+ @elements.empty?
32
+ end
33
+
34
+ # http://stackoverflow.com/questions/4463561/weighed-random-selection-from-array
35
+ def pick_one_with_index
36
+ rnd = Kernel.rand * @total
37
+ idx = @sums.index { |x| x >= rnd }
38
+ [@elements[idx], idx]
39
+ end
40
+
41
+ def pick_one
42
+ pick_one_with_index[0]
43
+ end
44
+
45
+ end
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "liquid-ext"
5
- spec.version = "1.2.0"
5
+ spec.version = "1.2.1"
6
6
  spec.authors = ["LiquidM, Inc."]
7
7
  spec.email = ["opensource@liquidm.com"]
8
8
  spec.description = %q{Ruby core extensions and helper libraries}
@@ -0,0 +1,67 @@
1
+ # encoding: utf-8
2
+
3
+ require 'liquid/weighted_selector'
4
+
5
+ describe WeightedSelector do
6
+
7
+ describe "#empty?" do
8
+
9
+ it do
10
+ should be_empty
11
+ end
12
+
13
+ context do
14
+
15
+ before do
16
+ subject.add 'a', 0
17
+ end
18
+
19
+ it do
20
+ should_not be_empty
21
+ end
22
+
23
+ end
24
+ end
25
+
26
+ describe '#pick_one' do
27
+
28
+ context do
29
+
30
+ before do
31
+ subject.add 'a', 2
32
+ subject.add 'b', 2
33
+ subject.add 'c', 0
34
+ end
35
+
36
+ it do
37
+ ['a', 'b'].should include subject.pick_one
38
+ end
39
+
40
+ end
41
+
42
+ context do
43
+
44
+ before do
45
+ subject.add 'a', 2
46
+ subject.add 'b', 2
47
+ subject.add 'c', 0
48
+ subject.delete 'a'
49
+ end
50
+
51
+ its(:pick_one){ should == 'b' }
52
+ its(:pick_one_with_index){ should == ['b', 0] }
53
+
54
+ end
55
+
56
+ context do
57
+
58
+ before do
59
+ Kernel.stub(:rand).and_return(1.00)
60
+ subject.add 'a', 0.1
61
+ end
62
+
63
+ its(:pick_one){ should == 'a' }
64
+
65
+ end
66
+ end
67
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: liquid-ext
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - LiquidM, Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-05 00:00:00.000000000 Z
11
+ date: 2013-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -163,9 +163,11 @@ files:
163
163
  - lib/liquid/templates/server.rb.tt
164
164
  - lib/liquid/timing.rb
165
165
  - lib/liquid/transaction_id.rb
166
+ - lib/liquid/weighted_selector.rb
166
167
  - liquid-ext.gemspec
167
168
  - spec/lib/liquid/ext/enumerable_spec.rb
168
169
  - spec/lib/liquid/router_spec.rb
170
+ - spec/lib/liquid/weighted_selector_spec.rb
169
171
  - spec/spec_helper.rb
170
172
  homepage: https://github.com/liquidm/ext
171
173
  licenses:
@@ -187,12 +189,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
187
189
  version: '0'
188
190
  requirements: []
189
191
  rubyforge_project:
190
- rubygems_version: 2.1.11
192
+ rubygems_version: 2.1.9
191
193
  signing_key:
192
194
  specification_version: 4
193
195
  summary: Ruby core extensions and helper libraries
194
196
  test_files:
195
197
  - spec/lib/liquid/ext/enumerable_spec.rb
196
198
  - spec/lib/liquid/router_spec.rb
199
+ - spec/lib/liquid/weighted_selector_spec.rb
197
200
  - spec/spec_helper.rb
198
201
  has_rdoc: