randomevent 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6155df4b607a596457e2e9fa7236aacb9266cde4
4
+ data.tar.gz: 2141bde33c24e95967e952e72ab6003bbd23f79c
5
+ SHA512:
6
+ metadata.gz: 547f343ea0d6b03b9e67a5e06dea3668a5841e7b453a31ab5cf09cd96b98d584c28c0c0bf2eea40df9c71a471736ecd51f312bc7a2a723bf60191f9170a601f6
7
+ data.tar.gz: 7e228e419e526a3acd49de84290c6c356090bc42a0aeea056380c78b3744bf820889d8be0494da9892c3f2be457930e191e0ee30f83aafda5cd6ea05d041d252
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in randomevent.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Nathan Reed
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # Randomevent
2
+
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'randomevent'
10
+ ```
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install randomevent
19
+
20
+ ## Usage
21
+
22
+
23
+
24
+ ## Development
25
+
26
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
27
+
28
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
29
+
30
+ ## Contributing
31
+
32
+ 1. Fork it ( https://github.com/[my-github-username]/randomevent/fork )
33
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
34
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
35
+ 4. Push to the branch (`git push origin my-new-feature`)
36
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ t.pattern = "test/*_test.rb"
7
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "randomevent"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ module Randomevent
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,69 @@
1
+ require "randomevent/version"
2
+
3
+ #
4
+ # RandomEvent - a DSL class to make it easy to execute random events
5
+ #
6
+ # Use the chance method inside the constructor to add an chance 'path'
7
+ # the probabilty passed in should be between 0 and 1. You can also pass
8
+ # :else. This will be executed if no other blocks are hit
9
+ #
10
+ # Example:
11
+ #
12
+ # # half of the time it will output hello, 25% of the time goodbye
13
+ # # and 25 % of the time the default will be executed
14
+ # RandomEvent.new do |r|
15
+ # r.chance 0.5 do
16
+ # puts 'hello'
17
+ # end
18
+ #
19
+ # r.chance 0.25 do
20
+ # puts 'goodbye'
21
+ # end
22
+ #
23
+ # r.chance :else do
24
+ # puts '-'
25
+ # end
26
+ # end
27
+ #
28
+ #
29
+ class RandomEvent
30
+ def initialize
31
+ @paths = []
32
+ @else_block = nil
33
+
34
+ # run the DSL
35
+ yield self
36
+ self.execute
37
+ end
38
+
39
+ def chance(prob, &block)
40
+ if prob == :else
41
+ @else_block = block
42
+ elsif prob.kind_of? Numeric
43
+ @paths.push({:p => prob, :block => block})
44
+ end
45
+ end
46
+
47
+ def execute
48
+ sum_prob = 0
49
+ v = rand()
50
+
51
+ # get the sum of the hash, and make sure the probabilites add up
52
+ total_p = @paths.map {|path| path[:p] }.inject(0, :+)
53
+ raise 'total probability is > 1.0' if total_p > 1
54
+
55
+ @paths.each do |path|
56
+ sum_prob += path[:p]
57
+
58
+ if v < sum_prob
59
+ path[:block].call
60
+ return
61
+ end
62
+ end
63
+
64
+ # we didn't hit anything... call the else block if we have
65
+ # one
66
+ @else_block.call if !@else_block.nil?
67
+
68
+ end
69
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'randomevent/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "randomevent"
8
+ spec.version = Randomevent::VERSION
9
+ spec.authors = ["Nathan Reed"]
10
+ spec.email = ["reednj@gmail.com"]
11
+
12
+ spec.summary = %q{A Gem to execute a block from a set based on a certain probability}
13
+ spec.homepage = "http://github.com/reednj/randomevent"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.9"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "minitest"
25
+ end
@@ -0,0 +1,91 @@
1
+
2
+ require 'randomevent'
3
+ require 'minitest/autorun'
4
+
5
+ class RandomEventTest < Minitest::Test
6
+
7
+ def test_default_event
8
+ was_run = false
9
+
10
+ RandomEvent.new do |r|
11
+ r.chance :else do
12
+ was_run = true
13
+ end
14
+ end
15
+
16
+ assert was_run, 'default block not executed'
17
+ end
18
+
19
+ def test_single_event
20
+ was_run = false
21
+
22
+ RandomEvent.new do |r|
23
+ r.chance 0.5 do
24
+ was_run = true
25
+ end
26
+ r.chance 0.5 do
27
+ was_run = true
28
+ end
29
+ end
30
+
31
+ assert was_run, 'block not executed'
32
+ end
33
+
34
+ def test_zero_prob
35
+ was_run = false
36
+
37
+ RandomEvent.new do |r|
38
+ r.chance 0.0 do
39
+ was_run = true
40
+ end
41
+ end
42
+
43
+ assert !was_run, 'block with zero probablity executed'
44
+ end
45
+
46
+ def test_many_runs
47
+ default_chance = 1.0/100.0
48
+ runs_required = 10000
49
+ expected = (default_chance*runs_required)
50
+
51
+ exec_count = 0
52
+ else_count = 0
53
+
54
+ # ok this test is a bit non-deterministic, because we don't know
55
+ # exactly how many times the events will be triggered, but we can be
56
+ # pretty confident it is going to fall within some sort of range, so
57
+ # we will run assertions against that
58
+ (0...runs_required).each do
59
+
60
+ RandomEvent.new do |r|
61
+ r.chance default_chance do
62
+ exec_count += 1
63
+ end
64
+
65
+ r.chance :else do
66
+ else_count += 1
67
+ end
68
+ end
69
+ end
70
+
71
+ assert exec_count > expected * 0.5, "#{exec_count} executions - expected around #{expected}"
72
+ assert exec_count < expected * 2.0, "#{exec_count} executions - expected around #{expected}"
73
+ assert exec_count + else_count == runs_required, "totals runs should equal #{runs_required}, got #{exec_count + else_count}"
74
+ end
75
+
76
+ def test_prob_more_than_1
77
+ # here the probablity of the events adds up to more than
78
+ # 1 - this should throw an error
79
+ assert_raises RuntimeError do
80
+ RandomEvent.new do |r|
81
+ r.chance 0.8 do
82
+ was_run = true
83
+ end
84
+ r.chance 0.5 do
85
+ was_run = true
86
+ end
87
+ end
88
+ end
89
+ end
90
+
91
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: randomevent
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Reed
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - reednj@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - bin/console
68
+ - bin/setup
69
+ - lib/randomevent.rb
70
+ - lib/randomevent/version.rb
71
+ - randomevent.gemspec
72
+ - test/randomevent_test.rb
73
+ homepage: http://github.com/reednj/randomevent
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.4.5
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: A Gem to execute a block from a set based on a certain probability
97
+ test_files:
98
+ - test/randomevent_test.rb