weighted_random 0.0.2 → 0.0.3
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.
- data/README.rdoc +21 -17
- data/lib/weighted_random/railtie.rb +12 -0
- data/lib/weighted_random/version.rb +1 -1
- data/lib/weighted_random.rb +23 -13
- metadata +3 -3
- data/lib/weighted_random/activerecord/base.rb +0 -7
data/README.rdoc
CHANGED
@@ -3,33 +3,36 @@ Weighted randomization extension for ActiveRecord.
|
|
3
3
|
|
4
4
|
Loads records with weight value for randomization and gives ability of weighted randomization of them.
|
5
5
|
|
6
|
-
==
|
6
|
+
== Installation
|
7
7
|
|
8
|
-
|
8
|
+
=== Gem
|
9
9
|
|
10
10
|
gem 'weighted_random'
|
11
11
|
|
12
|
-
|
12
|
+
=== Model
|
13
|
+
|
14
|
+
Generate model with <code>weight</code> and <code>cumulative_weight</code> integer attributes:
|
13
15
|
|
14
|
-
|
16
|
+
rails g model your_model [your_attributes] weight:integer cumulative_weight:integer
|
15
17
|
|
16
|
-
|
18
|
+
Or add these two attributes into migration of existing model:
|
17
19
|
|
18
20
|
t.integer :weight
|
19
21
|
t.integer :cumulative_weight
|
20
22
|
|
21
|
-
|
22
|
-
|
23
|
-
Load methods for weighted randomization into your desired class model:
|
23
|
+
Load weighted randomization stuff into desired model and set accessibillity of previous these attributes:
|
24
24
|
|
25
|
-
class
|
25
|
+
class YourModel < ActiveRecord::Base
|
26
26
|
weighted_randomizable
|
27
|
+
attr_accessible :weight, :cumulative_weight
|
27
28
|
...
|
28
29
|
end
|
29
30
|
|
30
|
-
|
31
|
+
== Usage
|
32
|
+
|
33
|
+
=== Load data
|
31
34
|
|
32
|
-
Load records with weight
|
35
|
+
Load records with <code>weight</code> attribute for randomization, here is an example of loading last names and their frequencies from CSV file:
|
33
36
|
|
34
37
|
last_names.csv:
|
35
38
|
Smith,10
|
@@ -47,18 +50,19 @@ seeds.rb:
|
|
47
50
|
csv.close
|
48
51
|
LastName.create_with_cumulative_weight(names)
|
49
52
|
|
50
|
-
Simply you have to supply collection of hashes, each with
|
51
|
-
method of your desired model class
|
53
|
+
Simply you have to supply collection of hashes, each with <code>weight</code> integer property, to
|
54
|
+
<code>create_with_cumulative_weight</code> method of your desired model class, so it will load them all
|
55
|
+
into database with additional <code>cumulative_weight</code> attribute, which is reqired for faster weighted randomization.
|
52
56
|
|
53
57
|
=== Weighted randomization
|
54
58
|
|
55
|
-
To
|
59
|
+
To get weighed random record simply run:
|
56
60
|
|
57
61
|
LastName.weighted_rand
|
58
62
|
|
59
|
-
|
63
|
+
=== Demonstration
|
60
64
|
|
61
|
-
10.times { puts
|
65
|
+
10.times { puts LastName.weighted_rand.name }
|
62
66
|
Johnson
|
63
67
|
Brown
|
64
68
|
Johnson
|
@@ -72,4 +76,4 @@ To randomize some record in a weighted way, simply run:
|
|
72
76
|
|
73
77
|
== Author
|
74
78
|
|
75
|
-
Szymon Przybył (github.com/apocalyptiq)
|
79
|
+
Szymon Przybył (http://github.com/apocalyptiq)
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module WeightedRandom
|
2
|
+
if defined? Rails::Railtie
|
3
|
+
require 'rails'
|
4
|
+
class Railtie < Rails::Railtie
|
5
|
+
initializer 'weighted_random.insert_into_active_record' do
|
6
|
+
ActiveSupport.on_load :active_record do
|
7
|
+
ActiveRecord::Base.send(:extend, WeightedRandom::Loader)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/lib/weighted_random.rb
CHANGED
@@ -1,20 +1,30 @@
|
|
1
|
+
require 'weighted_random/railtie'
|
2
|
+
|
1
3
|
module WeightedRandom
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
item[:cumulative_weight] = weight_sum
|
7
|
-
item
|
4
|
+
|
5
|
+
module Loader
|
6
|
+
def weighted_randomizable
|
7
|
+
extend WeightedRandom::ClassMethods
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
module ClassMethods
|
12
|
+
def weighted_rand
|
13
|
+
self.where("cumulative_weight >= #{rand(self.maximum('cumulative_weight'))}").order('cumulative_weight').first
|
14
|
+
end
|
14
15
|
|
15
|
-
|
16
|
-
|
16
|
+
def create_with_cumulative_weight(collection)
|
17
|
+
self.create! self.compute_and_insert_cumulative_weight(collection)
|
18
|
+
end
|
19
|
+
|
20
|
+
def compute_and_insert_cumulative_weight(collection)
|
21
|
+
weight_sum = 0
|
22
|
+
collection.collect do |item|
|
23
|
+
weight_sum += item[:weight]
|
24
|
+
item[:cumulative_weight] = weight_sum
|
25
|
+
item
|
26
|
+
end
|
27
|
+
end
|
17
28
|
end
|
18
|
-
end
|
19
29
|
|
20
|
-
|
30
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: weighted_random
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-04-
|
12
|
+
date: 2011-04-12 00:00:00.000000000 +02:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
description: ActiveRecord extension for weighted randomization which supplies loading
|
@@ -25,7 +25,7 @@ files:
|
|
25
25
|
- README.rdoc
|
26
26
|
- Rakefile
|
27
27
|
- lib/weighted_random.rb
|
28
|
-
- lib/weighted_random/
|
28
|
+
- lib/weighted_random/railtie.rb
|
29
29
|
- lib/weighted_random/version.rb
|
30
30
|
- weighted_random.gemspec
|
31
31
|
has_rdoc: true
|