frequency-dsl 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,27 @@
1
+ Copyright (c) 2010, Tiago Peczenyj
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+ 1. Redistributions of source code must retain the above copyright
7
+ notice, this list of conditions and the following disclaimer.
8
+ 2. Redistributions in binary form must reproduce the above copyright
9
+ notice, this list of conditions and the following disclaimer in the
10
+ documentation and/or other materials provided with the distribution.
11
+ 3. All advertising materials mentioning features or use of this software
12
+ must display the following acknowledgement:
13
+ This product includes software developed by the <organization>.
14
+ 4. Neither the name of the <organization> nor the
15
+ names of its contributors may be used to endorse or promote products
16
+ derived from this software without specific prior written permission.
17
+
18
+ THIS SOFTWARE IS PROVIDED BY Tiago Peczenyj ''AS IS'' AND ANY
19
+ EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
+ DISCLAIMED. IN NO EVENT SHALL Tiago Peczenyj BE LIABLE FOR ANY
22
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.rdoc ADDED
@@ -0,0 +1,23 @@
1
+ = Frequency
2
+
3
+ Frequency is a small dsl written in ruby to work with frequency events (never, sometimes, always..)
4
+
5
+ == Examples
6
+
7
+ include Frequency
8
+
9
+ sometimes do
10
+ puts "sometimes you can see this times, sometimes not"
11
+ end
12
+
13
+ never do
14
+ puts "this line never will be print"
15
+ end
16
+
17
+ rarely :with_probability => 0.01 do
18
+ puts "ok, its very rare..."
19
+ end
20
+
21
+ == Copyright
22
+
23
+ Copyright (c) 2010 Tiago Peczenyj. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,51 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "frequency-dsl"
8
+ gem.summary = %Q{A small dsl written in ruby to work with frequency events}
9
+ gem.description = %Q{A small dsl written in ruby to work with frequency events (never, sometimes, always..)}
10
+ gem.email = "tiago.peczenyj@gmail.com"
11
+ gem.homepage = "http://github.com/peczenyj/Frequency"
12
+ gem.authors = ["Tiago Peczenyj"]
13
+ gem.add_development_dependency "rspec"
14
+ # gem is a Gem::Specification...
15
+ # See http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
20
+ end
21
+
22
+ require 'spec/rake/spectask'
23
+ Spec::Rake::SpecTask.new(:spec) do |spec|
24
+ spec.libs << 'lib' << 'spec'
25
+ spec.spec_files = FileList['spec/**/*_spec.rb']
26
+ end
27
+
28
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
29
+ spec.libs << 'lib' << 'spec'
30
+ spec.pattern = 'spec/**/*_spec.rb'
31
+ spec.rcov = true
32
+ end
33
+
34
+ task :spec => :check_dependencies
35
+
36
+ task :default => :spec
37
+
38
+ require 'rake/rdoctask'
39
+ Rake::RDocTask.new do |rdoc|
40
+ if File.exist?('VERSION')
41
+ version = File.read('VERSION')
42
+ else
43
+ version = ""
44
+ end
45
+
46
+ rdoc.rdoc_dir = 'rdoc'
47
+ rdoc.title = "frequency #{version}"
48
+ rdoc.rdoc_files.include('README*')
49
+ rdoc.rdoc_files.include('lib/**/*.rb')
50
+ end
51
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/lib/frequency.rb ADDED
@@ -0,0 +1,31 @@
1
+ module Frequency
2
+ NORMALLY = 0.75
3
+ SOMETIMES = 0.50
4
+ RARELY = 0.25
5
+
6
+ def self.always(cond={})
7
+ yield if block_given?
8
+ end
9
+
10
+ def self.normally(cond={})
11
+ self.execute_with_probability(cond,NORMALLY ) { yield } if block_given?
12
+ end
13
+
14
+ def self.sometimes(cond={})
15
+ self.execute_with_probability(cond,SOMETIMES) { yield } if block_given?
16
+ end
17
+
18
+ def self.rarely(cond={})
19
+ self.execute_with_probability(cond,RARELY ) { yield } if block_given?
20
+ end
21
+
22
+ def self.never(cond={})
23
+ nil
24
+ end
25
+
26
+ private
27
+ def self.execute_with_probability(conditions,default)
28
+ rate = conditions[:with_probability] || default
29
+ yield if Kernel.rand() < rate
30
+ end
31
+ end
@@ -0,0 +1,24 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Frequency" do
4
+
5
+ describe "always" do
6
+
7
+ it "should be execute always" do
8
+ Frequency.always { true }.should be_true
9
+ end
10
+ it "should be not execute never" do
11
+ Frequency.never { true }.should be_nil
12
+ end
13
+ it "should be execute if rand() returns less than 0.50" do
14
+ Kernel.should_receive(:rand).with().and_return(0.00)
15
+ Frequency.sometimes { true }.should be_true
16
+ end
17
+ it "should not be execute if rand() returns more than 0.50" do
18
+ Kernel.should_receive(:rand).with().and_return(0.99)
19
+ Frequency.sometimes { true }.should be_nil
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'frequency'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: frequency-dsl
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Tiago Peczenyj
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-05-19 00:00:00 -03:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: A small dsl written in ruby to work with frequency events (never, sometimes, always..)
36
+ email: tiago.peczenyj@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.rdoc
44
+ files:
45
+ - LICENSE
46
+ - README.rdoc
47
+ - Rakefile
48
+ - VERSION
49
+ - lib/frequency.rb
50
+ - spec/frequency_helper.rb
51
+ - spec/spec_helper.rb
52
+ has_rdoc: true
53
+ homepage: http://github.com/peczenyj/Frequency
54
+ licenses: []
55
+
56
+ post_install_message:
57
+ rdoc_options:
58
+ - --charset=UTF-8
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ hash: 3
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ requirements: []
80
+
81
+ rubyforge_project:
82
+ rubygems_version: 1.3.7
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: A small dsl written in ruby to work with frequency events
86
+ test_files:
87
+ - spec/frequency_helper.rb
88
+ - spec/spec_helper.rb