active_decimal 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e7db1178cef7189f1b8341edb5027d752167b4b1
4
+ data.tar.gz: 136c0f1b2ecd9479c19a0c3ca1b38bf9cadad20c
5
+ SHA512:
6
+ metadata.gz: 0ce2cbcde9a1dabf754435b7f9d368e8b10820f447134710c469f2de7d2b70f632b449466d2f9cc059a725f1155293a5f1c169c2ec714f09535231e90678594e
7
+ data.tar.gz: 70ce3d5fff9e5b840fa188753d1873d0664d14b3386d48c3057550a9171a862808651323e7420fc23f20e58a4843618793fafcd4d67fc96150ebf75abbba9983
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in active_decimal.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Mike Kendall
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # ActiveDecimal - Word-number mapping for human readable code
2
+
3
+ When working with numbers there are a number of slippery slopes. When dealing with large numbers, for example, you can accidentally put too many or too few zeros. It's dangerous to be typing out novemdecillions by hand. ActiveDecimal to the rescue. Bill Gates isn't worth $`78200000000`, he's worth $`78.2.billion`.
4
+
5
+ In addition to large numbers, ActiveDecimal will also make small numbers easy to manage and human readable. Rather than writing `0.125` or `1.0/8` (don't you hate adding `.0` to make something a float?) you can write `1.eighth`. Or if you're talking about statistics in your DnD game, you could use `4.twentieths`.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'active_decimal'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install active_decimal
20
+
21
+ ## Usage
22
+
23
+ 2.0.0-p247 :001 > require 'active_decimal'
24
+ => true
25
+ 2.0.0-p247 :002 > 4.thousand
26
+ => 4000
27
+ 2.0.0-p247 :003 > 5.fifths
28
+ => 1
29
+ 2.0.0-p247 :004 > 1.twentieth
30
+ => (1/20)
31
+ 2.0.0-p247 :005 > 6.googol
32
+ => 60000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
33
+ 2.0.0-p247 :006 > 3.8.billion
34
+ => 3800000000
35
+
36
+ You might even want to use it with money~
37
+
38
+ 2.0.0-p247 :010 > require 'money'
39
+ => true
40
+ 2.0.0-p247 :011 > bill = 78.1.billion.to_money
41
+ => #<Money fractional:7810000000000 currency:USD>
42
+ 2.0.0-p247 :012 > salary = 1.million.to_money
43
+ => #<Money fractional:100000000 currency:USD>
44
+
45
+ Though I like it better like this...
46
+
47
+ 2.0.0-p247 :016 > class Numeric
48
+ 2.0.0-p247 :017?> alias_method :dollars, :to_money
49
+ 2.0.0-p247 :018?> end
50
+ => Numeric
51
+ 2.0.0-p247 :019 >
52
+ 2.0.0-p247 :020 > bill += 5.trillion.dollars
53
+ => #<Money fractional:507810000000000 currency:USD>
54
+
55
+ ## Contributing
56
+
57
+ 1. Fork it
58
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
59
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
60
+ 4. Push to the branch (`git push origin my-new-feature`)
61
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -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 'active_decimal/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_decimal"
8
+ spec.version = ActiveDecimal::VERSION
9
+ spec.authors = ["Mike Kendall"]
10
+ spec.email = ["zenkalia@gmail.com"]
11
+ spec.description = %q{Word-number mapping for human readable code}
12
+ spec.summary = %q{Word-number mapping for human readable code}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "pry"
25
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveDecimal
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,130 @@
1
+ require "active_decimal/version"
2
+
3
+ module ActiveDecimal
4
+ BIG_NUMBERS = {
5
+ :thousand => 10**3,
6
+ :million => 10**6,
7
+ :billion => 10**9,
8
+ :trillion => 10**12,
9
+ :quadrillion => 10**15,
10
+ :quintillion => 10**18,
11
+ :sextillion => 10**21,
12
+ :septillion => 10**24,
13
+ :octillion => 10**27,
14
+ :nonillion => 10**30,
15
+ :decillion => 10**33,
16
+ :undecillion => 10**36,
17
+ :duodecillion => 10**39,
18
+ :tredecillion => 10**42,
19
+ :quattuordecillion => 10**45,
20
+ :quindecillion => 10**48,
21
+ :quinquadecillion => 10**48,
22
+ :sexdecillion => 10**51,
23
+ :sedecillion => 10**51,
24
+ :septendecillion => 10**54,
25
+ :octodecillion => 10**57,
26
+ :novemdecillion => 10**60,
27
+ :novendecillion => 10**60,
28
+ :vigintillion => 10**63,
29
+ :centillion => 10**303,
30
+ :googol => 10**100,
31
+ :dozen => 12,
32
+ :bakers_dozen => 13,
33
+ :gross => 144,
34
+ :small_gross => 120,
35
+ :great_gross => 1728
36
+ }
37
+
38
+ SINGULAR_SMALL_NUMBERS = {
39
+ :half => 2,
40
+ :third => 3,
41
+ :quarter => 4,
42
+ :fourth => 4,
43
+ :fifth => 5,
44
+ :sixth => 6,
45
+ :seventh => 7,
46
+ :eighth => 8,
47
+ :ninth => 9,
48
+ :tenth => 10,
49
+ :eleventh => 11,
50
+ :twelth => 12,
51
+ :thirteen => 13,
52
+ :fourteenth => 14,
53
+ :fifteenth => 15,
54
+ :sixteenth => 16,
55
+ :seventeenth => 17,
56
+ :eighteenth => 18,
57
+ :nineteenth => 19,
58
+ :twentieth => 20,
59
+ :thirtieth => 30,
60
+ :fourtieth => 40,
61
+ :fiftieth => 50,
62
+ :sixtieth => 60,
63
+ :seventieth => 70,
64
+ :eightieth => 80,
65
+ :ninetieth => 90,
66
+ :hundredth => 100,
67
+ }
68
+
69
+ PLURAL_SMALL_NUMBERS = {
70
+ :halves => 2,
71
+ :thirds => 3,
72
+ :quarters => 4,
73
+ :fourths => 4,
74
+ :fifths => 5,
75
+ :sixths => 6,
76
+ :sevenths => 7,
77
+ :eighths => 8,
78
+ :ninths => 9,
79
+ :tenths => 10,
80
+ :elevenths => 11,
81
+ :twelths => 12,
82
+ :thirteens => 13,
83
+ :fourteenths => 14,
84
+ :fifteenths => 15,
85
+ :sixteenths => 16,
86
+ :seventeenths => 17,
87
+ :eighteenths => 18,
88
+ :nineteenths => 19,
89
+ :twentieths => 20,
90
+ :thirtieths => 30,
91
+ :fourtieths => 40,
92
+ :fiftieths => 50,
93
+ :sixtieths => 60,
94
+ :seventieths => 70,
95
+ :eightieths => 80,
96
+ :ninetieths => 90,
97
+ :hundredths => 100,
98
+ }
99
+
100
+ class BadGrammar < StandardError
101
+ end
102
+ end
103
+
104
+ class Numeric
105
+ def method_missing(meth, *args, &block)
106
+
107
+ if ActiveDecimal::BIG_NUMBERS[meth]
108
+ val = self * ActiveDecimal::BIG_NUMBERS[meth]
109
+ return val.to_i == val ? val.to_i : val
110
+ end
111
+
112
+ if ActiveDecimal::SINGULAR_SMALL_NUMBERS[meth]
113
+ if self <= 1 and self > 0
114
+ return Rational(self, ActiveDecimal::SINGULAR_SMALL_NUMBERS[meth])
115
+ else
116
+ raise ActiveDecimal::BadGrammar
117
+ end
118
+ end
119
+
120
+ if ActiveDecimal::PLURAL_SMALL_NUMBERS[meth]
121
+ if self > 1 or self <= 0
122
+ return Rational(self, ActiveDecimal::PLURAL_SMALL_NUMBERS[meth])
123
+ else
124
+ raise ActiveDecimal::BadGrammar
125
+ end
126
+ end
127
+
128
+ super
129
+ end
130
+ end
@@ -0,0 +1,60 @@
1
+ require 'active_decimal'
2
+
3
+ describe Numeric do
4
+ describe 'numbers getting bigger' do
5
+ it 'lets you count thousands' do
6
+ expect( 3.thousand ).to eq(3000)
7
+ end
8
+ it 'lets you count millions' do
9
+ expect( 5.million ).to eq(5000000)
10
+ end
11
+ it 'lets you count billions' do
12
+ expect( 9.billion ).to eq(9 * 10**9)
13
+ end
14
+ it 'lets you count trillions' do
15
+ expect( 5.3.trillion ).to eq(5.3 * 10**12)
16
+ end
17
+ it 'lets you count even bigger ones' do
18
+ expect( 3.4.googol ).to eq(3.4 * 10**100)
19
+ end
20
+ it 'gives you integers when it can' do
21
+ expect( 3.4.billion.class ).to eq( Fixnum )
22
+ end
23
+ it 'lets you go to the bakery' do
24
+ expect( 5.dozen ).to eq( 60 )
25
+ expect( 2.bakers_dozen ).to eq( 26 )
26
+ end
27
+ end
28
+
29
+ describe 'numbers getting smaller' do
30
+ it 'lets you make quarters' do
31
+ expect( 1.fourth ).to eq( 0.25 )
32
+ expect( 1.quarter ).to eq( 0.25 )
33
+ end
34
+ it 'lets you make teen fractions' do
35
+ expect( 1.fifteenth ).to eq( 1.0 / 15 )
36
+ expect( 1.sixteenth ).to eq( 1.0 / 16 )
37
+ end
38
+ it 'does not let you make grammar errors' do
39
+ expect{ 4.sixteenth }.to raise_error ActiveDecimal::BadGrammar
40
+ expect{ 9.half }.to raise_error ActiveDecimal::BadGrammar
41
+ expect{ 1.fourths }.to raise_error ActiveDecimal::BadGrammar
42
+ expect{ 1.hundredths }.to raise_error ActiveDecimal::BadGrammar
43
+ end
44
+ it 'lets you make plural fractions' do
45
+ expect( 5.fourths ).to eq( 1.25 )
46
+ expect( 8.halves ).to eq( 4 )
47
+ end
48
+ end
49
+
50
+ describe 'math masturbation' do
51
+ it 'is commutative' do
52
+ expect( 4.million.fifths ).to eq( 4.fifths.million )
53
+ expect( 6.9.billion.trillion ).to eq( 6.9.trillion.billion )
54
+ end
55
+ it 'is distributive' do
56
+ expect( 4.billion + 5.billion ).to eq( (4+5).billion )
57
+ expect( 5.fourths - 8.fourths ).to eq( (5-8).fourths )
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,9 @@
1
+ $:.push File.expand_path( '..', __FILE__ )
2
+
3
+ require 'rspec'
4
+
5
+ ENV['RACK_ENV'] = 'test'
6
+
7
+ RSpec.configure do |config|
8
+ puts 'sup bro'
9
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_decimal
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Mike Kendall
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-24 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
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
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Word-number mapping for human readable code
70
+ email:
71
+ - zenkalia@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - active_decimal.gemspec
82
+ - lib/active_decimal.rb
83
+ - lib/active_decimal/version.rb
84
+ - spec/active_decimal_spec.rb
85
+ - spec/spec_helper.rb
86
+ homepage: ''
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.0.6
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Word-number mapping for human readable code
110
+ test_files:
111
+ - spec/active_decimal_spec.rb
112
+ - spec/spec_helper.rb