frequency-dsl 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 +8 -0
- data/VERSION +1 -1
- data/frequency-dsl.gemspec +1 -1
- data/lib/frequency.rb +6 -1
- data/spec/frequency_spec.rb +24 -2
- data/spec/spec_helper.rb +1 -0
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -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.
|
1
|
+
0.0.3
|
data/frequency-dsl.gemspec
CHANGED
data/lib/frequency.rb
CHANGED
@@ -25,7 +25,12 @@ module Frequency
|
|
25
25
|
|
26
26
|
private
|
27
27
|
def execute_with_probability(conditions,default)
|
28
|
-
rate
|
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
|
data/spec/frequency_spec.rb
CHANGED
@@ -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.
|
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
|
data/spec/spec_helper.rb
CHANGED
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:
|
4
|
+
hash: 25
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Tiago Peczenyj
|