frequency-dsl 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -17,6 +17,14 @@ Frequency is a small dsl written in ruby to work with frequency events (never, s
17
17
  rarely :with_probability => 0.01 do
18
18
  puts "ok, its very rare..."
19
19
  end
20
+
21
+ normally :with_probability => '24%' do
22
+ puts "you can use strings instead float numbers"
23
+ end
24
+
25
+ alwais do
26
+ puts "bye"
27
+ end
20
28
 
21
29
  == Copyright
22
30
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{frequency-dsl}
8
- s.version = "0.0.2"
8
+ s.version = "0.0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Tiago Peczenyj"]
@@ -25,7 +25,12 @@ module Frequency
25
25
 
26
26
  private
27
27
  def execute_with_probability(conditions,default)
28
- rate = conditions[:with_probability] || default
28
+ rate = conditions[:with_probability] || default
29
+ rate = Float(rate) rescue correct_if_string(rate)
29
30
  yield if Kernel.rand() < rate
30
31
  end
32
+
33
+ def correct_if_string(rate)
34
+ Float(rate.gsub(/%$/,""))/100 if rate =~ /^\d+(.\d+)?%$/
35
+ end
31
36
  end
@@ -17,13 +17,35 @@ describe "Frequency" do
17
17
 
18
18
  describe "sometimes" do
19
19
  it "should be execute if rand() returns less than 0.50" do
20
- Kernel.should_receive(:rand).with().and_return(0.00)
20
+ Kernel.should_receive(:rand).with().and_return(0.14999)
21
21
  sometimes { true }.should be_true
22
22
  end
23
23
  it "should not be execute if rand() returns more than 0.50" do
24
24
  Kernel.should_receive(:rand).with().and_return(0.99)
25
25
  sometimes { true }.should be_nil
26
- end
26
+ end
27
+ it "should be execute if rand return less than 0.15" do
28
+ Kernel.should_receive(:rand).with().and_return(0.14999)
29
+ sometimes :with_probability => 0.15 do
30
+ true
31
+ end.should be_true
32
+
33
+ end
34
+ it "should not be execute if rand return more than 0.15" do
35
+ Kernel.should_receive(:rand).with().and_return(0.15)
36
+ sometimes :with_probability => 0.15 do
37
+ true
38
+ end.should be_nil
39
+
40
+ end
41
+ it "should be execute if rand return less than 15%" do
42
+ Kernel.should_receive(:rand).with().and_return(0.149999)
43
+ sometimes :with_probability => '15%' do
44
+ true
45
+ end.should be_true
46
+
47
+ end
48
+
27
49
  end
28
50
 
29
51
  end
@@ -1,5 +1,6 @@
1
1
  $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'rubygems'
3
4
  require 'frequency'
4
5
  require 'spec'
5
6
  require 'spec/autorun'
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: frequency-dsl
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tiago Peczenyj