aeonscope-number_to_fraction 1.0.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.
- data/CHANGELOG.rdoc +3 -0
- data/LICENSE.rdoc +20 -0
- data/README.rdoc +45 -0
- data/Rakefile +49 -0
- data/VERSION.yml +4 -0
- data/lib/number_to_fraction.rb +56 -0
- data/spec/number_to_fraction_spec.rb +137 -0
- data/spec/spec_helper.rb +11 -0
- metadata +73 -0
data/CHANGELOG.rdoc
ADDED
data/LICENSE.rdoc
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Brooke Kuhlmann of {Berserk Technologies}[http://www.berserktech.com]
|
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,45 @@
|
|
1
|
+
= Overview
|
2
|
+
|
3
|
+
A helper that can format a number into a fraction. Unfortunately, this only handles the "pretty printing" of factions less than 1/10th at the moment (as a better algorithm needs to be used for more complex number cases).
|
4
|
+
|
5
|
+
= License
|
6
|
+
|
7
|
+
Copyright (c) 2009 Brooke Kuhlmann of {Berserk Technologies}[http://www.berserktech.com].
|
8
|
+
See the included LICENSE for more info.
|
9
|
+
|
10
|
+
= History
|
11
|
+
|
12
|
+
See the CHANGELOG file for more info.
|
13
|
+
|
14
|
+
= Requirements
|
15
|
+
|
16
|
+
1. {Ruby on Rails}[http://rubyonrails.org].
|
17
|
+
|
18
|
+
= Installation
|
19
|
+
|
20
|
+
Type the following from the command line to install:
|
21
|
+
|
22
|
+
* *UNIX*: sudo gem install aeonscope-number_to_function
|
23
|
+
* *Windows*: gem install aeonscope-number_to_function
|
24
|
+
|
25
|
+
Update your environment.rb file to include the new gem:
|
26
|
+
|
27
|
+
* config.gem "aeonscope-number_to_function", :lib => "number_to_function", :source => "http://gems.github.com"
|
28
|
+
|
29
|
+
= Usage
|
30
|
+
|
31
|
+
Example:
|
32
|
+
|
33
|
+
number_to_function 3.75
|
34
|
+
|
35
|
+
Result:
|
36
|
+
|
37
|
+
3 3/4
|
38
|
+
|
39
|
+
View the Rspec test suite for more usage and examples.
|
40
|
+
|
41
|
+
= Contact/Feedback/Issues
|
42
|
+
|
43
|
+
* {Berserk Technologies}[http://www.berserktech.com] - Company web site.
|
44
|
+
* Aeonscope[http://www.aeonscope.net] - Personal web site.
|
45
|
+
* Twitter[http://www.twitter.com/Aeonscope] - Short bursts of insight and/or noise.
|
data/Rakefile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "number_to_fraction"
|
8
|
+
gem.summary = "A helper that can format a number into a fraction."
|
9
|
+
gem.description = "A helper that can format a number into a fraction."
|
10
|
+
gem.authors = ["Brooke Kuhlmann"]
|
11
|
+
gem.email = "aeonscope@gmail.com"
|
12
|
+
gem.homepage = "http://github.com/aeonscope/number_to_fraction"
|
13
|
+
gem.required_ruby_version = ">= 1.8.6"
|
14
|
+
gem.add_dependency "rails", ">= 2.3.2"
|
15
|
+
gem.rdoc_options << "CHANGELOG.rdoc"
|
16
|
+
gem.files = FileList["[A-Z]*", "{bin,lib,generators,rails_generators,test,spec}/**/*"]
|
17
|
+
end
|
18
|
+
|
19
|
+
rescue LoadError
|
20
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
21
|
+
end
|
22
|
+
|
23
|
+
require 'spec/rake/spectask'
|
24
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
25
|
+
spec.libs << 'lib' << 'spec'
|
26
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
27
|
+
end
|
28
|
+
|
29
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
30
|
+
spec.libs << 'lib' << 'spec'
|
31
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
32
|
+
spec.rcov = true
|
33
|
+
end
|
34
|
+
|
35
|
+
task :default => :spec
|
36
|
+
|
37
|
+
require 'rake/rdoctask'
|
38
|
+
Rake::RDocTask.new do |rdoc|
|
39
|
+
if File.exist?('VERSION')
|
40
|
+
version = File.read('VERSION')
|
41
|
+
else
|
42
|
+
version = ""
|
43
|
+
end
|
44
|
+
|
45
|
+
rdoc.rdoc_dir = 'rdoc'
|
46
|
+
rdoc.title = "number_to_fraction #{version}"
|
47
|
+
rdoc.rdoc_files.include('README*')
|
48
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
+
end
|
data/VERSION.yml
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
module ActionView
|
2
|
+
module Helpers #:nodoc:
|
3
|
+
module NumberHelper
|
4
|
+
# Formats a +number+ into a fraction string (i.e. 1 1/3 or 2/9). You can customize the format
|
5
|
+
# in the +options+ hash.
|
6
|
+
#
|
7
|
+
# *NOTE*: This function only supports the "pretty printing" of factions less than 1/10th.
|
8
|
+
#
|
9
|
+
# ==== Options
|
10
|
+
# * *precision* - Options. Sets the level of precision. Defaults to 3.
|
11
|
+
#
|
12
|
+
# ==== Examples
|
13
|
+
# number_to_fraction 0.25 # => 1/4
|
14
|
+
# number_to_fraction 1.375 # => 1 3/8
|
15
|
+
# number_to_fraction 5.22 # => 5 2/9
|
16
|
+
# number_to_fraction 3.55 :precision => 2 # 3 => 5/9
|
17
|
+
def number_to_fraction number = '', options = {}
|
18
|
+
number = number.to_s.strip
|
19
|
+
options.reverse_merge! :precision => 3
|
20
|
+
whole, numerator, denominator = nil
|
21
|
+
if number.include? '.'
|
22
|
+
whole, numerator = number.split '.'
|
23
|
+
rational = Rational(numerator.ljust(options[:precision], '0').to_i, (10 ** options[:precision]))
|
24
|
+
# Check for repeating/complex decimals - yeah, this solution is lame.
|
25
|
+
case
|
26
|
+
when !numerator.match(/^33+/).nil? then numerator, denominator = 1, 3
|
27
|
+
when !numerator.match(/^66+/).nil? then numerator, denominator = 2, 3
|
28
|
+
when !numerator.match(/^1{1}6+/).nil? then numerator, denominator = 1, 6
|
29
|
+
when !numerator.match(/^8{1}3+/).nil? then numerator, denominator = 5, 6
|
30
|
+
when !numerator.match(/^142/).nil? then numerator, denominator = 1, 7
|
31
|
+
when !numerator.match(/^285/).nil? then numerator, denominator = 2, 7
|
32
|
+
when !numerator.match(/^428/).nil? then numerator, denominator = 3, 7
|
33
|
+
when !numerator.match(/^571/).nil? then numerator, denominator = 4, 7
|
34
|
+
when !numerator.match(/^714/).nil? then numerator, denominator = 5, 7
|
35
|
+
when !numerator.match(/^857/).nil? then numerator, denominator = 6, 7
|
36
|
+
when !numerator.match(/^125/).nil? then numerator, denominator = 1, 8
|
37
|
+
when !numerator.match(/^375/).nil? then numerator, denominator = 3, 8
|
38
|
+
when !numerator.match(/^625/).nil? then numerator, denominator = 5, 8
|
39
|
+
when !numerator.match(/^875/).nil? then numerator, denominator = 7, 8
|
40
|
+
when !numerator.match(/^11+/).nil? then numerator, denominator = 1, 9
|
41
|
+
when !numerator.match(/^22+/).nil? then numerator, denominator = 2, 9
|
42
|
+
when !numerator.match(/^44+/).nil? then numerator, denominator = 4, 9
|
43
|
+
when !numerator.match(/^55+/).nil? then numerator, denominator = 5, 9
|
44
|
+
when !numerator.match(/^77+/).nil? then numerator, denominator = 7, 9
|
45
|
+
when !numerator.match(/^88+/).nil? then numerator, denominator = 8, 9
|
46
|
+
else numerator, denominator = rational.numerator, rational.denominator
|
47
|
+
end
|
48
|
+
else
|
49
|
+
whole = number
|
50
|
+
end
|
51
|
+
whole = nil if whole == '0' && numerator
|
52
|
+
[whole, [numerator, denominator].compact * '/'].compact.join(' ').strip
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,137 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "Number to Fraction" do
|
4
|
+
include ActionView::Helpers::NumberHelper
|
5
|
+
|
6
|
+
it "should be blank when nil" do
|
7
|
+
number_to_fraction(nil).should == ''
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should be blank when blank" do
|
11
|
+
number_to_fraction('').should == ''
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should be 0" do
|
15
|
+
number_to_fraction(0).should == '0'
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should be 1" do
|
19
|
+
number_to_fraction(1).should == '1'
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should be 1/3" do
|
23
|
+
number_to_fraction(0.33).should == "1/3"
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should be 2/3" do
|
27
|
+
number_to_fraction(0.66).should == "2/3"
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should be 1/4" do
|
31
|
+
number_to_fraction(0.25).should == "1/4"
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should be 3/4" do
|
35
|
+
number_to_fraction(0.75).should == "3/4"
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should be 1/5" do
|
39
|
+
number_to_fraction(0.2).should == "1/5"
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should be 2/5" do
|
43
|
+
number_to_fraction(0.4).should == "2/5"
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should be 3/5" do
|
47
|
+
number_to_fraction(0.6).should == "3/5"
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should be 4/5" do
|
51
|
+
number_to_fraction(0.8).should == "4/5"
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should be 1/6" do
|
55
|
+
number_to_fraction(0.166).should == "1/6"
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should be 5/6" do
|
59
|
+
number_to_fraction(0.833).should == "5/6"
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should be 1/7" do
|
63
|
+
number_to_fraction(0.142).should == "1/7"
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should be 2/7" do
|
67
|
+
number_to_fraction(0.285).should == "2/7"
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should be 3/7" do
|
71
|
+
number_to_fraction(0.428).should == "3/7"
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should be 4/7" do
|
75
|
+
number_to_fraction(0.571).should == "4/7"
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should be 5/7" do
|
79
|
+
number_to_fraction(0.714).should == "5/7"
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should be 6/7" do
|
83
|
+
number_to_fraction(0.857).should == "6/7"
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should be 1/8" do
|
87
|
+
number_to_fraction(0.125).should == "1/8"
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should be 3/8" do
|
91
|
+
number_to_fraction(0.375).should == "3/8"
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should be 5/8" do
|
95
|
+
number_to_fraction(0.625).should == "5/8"
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should be 7/8" do
|
99
|
+
number_to_fraction(0.875).should == "7/8"
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should be 1/9" do
|
103
|
+
number_to_fraction(0.11).should == "1/9"
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should be 2/9" do
|
107
|
+
number_to_fraction(0.22).should == "2/9"
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should be 4/9" do
|
111
|
+
number_to_fraction(0.44).should == "4/9"
|
112
|
+
end
|
113
|
+
|
114
|
+
it "should be 5/9" do
|
115
|
+
number_to_fraction(0.55).should == "5/9"
|
116
|
+
end
|
117
|
+
|
118
|
+
it "should be 7/9" do
|
119
|
+
number_to_fraction(0.77).should == "7/9"
|
120
|
+
end
|
121
|
+
|
122
|
+
it "should be 8/9" do
|
123
|
+
number_to_fraction(0.88).should == "8/9"
|
124
|
+
end
|
125
|
+
|
126
|
+
it "should be 1 2/3" do
|
127
|
+
number_to_fraction(1.66).should == "1 2/3"
|
128
|
+
end
|
129
|
+
|
130
|
+
it "should be 55 2/9" do
|
131
|
+
number_to_fraction(55.22).should == "55 2/9"
|
132
|
+
end
|
133
|
+
|
134
|
+
it "should be 10203 3/8" do
|
135
|
+
number_to_fraction(10203.375).should == "10203 3/8"
|
136
|
+
end
|
137
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'active_support'
|
6
|
+
require 'number_to_fraction'
|
7
|
+
require 'spec'
|
8
|
+
require 'spec/autorun'
|
9
|
+
|
10
|
+
Spec::Runner.configure do |config|
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: aeonscope-number_to_fraction
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brooke Kuhlmann
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-08-29 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rails
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.3.2
|
24
|
+
version:
|
25
|
+
description: A helper that can format a number into a fraction.
|
26
|
+
email: aeonscope@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE.rdoc
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- CHANGELOG.rdoc
|
36
|
+
- LICENSE.rdoc
|
37
|
+
- README.rdoc
|
38
|
+
- Rakefile
|
39
|
+
- VERSION.yml
|
40
|
+
- lib/number_to_fraction.rb
|
41
|
+
- spec/number_to_fraction_spec.rb
|
42
|
+
- spec/spec_helper.rb
|
43
|
+
has_rdoc: false
|
44
|
+
homepage: http://github.com/aeonscope/number_to_fraction
|
45
|
+
licenses:
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options:
|
48
|
+
- --charset=UTF-8
|
49
|
+
- CHANGELOG.rdoc
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: 1.8.6
|
57
|
+
version:
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: "0"
|
63
|
+
version:
|
64
|
+
requirements: []
|
65
|
+
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.3.5
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: A helper that can format a number into a fraction.
|
71
|
+
test_files:
|
72
|
+
- spec/number_to_fraction_spec.rb
|
73
|
+
- spec/spec_helper.rb
|