bootstripe 0.2.15 → 0.2.16

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,7 +6,7 @@ class Array
6
6
  def mean
7
7
  sum / size
8
8
  end
9
-
9
+
10
10
  def count(&action)
11
11
  return size unless block_given?
12
12
  self.inject(0){ |memo,item| memo += 1 if action.call(item); memo }
@@ -15,7 +15,7 @@ class Array
15
15
  def compakt
16
16
  self.reject do |i|
17
17
  i.nil? ||
18
- (i.respond_to?(:length) && i.length == 0) ||
18
+ (i.respond_to?(:length) && i.length == 0) ||
19
19
  (i.respond_to?(:match) && i.match(/^\s+$/))
20
20
  end
21
21
  end
@@ -24,8 +24,15 @@ class Array
24
24
  num_elements == 1 ? self[Kernel.rand(size)] : sort_by{ Kernel.rand }.slice(0...num_elements)
25
25
  end
26
26
 
27
+ def random_by_frequency(seed = nil)
28
+ self.select{|i| i.is_a?(Hash) }.inject({}) do |memo, item|
29
+ memo[item] = item['frequency'] || item[:frequency] || 0
30
+ memo
31
+ end.random_by_frequency(seed)
32
+ end
33
+
27
34
  def randomized
28
- sort_by { rand }
35
+ sort_by { rand }
29
36
  end
30
37
 
31
38
  # Return subarrays of random length between a min and max
@@ -23,4 +23,19 @@ class Hash
23
23
  end
24
24
  end
25
25
  end
26
+
27
+ def random_by_frequency(seed = nil)
28
+ options = select{ |k,v| v.is_a?(Fixnum) || (v.is_a?(Hash) && (v['frequency'] || v[:frequency])) }
29
+ total_frequency = options.map{ |k,v| v.is_a?(Hash) ? v['frequency'] || v[:frequency] : v }.sum
30
+ seed ||= rand(total_frequency)
31
+
32
+ s = 0
33
+ options.find do |option, val|
34
+ freq = val.is_a?(Fixnum) ? val : val['frequency'] || val[:frequency]
35
+ s += freq
36
+ return option if s > seed
37
+ end
38
+ nil
39
+ end
40
+
26
41
  end
@@ -1,3 +1,3 @@
1
1
  module Bootstripe
2
- VERSION = "0.2.15"
2
+ VERSION = "0.2.16"
3
3
  end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe :frequency do
4
+
5
+ it 'should pick an item from an array based on frequency key' do
6
+ options = [
7
+ { :name => 'one', :frequency => 10 },
8
+ { :name => 'two', :frequency => 5 },
9
+ { :name => 'three', :frequency => 1 }
10
+ ]
11
+
12
+ options.random_by_frequency(5).should == options[0]
13
+ options.random_by_frequency(12).should == options[1]
14
+ options.random_by_frequency(15).should == options[2]
15
+ end
16
+
17
+ it 'should pick an item from a hash based on frequency key' do
18
+ options = {
19
+ :one => { :name => 'one', :frequency => 10 },
20
+ :two => { :name => 'two', :frequency => 5 },
21
+ :three => { :name => 'three', :frequency => 1 }
22
+ }
23
+
24
+ options.random_by_frequency(5).should == :one
25
+ options.random_by_frequency(12).should == :two
26
+ options.random_by_frequency(15).should == :three
27
+ end
28
+
29
+ it 'should pick an item from a hash based on frequency value' do
30
+ options = {
31
+ 'one' => 10,
32
+ 'two' => 5,
33
+ 'three' => 1
34
+ }
35
+
36
+ options.random_by_frequency(5).should == 'one'
37
+ options.random_by_frequency(12).should == 'two'
38
+ options.random_by_frequency(15).should == 'three'
39
+ end
40
+
41
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bootstripe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.15
4
+ version: 0.2.16
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: 2012-10-23 00:00:00.000000000 Z
12
+ date: 2012-11-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -49,6 +49,7 @@ files:
49
49
  - lib/bootstripe/www_middleware.rb
50
50
  - spec/array_additions_spec.rb
51
51
  - spec/enumberable_additions_spec.rb
52
+ - spec/frequency_spec.rb
52
53
  - spec/hash_additions_spec.rb
53
54
  - spec/number_additions_spec.rb
54
55
  - spec/spec_helper.rb
@@ -80,6 +81,7 @@ summary: A handful of useful additions for Ruby
80
81
  test_files:
81
82
  - spec/array_additions_spec.rb
82
83
  - spec/enumberable_additions_spec.rb
84
+ - spec/frequency_spec.rb
83
85
  - spec/hash_additions_spec.rb
84
86
  - spec/number_additions_spec.rb
85
87
  - spec/spec_helper.rb