linkingpaths-alea 0.0.2

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/HISTORY ADDED
@@ -0,0 +1,4 @@
1
+ == 0.0.1 2008-09-17
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Linking Paths
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/Manifest.txt ADDED
@@ -0,0 +1,19 @@
1
+ HISTORY
2
+ LICENSE
3
+ Manifest.txt
4
+ POSTINSTALL
5
+ README.markdown
6
+ Rakefile
7
+ config/hoe.rb
8
+ config/requirements.rb
9
+ lib/alea.rb
10
+ lib/alea/kernel.rb
11
+ lib/alea/numeric.rb
12
+ lib/alea/percentage.rb
13
+ lib/alea/range.rb
14
+ lib/alea/version.rb
15
+ test/test_helper.rb
16
+ test/test_kernel.rb
17
+ test/test_numeric.rb
18
+ test/test_percentage.rb
19
+ test/test_range.rb
data/POSTINSTALL ADDED
@@ -0,0 +1,3 @@
1
+
2
+ For more information on alea, see http://github.com/linkingpaths/alea
3
+
data/README.markdown ADDED
@@ -0,0 +1,71 @@
1
+ alea
2
+ ============
3
+ alea is a tiny gem that aim to bring some aleatory behavior to ruby Kernel.
4
+
5
+ > Alea - Greek soldier (From Wikipedia, the free encyclopedia):
6
+ "Alea was a Greek soldier who reputedly [invented the dicing game](http://books.google.co.uk/books?id=7rEkOX8NmM0C&pg=RA1-PA61&lpg=RA1-PA61&dq=disis+occidens+adam&source=web&ots=0MbgJ4ioAf&sig=Gu5-vxetefyJJeB8wTuGPzzFZQo&hl=en&sa=X&oi=book_result&resnum=1&ct=result#PRA1-PA60,M1) ['Tabula'](http://en.wikipedia.org/wiki/Tabula).This is the origin of the name aléatoire, meaning random.
7
+
8
+ Install
9
+ -------
10
+
11
+ ´sudo gem install linkingpaths-alea´
12
+
13
+ The junk
14
+ --------
15
+ <pre>
16
+ require 'alea'
17
+
18
+ # Add between 10 and 25 friends to a user
19
+ @user.add_friends rand_within(10..25)
20
+
21
+ # 75% chances of this happens
22
+ frequently do
23
+ update_profile(@user)
24
+ end
25
+
26
+ # 65% chances of this happens
27
+ probably do
28
+ @group.activate!
29
+ end
30
+
31
+ # 20% chances of this happens
32
+ rarely do
33
+ generate_404
34
+ end
35
+
36
+ # 5% chances of this happens
37
+ almost_never do
38
+ grant_admin_privileges(@user)
39
+ end
40
+
41
+ # 95% chances of this happens
42
+ almost_always do
43
+ post_incorrect_data_for_captcha
44
+ end
45
+
46
+ # 50% chances of true
47
+ @group.moderated = maybe
48
+
49
+ # Calculate rounded percents
50
+ send_mail_to 20.percent.of(the_community)
51
+
52
+
53
+ </pre>
54
+
55
+ PS: I will probably give the option of set custom % via Alea::PROBABLY, Alea::RARELY etc. soon.
56
+
57
+ Credits
58
+ -------
59
+
60
+ This code is based and inspired on some chunks of code appeared on the always entertaining, brilliant and funny [project.ioni.st](http://project.ioni.st/) tumblelog.
61
+
62
+ More
63
+ -------
64
+
65
+ [http://github.com/linkingpaths/alea](http://github.com/linkingpaths/alea)
66
+
67
+ [http://github.com/linkingpaths/alea/wikis](http://github.com/linkingpaths/alea/wikis)
68
+
69
+
70
+ Copyright (c) 2008 Linking Paths, released under the MIT license
71
+
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require 'config/requirements'
2
+ require 'config/hoe' # setup Hoe + all gem configuration
3
+
4
+ Dir['tasks/**/*.rake'].each { |rake| load rake }
data/config/hoe.rb ADDED
@@ -0,0 +1,39 @@
1
+ require 'alea/version'
2
+
3
+ AUTHOR = 'Linking Paths' # can also be an array of Authors
4
+ EMAIL = "aitor@linkingpaths.com"
5
+ DESCRIPTION = "alea is a tiny gem that aim to bring some aleatory behavior to ruby Kernel."
6
+ GEM_NAME = 'alea'
7
+ HOMEPATH = "http://github.com/linkingpaths/alea"
8
+ EXTRA_DEPENDENCIES = []
9
+
10
+ REV = nil
11
+ # UNCOMMENT IF REQUIRED:
12
+ # REV = YAML.load(`svn info`)['Revision']
13
+ VERS = Alea::VERSION::STRING + (REV ? ".#{REV}" : "")
14
+ RDOC_OPTS = ['--quiet', '--title', 'alea documentation',
15
+ "--opname", "index.html",
16
+ "--line-numbers",
17
+ "--main", "README",
18
+ "--inline-source"]
19
+
20
+
21
+ # Generate all the Rake tasks
22
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
23
+ $hoe = Hoe.new(GEM_NAME, VERS) do |p|
24
+ p.developer(AUTHOR, EMAIL)
25
+ p.description = DESCRIPTION
26
+ p.summary = DESCRIPTION
27
+ p.url = HOMEPATH
28
+ p.test_globs = ["test/**/test_*.rb"]
29
+ p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
30
+
31
+ # == Optional
32
+ p.changes = p.paragraphs_of("HISTORY", 0..1).join("\n\n")
33
+ #p.extra_deps = EXTRA_DEPENDENCIES
34
+
35
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
36
+ end
37
+
38
+ CHANGES = $hoe.paragraphs_of('HISTORY', 0..1).join("\\n\\n")
39
+ $hoe.spec.post_install_message = File.open(File.dirname(__FILE__) + "/../POSTINSTALL").read rescue ""
@@ -0,0 +1,15 @@
1
+ require 'fileutils'
2
+ include FileUtils
3
+
4
+ require 'rubygems'
5
+ %w[rake hoe ].each do |req_gem|
6
+ begin
7
+ require req_gem
8
+ rescue LoadError
9
+ puts "This Rakefile requires the '#{req_gem}' RubyGem."
10
+ puts "Installation: gem install #{req_gem} -y"
11
+ exit
12
+ end
13
+ end
14
+
15
+ $:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
data/lib/alea.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'alea/percentage'
2
+ require 'alea/kernel'
3
+ require 'alea/numeric'
4
+ require 'alea/range'
@@ -0,0 +1,40 @@
1
+ module Kernel
2
+
3
+ def rand_within(range)
4
+ rand(range.max - range.min) + range.min
5
+ end
6
+
7
+ def maybe(percent = 50.percent, &block)
8
+ chances_of_this_happens 50.percent, &block
9
+ end
10
+
11
+ def frequently(&block)
12
+ chances_of_this_happens 75.percent, &block
13
+ end
14
+
15
+ def probably(&block)
16
+ chances_of_this_happens 65.percent, &block
17
+ end
18
+
19
+ def rarely(&block)
20
+ chances_of_this_happens 20.percent, &block
21
+ end
22
+
23
+ def almost_never(&block)
24
+ chances_of_this_happens 5.percent, &block
25
+ end
26
+
27
+ def almost_always(&block)
28
+ chances_of_this_happens 95.percent, &block
29
+ end
30
+
31
+ def chances_of_this_happens(percent = 50.percent, &block)
32
+ if block_given?
33
+ percent.chance.of &block
34
+ else
35
+ percent.chance.happens?
36
+ end
37
+
38
+ end
39
+
40
+ end
@@ -0,0 +1,7 @@
1
+ class Numeric
2
+
3
+ def percent
4
+ Percentage.new(self)
5
+ end
6
+
7
+ end
@@ -0,0 +1,30 @@
1
+ class Percentage
2
+ attr_reader :amount
3
+ def initialize(amount)
4
+ @amount = amount
5
+ end
6
+
7
+ def chance
8
+ Chance.new(self)
9
+ end
10
+
11
+ def of(number, rounded=true)
12
+ p = number * amount / 100.0
13
+ rounded ? p.round : p
14
+ end
15
+
16
+ class Chance
17
+ attr_reader :odds, :happens
18
+ alias :happens? :happens
19
+
20
+ def initialize(percent)
21
+ @odds = percent.amount
22
+ @happens = @odds > Kernel.rand(100)
23
+ end
24
+
25
+ def of(&block)
26
+ yield if happens?
27
+ end
28
+ end
29
+
30
+ end
data/lib/alea/range.rb ADDED
@@ -0,0 +1,7 @@
1
+ class Range
2
+
3
+ def percent
4
+ Percentage.new(rand_within(self))
5
+ end
6
+
7
+ end
@@ -0,0 +1,9 @@
1
+ module Alea
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ TINY = 2
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
@@ -0,0 +1,2 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/alea'
@@ -0,0 +1,12 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestKernel < Test::Unit::TestCase
4
+
5
+ def test_rand_within
6
+ 100_000.times{
7
+ lottery = rand_within(10..20)
8
+ assert lottery >=10 && lottery <= 20
9
+ }
10
+ end
11
+
12
+ end
@@ -0,0 +1,14 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestNumeric < Test::Unit::TestCase
4
+
5
+ def test_regular_percent
6
+ assert_equal 10, 10.percent.of(100)
7
+ end
8
+ def test_rounded_percent
9
+ assert_equal 10, 10.percent.of(99)
10
+ end
11
+ def test_not_rounded_percent
12
+ assert_equal 9.9, 10.percent.of(99, false)
13
+ end
14
+ end
@@ -0,0 +1,13 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestPercentage < Test::Unit::TestCase
4
+
5
+ def test_amount
6
+ assert_equal 10, 10.percent.amount
7
+ assert_equal 10.5, 10.5.percent.amount
8
+ end
9
+ def test_odds
10
+ assert_equal 10, 10.percent.chance.odds
11
+ end
12
+
13
+ end
@@ -0,0 +1,9 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestRange < Test::Unit::TestCase
4
+
5
+ def test_regular_percent
6
+ estimation = (5..10).percent.of(100)
7
+ assert estimation >= 5 && estimation <= 10
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: linkingpaths-alea
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Linking Paths
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-10-04 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.7.0
23
+ version:
24
+ description: alea is a tiny gem that aim to bring some aleatory behavior to ruby Kernel.
25
+ email:
26
+ - aitor@linkingpaths.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - Manifest.txt
33
+ files:
34
+ - HISTORY
35
+ - LICENSE
36
+ - Manifest.txt
37
+ - POSTINSTALL
38
+ - README.markdown
39
+ - Rakefile
40
+ - config/hoe.rb
41
+ - config/requirements.rb
42
+ - lib/alea.rb
43
+ - lib/alea/kernel.rb
44
+ - lib/alea/numeric.rb
45
+ - lib/alea/percentage.rb
46
+ - lib/alea/range.rb
47
+ - lib/alea/version.rb
48
+ - test/test_helper.rb
49
+ - test/test_kernel.rb
50
+ - test/test_numeric.rb
51
+ - test/test_percentage.rb
52
+ - test/test_range.rb
53
+ has_rdoc: true
54
+ homepage: http://github.com/linkingpaths/alea
55
+ post_install_message: |+
56
+
57
+ For more information on alea, see http://github.com/linkingpaths/alea
58
+
59
+ rdoc_options:
60
+ - --main
61
+ - README.txt
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: "0"
75
+ version:
76
+ requirements: []
77
+
78
+ rubyforge_project: alea
79
+ rubygems_version: 1.2.0
80
+ signing_key:
81
+ specification_version: 2
82
+ summary: alea is a tiny gem that aim to bring some aleatory behavior to ruby Kernel.
83
+ test_files:
84
+ - test/test_helper.rb
85
+ - test/test_kernel.rb
86
+ - test/test_numeric.rb
87
+ - test/test_percentage.rb
88
+ - test/test_range.rb