alea 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY +4 -0
- data/LICENSE +20 -0
- data/Manifest.txt +19 -0
- data/POSTINSTALL +3 -0
- data/README.markdown +71 -0
- data/Rakefile +22 -0
- data/VERSION +1 -0
- data/alea.gemspec +65 -0
- data/config/hoe.rb +39 -0
- data/config/requirements.rb +15 -0
- data/lib/alea/kernel.rb +40 -0
- data/lib/alea/numeric.rb +7 -0
- data/lib/alea/percentage.rb +30 -0
- data/lib/alea/range.rb +7 -0
- data/lib/alea/version.rb +9 -0
- data/lib/alea.rb +4 -0
- data/test/test_helper.rb +2 -0
- data/test/test_kernel.rb +12 -0
- data/test/test_numeric.rb +14 -0
- data/test/test_percentage.rb +13 -0
- data/test/test_range.rb +9 -0
- metadata +87 -0
data/HISTORY
ADDED
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
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,22 @@
|
|
1
|
+
#require 'config/requirements'
|
2
|
+
#require 'config/hoe' # setup Hoe + all gem configuration
|
3
|
+
#
|
4
|
+
#Dir['tasks/**/*.rake'].each { |rake| load rake }
|
5
|
+
|
6
|
+
|
7
|
+
|
8
|
+
begin
|
9
|
+
require 'jeweler'
|
10
|
+
Jeweler::Tasks.new do |gemspec|
|
11
|
+
gemspec.name = "alea"
|
12
|
+
gemspec.summary = "alea is a tiny gem that aim to bring some aleatory behavior to ruby Kernel."
|
13
|
+
gemspec.description = "alea is a tiny gem that aim to bring some aleatory behavior to ruby Kernel."
|
14
|
+
gemspec.email = "info@linkingpaths.com"
|
15
|
+
gemspec.homepage = "http://github.com/linkingpaths/alea"
|
16
|
+
gemspec.authors = ["Aitor Garcia"]
|
17
|
+
end
|
18
|
+
Jeweler::GemcutterTasks.new
|
19
|
+
rescue LoadError
|
20
|
+
puts "Jeweler not available. Install it with: sudo gem install jeweler -s http://gemcutter.org"
|
21
|
+
end
|
22
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/alea.gemspec
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{alea}
|
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 = ["Aitor Garcia"]
|
12
|
+
s.date = %q{2010-09-28}
|
13
|
+
s.description = %q{alea is a tiny gem that aim to bring some aleatory behavior to ruby Kernel.}
|
14
|
+
s.email = %q{info@linkingpaths.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.markdown"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
"HISTORY",
|
21
|
+
"LICENSE",
|
22
|
+
"Manifest.txt",
|
23
|
+
"POSTINSTALL",
|
24
|
+
"README.markdown",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"alea.gemspec",
|
28
|
+
"config/hoe.rb",
|
29
|
+
"config/requirements.rb",
|
30
|
+
"lib/alea.rb",
|
31
|
+
"lib/alea/kernel.rb",
|
32
|
+
"lib/alea/numeric.rb",
|
33
|
+
"lib/alea/percentage.rb",
|
34
|
+
"lib/alea/range.rb",
|
35
|
+
"lib/alea/version.rb",
|
36
|
+
"test/test_helper.rb",
|
37
|
+
"test/test_kernel.rb",
|
38
|
+
"test/test_numeric.rb",
|
39
|
+
"test/test_percentage.rb",
|
40
|
+
"test/test_range.rb"
|
41
|
+
]
|
42
|
+
s.homepage = %q{http://github.com/linkingpaths/alea}
|
43
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
44
|
+
s.require_paths = ["lib"]
|
45
|
+
s.rubygems_version = %q{1.3.6}
|
46
|
+
s.summary = %q{alea is a tiny gem that aim to bring some aleatory behavior to ruby Kernel.}
|
47
|
+
s.test_files = [
|
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
|
+
]
|
54
|
+
|
55
|
+
if s.respond_to? :specification_version then
|
56
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
57
|
+
s.specification_version = 3
|
58
|
+
|
59
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
60
|
+
else
|
61
|
+
end
|
62
|
+
else
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
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/kernel.rb
ADDED
@@ -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
|
data/lib/alea/numeric.rb
ADDED
@@ -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
data/lib/alea/version.rb
ADDED
data/lib/alea.rb
ADDED
data/test/test_helper.rb
ADDED
data/test/test_kernel.rb
ADDED
@@ -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
|
data/test/test_range.rb
ADDED
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: alea
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Aitor Garcia
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-09-28 00:00:00 +00:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: alea is a tiny gem that aim to bring some aleatory behavior to ruby Kernel.
|
22
|
+
email: info@linkingpaths.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- LICENSE
|
29
|
+
- README.markdown
|
30
|
+
files:
|
31
|
+
- HISTORY
|
32
|
+
- LICENSE
|
33
|
+
- Manifest.txt
|
34
|
+
- POSTINSTALL
|
35
|
+
- README.markdown
|
36
|
+
- Rakefile
|
37
|
+
- VERSION
|
38
|
+
- alea.gemspec
|
39
|
+
- config/hoe.rb
|
40
|
+
- config/requirements.rb
|
41
|
+
- lib/alea.rb
|
42
|
+
- lib/alea/kernel.rb
|
43
|
+
- lib/alea/numeric.rb
|
44
|
+
- lib/alea/percentage.rb
|
45
|
+
- lib/alea/range.rb
|
46
|
+
- lib/alea/version.rb
|
47
|
+
- test/test_helper.rb
|
48
|
+
- test/test_kernel.rb
|
49
|
+
- test/test_numeric.rb
|
50
|
+
- test/test_percentage.rb
|
51
|
+
- test/test_range.rb
|
52
|
+
has_rdoc: true
|
53
|
+
homepage: http://github.com/linkingpaths/alea
|
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
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
segments:
|
66
|
+
- 0
|
67
|
+
version: "0"
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
version: "0"
|
75
|
+
requirements: []
|
76
|
+
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 1.3.6
|
79
|
+
signing_key:
|
80
|
+
specification_version: 3
|
81
|
+
summary: alea is a tiny gem that aim to bring some aleatory behavior to ruby Kernel.
|
82
|
+
test_files:
|
83
|
+
- test/test_helper.rb
|
84
|
+
- test/test_kernel.rb
|
85
|
+
- test/test_numeric.rb
|
86
|
+
- test/test_percentage.rb
|
87
|
+
- test/test_range.rb
|