in_every 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "http://rubygems.org"
2
+
3
+ group :development do
4
+ gem "rspec", "~> 2.8.0"
5
+ gem "rspec-given"
6
+ gem "yard", "~> 0.7"
7
+ gem "rdoc", "~> 3.12"
8
+ gem "bundler", "~> 1.1.0"
9
+ gem "jeweler", "~> 1.8.3"
10
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,36 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.3)
5
+ git (1.2.5)
6
+ jeweler (1.8.3)
7
+ bundler (~> 1.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rdoc
11
+ json (1.6.6)
12
+ rake (0.9.2.2)
13
+ rdoc (3.12)
14
+ json (~> 1.4)
15
+ rspec (2.8.0)
16
+ rspec-core (~> 2.8.0)
17
+ rspec-expectations (~> 2.8.0)
18
+ rspec-mocks (~> 2.8.0)
19
+ rspec-core (2.8.0)
20
+ rspec-expectations (2.8.0)
21
+ diff-lcs (~> 1.1.2)
22
+ rspec-given (1.4.2)
23
+ rspec (> 1.2.8)
24
+ rspec-mocks (2.8.0)
25
+ yard (0.7.5)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ bundler (~> 1.1.0)
32
+ jeweler (~> 1.8.3)
33
+ rdoc (~> 3.12)
34
+ rspec (~> 2.8.0)
35
+ rspec-given
36
+ yard (~> 0.7)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Colin Gemmell
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,42 @@
1
+ = In Every
2
+
3
+ Execute a block of code randomly x number of times in a cycle
4
+
5
+ == Current Status
6
+
7
+ Ready and able
8
+
9
+
10
+ == Installing
11
+
12
+ gem install in_every
13
+
14
+
15
+ == Usage
16
+
17
+ Setup
18
+
19
+ #setup in_every instance with block of code to be executed
20
+ in_every = 5.times.in_every(20) do |argument|
21
+ #insert really impressive code here
22
+ end
23
+
24
+ #execute a cycle. this will ether return the value from the block or nil if the block was not executed.
25
+ in_every.execute("some value")
26
+
27
+
28
+ == Contributing to in_every
29
+
30
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
31
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
32
+ * Fork the project.
33
+ * Start a feature/bugfix branch.
34
+ * Commit and push until you are happy with your contribution.
35
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
36
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
37
+
38
+ == Copyright
39
+
40
+ Copyright (c) 2012 Colin Gemmell. See LICENSE.txt for
41
+ further details.
42
+
data/Rakefile ADDED
@@ -0,0 +1,42 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "in_every"
18
+ gem.homepage = "http://github.com/pythonandchips/in_every"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{execute code x number of time in every y number of atempts}
21
+ gem.description = %Q{In every will randomly execute the supplied block of code randomly. It guarantees the code will be executed the given number of times in the code. }
22
+ gem.email = "pythonandchips@gmail.com"
23
+ gem.authors = ["Colin Gemmell"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'yard'
42
+ YARD::Rake::YardocTask.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/in_every.gemspec ADDED
@@ -0,0 +1,68 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "in_every"
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Colin Gemmell"]
12
+ s.date = "2012-04-16"
13
+ s.description = "In every will randomly execute the supplied block of code randomly. It guarantees the code will be executed the given number of times in the code. "
14
+ s.email = "pythonandchips@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "in_every.gemspec",
29
+ "lib/in_every.rb",
30
+ "lib/in_every/enumerator_extensions.rb",
31
+ "spec/in_every/enumerator_extensions_spec.rb",
32
+ "spec/in_every_spec.rb",
33
+ "spec/spec_helper.rb"
34
+ ]
35
+ s.homepage = "http://github.com/pythonandchips/in_every"
36
+ s.licenses = ["MIT"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = "1.8.15"
39
+ s.summary = "execute code x number of time in every y number of atempts"
40
+
41
+ if s.respond_to? :specification_version then
42
+ s.specification_version = 3
43
+
44
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
45
+ s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
46
+ s.add_development_dependency(%q<rspec-given>, [">= 0"])
47
+ s.add_development_dependency(%q<yard>, ["~> 0.7"])
48
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
49
+ s.add_development_dependency(%q<bundler>, ["~> 1.1.0"])
50
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
51
+ else
52
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
53
+ s.add_dependency(%q<rspec-given>, [">= 0"])
54
+ s.add_dependency(%q<yard>, ["~> 0.7"])
55
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
56
+ s.add_dependency(%q<bundler>, ["~> 1.1.0"])
57
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
58
+ end
59
+ else
60
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
61
+ s.add_dependency(%q<rspec-given>, [">= 0"])
62
+ s.add_dependency(%q<yard>, ["~> 0.7"])
63
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
64
+ s.add_dependency(%q<bundler>, ["~> 1.1.0"])
65
+ s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
66
+ end
67
+ end
68
+
data/lib/in_every.rb ADDED
@@ -0,0 +1,65 @@
1
+ require 'in_every/enumerator_extensions'
2
+
3
+ #Randomly execute a block of code x number of times in every number of runs
4
+ #Example:
5
+ # in_every = 5.times.in_every(20) do
6
+ # ##some really cool code
7
+ # end
8
+ #
9
+ # #will return value of the block if executed or nil if nothing happened
10
+ # in_every.execute
11
+ class InEvery
12
+ #thrown if there is a problem setting up InEvery
13
+ class SetupException < Exception; end
14
+
15
+ #create new in every instance
16
+ #
17
+ #==Parameters:
18
+ #enum::
19
+ # enumeration number for cycling through executions
20
+ #
21
+ #executions::
22
+ # number or times the proc is to be randomly executed
23
+ #
24
+ #method::
25
+ # What to execute randomly in a cycle
26
+ #
27
+ #==Returns:
28
+ # new instance of InEvery
29
+ def initialize(enum, executions, method)
30
+ @executions = (executions - 1)
31
+ @enum = enum
32
+ @method = method
33
+ generate_occurances
34
+ raise SetupException.new("occurancies must be greater than executions") if @enum.max > @executions
35
+ end
36
+
37
+ #execute the given proc if randomly
38
+ #
39
+ #==Parameters:
40
+ #args::
41
+ # arguments required to execute the proc
42
+ #
43
+ #==Returns:
44
+ #the result of the block or nil if nothing was executed
45
+ def execute *args
46
+ @counter ||= 0
47
+ if @counter == @execution_points.first
48
+ result = @method.call(*args)
49
+ @execution_points.delete_at(0)
50
+ end
51
+ if @counter == @executions
52
+ @counter = nil
53
+ generate_occurances
54
+ else
55
+ @counter += 1
56
+ end
57
+ result
58
+ end
59
+
60
+ private
61
+
62
+ def generate_occurances #nodoc
63
+ @execution_points = (0..(@executions)).to_a.sample(@enum.max).sort
64
+ end
65
+ end
@@ -0,0 +1,14 @@
1
+ class Enumerator #nodoc
2
+
3
+ #extension to create InEvery based on the enumeration
4
+ #
5
+ #==Parameters
6
+ #int::
7
+ # number of calls in a cycle
8
+ #
9
+ #block::
10
+ # what to execute randomly in cycle
11
+ def in_every int, &block
12
+ InEvery.new(self, int, block)
13
+ end
14
+ end
@@ -0,0 +1,9 @@
1
+ require "spec_helper"
2
+
3
+ describe "enumertor extensions" do
4
+
5
+ Given(:enumerator){ 5.times }
6
+ When(:result){ enumerator.in_every(10) }
7
+ Then { result.should be_kind_of InEvery }
8
+
9
+ end
@@ -0,0 +1,42 @@
1
+ require "spec_helper"
2
+
3
+ describe InEvery do
4
+
5
+ context "with 5 occrancies in every 10 executions" do
6
+ Given(:in_every) do
7
+ 5.times.in_every(10) do |argument|
8
+ @run_count += 1
9
+ end
10
+ end
11
+
12
+ When do
13
+ @run_count = 0
14
+ @values = []
15
+ (1..10).each{@values << in_every.execute("no arg")}
16
+ end
17
+
18
+ Then { @run_count.should eql 4 }
19
+ Then { @values.compact.should eql [1,2,3,4] }
20
+ end
21
+
22
+ context "when executing more times than specified" do
23
+ Given(:in_every) do
24
+ 5.times.in_every(10) do
25
+ @run_count += 1
26
+ end
27
+ end
28
+
29
+ When do
30
+ @run_count = 0
31
+ (1..20).each{in_every.execute}
32
+ end
33
+
34
+ Then { @run_count.should eql 8 }
35
+ end
36
+
37
+ context "when the specified time is higher than the execute number" do
38
+ Then do
39
+ lambda{ 10.times.in_every(3){} }.should raise_error(InEvery::SetupException, "occurancies must be greater than executions")
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,11 @@
1
+ require 'rspec'
2
+ require 'rspec/given'
3
+ require 'in_every'
4
+
5
+ # Requires supporting files with custom matchers and macros, etc,
6
+ # in ./support/ and its subdirectories.
7
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
8
+
9
+ RSpec.configure do |config|
10
+
11
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: in_every
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Colin Gemmell
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70253016785600 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.8.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70253016785600
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec-given
27
+ requirement: &70253016783920 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70253016783920
36
+ - !ruby/object:Gem::Dependency
37
+ name: yard
38
+ requirement: &70253016782980 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '0.7'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70253016782980
47
+ - !ruby/object:Gem::Dependency
48
+ name: rdoc
49
+ requirement: &70253016782120 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.12'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70253016782120
58
+ - !ruby/object:Gem::Dependency
59
+ name: bundler
60
+ requirement: &70253016780700 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: 1.1.0
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70253016780700
69
+ - !ruby/object:Gem::Dependency
70
+ name: jeweler
71
+ requirement: &70253016779980 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: 1.8.3
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *70253016779980
80
+ description: ! 'In every will randomly execute the supplied block of code randomly.
81
+ It guarantees the code will be executed the given number of times in the code. '
82
+ email: pythonandchips@gmail.com
83
+ executables: []
84
+ extensions: []
85
+ extra_rdoc_files:
86
+ - LICENSE.txt
87
+ - README.rdoc
88
+ files:
89
+ - .document
90
+ - .rspec
91
+ - Gemfile
92
+ - Gemfile.lock
93
+ - LICENSE.txt
94
+ - README.rdoc
95
+ - Rakefile
96
+ - VERSION
97
+ - in_every.gemspec
98
+ - lib/in_every.rb
99
+ - lib/in_every/enumerator_extensions.rb
100
+ - spec/in_every/enumerator_extensions_spec.rb
101
+ - spec/in_every_spec.rb
102
+ - spec/spec_helper.rb
103
+ homepage: http://github.com/pythonandchips/in_every
104
+ licenses:
105
+ - MIT
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ! '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ segments:
117
+ - 0
118
+ hash: 3656177316055348041
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 1.8.15
128
+ signing_key:
129
+ specification_version: 3
130
+ summary: execute code x number of time in every y number of atempts
131
+ test_files: []