percentise 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README +37 -0
- data/Rakefile +87 -0
- data/lib/percentise.rb +23 -0
- data/test/percentise_test.rb +40 -0
- data/test/test_helper.rb +8 -0
- metadata +73 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 [Matthew Rudy Jacobs]
|
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
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
Percentise
|
2
|
+
==========
|
3
|
+
|
4
|
+
A single way to produce consistent percentages
|
5
|
+
|
6
|
+
Example
|
7
|
+
=======
|
8
|
+
|
9
|
+
The obvious:
|
10
|
+
>> Percentise(50, 100)
|
11
|
+
=> 50.0
|
12
|
+
|
13
|
+
Error cases:
|
14
|
+
>> Percentise(nil, 100)
|
15
|
+
=> nil
|
16
|
+
>> Percentise(50, nil)
|
17
|
+
=> nil
|
18
|
+
>> Percentise(nil, nil)
|
19
|
+
=> nil
|
20
|
+
|
21
|
+
Don't worry about dividing by 0
|
22
|
+
|
23
|
+
>> Percentise(0,0)
|
24
|
+
=> 0.0
|
25
|
+
>> Percentise(1,0)
|
26
|
+
=> 100.0
|
27
|
+
>> Percentise(-1,0)
|
28
|
+
=> -100.0
|
29
|
+
|
30
|
+
Find the % difference
|
31
|
+
|
32
|
+
>> Percentise.diff(50, 100)
|
33
|
+
=> -50.0
|
34
|
+
>> Percentise.diff(100, 50)
|
35
|
+
=> 100.0
|
36
|
+
|
37
|
+
Copyright (c) 2010 [Matthew Rudy Jacobs], released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test Percentise.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.libs << 'test'
|
12
|
+
t.pattern = 'test/**/*_test.rb'
|
13
|
+
t.verbose = true
|
14
|
+
end
|
15
|
+
|
16
|
+
desc 'Generate documentation for Percentise.'
|
17
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
18
|
+
rdoc.rdoc_dir = 'rdoc'
|
19
|
+
rdoc.title = 'Percentise'
|
20
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
21
|
+
rdoc.rdoc_files.include('README')
|
22
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
23
|
+
end
|
24
|
+
|
25
|
+
require "rubygems"
|
26
|
+
require "rake/gempackagetask"
|
27
|
+
require "rake/rdoctask"
|
28
|
+
|
29
|
+
# This builds the actual gem. For details of what all these options
|
30
|
+
# mean, and other ones you can add, check the documentation here:
|
31
|
+
#
|
32
|
+
# http://rubygems.org/read/chapter/20
|
33
|
+
#
|
34
|
+
spec = Gem::Specification.new do |s|
|
35
|
+
|
36
|
+
# Change these as appropriate
|
37
|
+
s.name = "percentise"
|
38
|
+
s.version = "0.1.0"
|
39
|
+
s.summary = "Simple, consistent Percentises"
|
40
|
+
s.author = "Matthew Rudy Jacobs"
|
41
|
+
s.email = "MatthewRudyJacobs@gmail.com"
|
42
|
+
s.homepage = "https://github.com/matthewrudy/percentise"
|
43
|
+
|
44
|
+
s.has_rdoc = true
|
45
|
+
s.extra_rdoc_files = %w(README)
|
46
|
+
s.rdoc_options = %w(--main README)
|
47
|
+
|
48
|
+
# Add any extra files to include in the gem
|
49
|
+
s.files = %w(MIT-LICENSE Rakefile README) + Dir.glob("{test,lib}/**/*")
|
50
|
+
s.require_paths = ["lib"]
|
51
|
+
|
52
|
+
# If you want to depend on other gems, add them here, along with any
|
53
|
+
# relevant versions
|
54
|
+
# s.add_dependency("some_other_gem", "~> 0.1.0")
|
55
|
+
|
56
|
+
# If your tests use any gems, include them here
|
57
|
+
# s.add_development_dependency("mocha") # for example
|
58
|
+
end
|
59
|
+
|
60
|
+
# This task actually builds the gem. We also regenerate a static
|
61
|
+
# .gemspec file, which is useful if something (i.e. GitHub) will
|
62
|
+
# be automatically building a gem for this project. If you're not
|
63
|
+
# using GitHub, edit as appropriate.
|
64
|
+
#
|
65
|
+
# To publish your gem online, install the 'gemcutter' gem; Read more
|
66
|
+
# about that here: http://gemcutter.org/pages/gem_docs
|
67
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
68
|
+
pkg.gem_spec = spec
|
69
|
+
end
|
70
|
+
|
71
|
+
desc "Build the gemspec file #{spec.name}.gemspec"
|
72
|
+
task :gemspec do
|
73
|
+
file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
|
74
|
+
File.open(file, "w") {|f| f << spec.to_ruby }
|
75
|
+
end
|
76
|
+
|
77
|
+
# If you don't want to generate the .gemspec file, just remove this line. Reasons
|
78
|
+
# why you might want to generate a gemspec:
|
79
|
+
# - using bundler with a git source
|
80
|
+
# - building the gem without rake (i.e. gem build blah.gemspec)
|
81
|
+
# - maybe others?
|
82
|
+
task :package => :gemspec
|
83
|
+
|
84
|
+
desc 'Clear out RDoc and generated packages'
|
85
|
+
task :clean => [:clobber_rdoc, :clobber_package] do
|
86
|
+
rm "#{spec.name}.gemspec"
|
87
|
+
end
|
data/lib/percentise.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# Percentise
|
2
|
+
module Percentise
|
3
|
+
|
4
|
+
def self.this(top, bottom)
|
5
|
+
if top && bottom
|
6
|
+
if bottom == 0
|
7
|
+
top_sign = top.to_f <=> 0
|
8
|
+
top_sign * 100.0
|
9
|
+
else
|
10
|
+
100.0 * top.to_f / bottom.to_f
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.diff(top, bottom)
|
16
|
+
self.this(top - bottom, bottom)
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
def Percentise(*args)
|
22
|
+
Percentise.this(*args)
|
23
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'percentise'
|
3
|
+
|
4
|
+
class PercentiseTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
test "if top is nil returns nil" do
|
7
|
+
assert_equal nil, Percentise.this(nil, 20)
|
8
|
+
end
|
9
|
+
|
10
|
+
test "if bottom is nil returns nil" do
|
11
|
+
assert_equal nil, Percentise.this(20, nil)
|
12
|
+
end
|
13
|
+
|
14
|
+
test "if bottom is zero, and top is > 0, it is 100%" do
|
15
|
+
assert_equal 100.0, Percentise.this(20, 0)
|
16
|
+
end
|
17
|
+
|
18
|
+
test "if bottom and top are both zero it is 0%" do
|
19
|
+
assert_equal 0.0, Percentise.this(0, 0)
|
20
|
+
end
|
21
|
+
|
22
|
+
test "if bottom is zero, and top is < 0, it is -100%" do
|
23
|
+
assert_equal -100.0, Percentise.this(-20, 0)
|
24
|
+
end
|
25
|
+
|
26
|
+
test "Percentise.this" do
|
27
|
+
assert_equal 50.0, Percentise.this(1, 2)
|
28
|
+
assert_in_delta 33.3333, Percentise.this(7, 21), 1E-4
|
29
|
+
end
|
30
|
+
|
31
|
+
test "Percentise()" do
|
32
|
+
assert_equal 50.0, Percentise(1, 2)
|
33
|
+
assert_in_delta 33.3333, Percentise(7, 21), 1E-4
|
34
|
+
end
|
35
|
+
|
36
|
+
test "diff" do
|
37
|
+
assert_equal -50.0, Percentise.diff(50, 100)
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: percentise
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Matthew Rudy Jacobs
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-12-17 00:00:00 +08:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description:
|
23
|
+
email: MatthewRudyJacobs@gmail.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- README
|
30
|
+
files:
|
31
|
+
- MIT-LICENSE
|
32
|
+
- Rakefile
|
33
|
+
- README
|
34
|
+
- test/percentise_test.rb
|
35
|
+
- test/test_helper.rb
|
36
|
+
- lib/percentise.rb
|
37
|
+
has_rdoc: true
|
38
|
+
homepage: https://github.com/matthewrudy/percentise
|
39
|
+
licenses: []
|
40
|
+
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options:
|
43
|
+
- --main
|
44
|
+
- README
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
hash: 3
|
53
|
+
segments:
|
54
|
+
- 0
|
55
|
+
version: "0"
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 3
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.3.7
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: Simple, consistent Percentises
|
72
|
+
test_files: []
|
73
|
+
|