crazy_money 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.travis.yml +9 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README.md +50 -0
- data/Rakefile +8 -0
- data/bench/div.rb +15 -0
- data/crazy_money.gemspec +25 -0
- data/lib/crazy_money.rb +76 -0
- data/spec/crazy_money_spec.rb +118 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 22d4abd4ba8e797bdeabcd5602fce74b682e9b0f
|
4
|
+
data.tar.gz: 1a3ce939ff808b0ad91e8cb9c412aed640614b19
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8a76c26c72031391eb8bc928d203871e5c449b9c8af45066cb085d628b6f0533b340ab499630a96affb096dc52f0e36eba45110a6016582e1f04c726c2475a86
|
7
|
+
data.tar.gz: 9ccf86a03aee3de9717fe66e1b92035e5c9236fc2dc4bb988749ad48cb0d195aa8cda7cd959e199b8d4b8dc569d61531560c1c6536e4be1684d30f9d9c0b52a1
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013-2014 Bucky Box Limited
|
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,50 @@
|
|
1
|
+
# CrazyMoney
|
2
|
+
|
3
|
+
This is a simplier version of the Money class which comes with the [Money Gem](https://github.com/RubyMoney/money).
|
4
|
+
|
5
|
+
It doesn't try to handle currency conversions but simply wraps an amount of money into a [BigDecimal](http://www.ruby-doc.org/stdlib-2.0/libdoc/bigdecimal/rdoc/BigDecimal.html) instance to avoid precision issues with floating point arithmetic operations.
|
6
|
+
|
7
|
+
Features:
|
8
|
+
|
9
|
+
- doesn't monkey-patch [Ruby core types](https://github.com/RubyMoney/money/blob/master/lib/money/core_extensions.rb)
|
10
|
+
- less than 100 lines of code
|
11
|
+
- currency formatting support, e.g. `CrazyMoney.new("1234.56").with_currency("NZD") #=> "$1,234.56"`
|
12
|
+
- easy to integrate with Rails (see [Rails](#rails))
|
13
|
+
|
14
|
+
## Usage
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
amount = CrazyMoney.new("13.37")
|
18
|
+
amount > 1 #=> true
|
19
|
+
amount == 13 #=> false
|
20
|
+
amount == 13.37 #=> true
|
21
|
+
amount == CrazyMoney.new(13.37) #=> true
|
22
|
+
amount.cents.to_i #=> 1337
|
23
|
+
amount.with_currency("NZD") #=> "$13.37"
|
24
|
+
```
|
25
|
+
|
26
|
+
### Rails
|
27
|
+
|
28
|
+
At the moment, there is no built-in support for Rails but you can use this initializer as a drop-in
|
29
|
+
replacement for the `monetize` helper that is provided by the Money Gem.
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
# config/initializers/crazy_money.rb
|
33
|
+
|
34
|
+
class ActiveRecord::Base
|
35
|
+
class << self
|
36
|
+
def monetize attribute_cents
|
37
|
+
attribute = attribute_cents.to_s.sub(/_cents$/, "")
|
38
|
+
|
39
|
+
define_method attribute do
|
40
|
+
CrazyMoney.new(send(attribute_cents)) / 100
|
41
|
+
end
|
42
|
+
|
43
|
+
define_method "#{attribute}=" do |value|
|
44
|
+
send("#{attribute_cents}=", CrazyMoney.new(value).cents)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
data/Rakefile
ADDED
data/bench/div.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require_relative "../lib/crazy_money"
|
2
|
+
|
3
|
+
require "benchmark"
|
4
|
+
n = 1E5.to_i.freeze
|
5
|
+
Benchmark.bm do |x|
|
6
|
+
x.report { n.times { CrazyMoney.new(1337) / 100 } } # fastest
|
7
|
+
x.report { n.times { CrazyMoney.new(1337) / 100.0 } }
|
8
|
+
x.report { n.times { CrazyMoney.new(1337) / "100.0" } }
|
9
|
+
end
|
10
|
+
|
11
|
+
require "stackprof"
|
12
|
+
StackProf.run(mode: :cpu, out: "stackprof.dump") do
|
13
|
+
n.times { CrazyMoney.new(1337) / 100 }
|
14
|
+
end
|
15
|
+
|
data/crazy_money.gemspec
ADDED
@@ -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
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "crazy_money"
|
7
|
+
spec.version = "1.1.0"
|
8
|
+
spec.authors = ["Cédric Félizard"]
|
9
|
+
spec.email = ["cedric@felizard.fr"]
|
10
|
+
spec.description = %q{Simple money wrapper}
|
11
|
+
spec.summary = %q{Crazy Money}
|
12
|
+
spec.homepage = ""
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_runtime_dependency "currency_data", "~> 1.0.1"
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
end
|
data/lib/crazy_money.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
require "bigdecimal"
|
2
|
+
require "currency_data"
|
3
|
+
|
4
|
+
class CrazyMoney
|
5
|
+
include Comparable
|
6
|
+
|
7
|
+
def self.zero
|
8
|
+
new 0
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize amount
|
12
|
+
@amount = BigDecimal.new(amount.to_s)
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_s
|
16
|
+
sprintf("%.2f", @amount)
|
17
|
+
end
|
18
|
+
|
19
|
+
def == other
|
20
|
+
@amount == BigDecimal.new(other.to_s)
|
21
|
+
end
|
22
|
+
alias_method :eql?, :==
|
23
|
+
|
24
|
+
def <=> other
|
25
|
+
@amount <=> BigDecimal.new(other.to_s)
|
26
|
+
end
|
27
|
+
|
28
|
+
def positive?
|
29
|
+
@amount > 0
|
30
|
+
end
|
31
|
+
|
32
|
+
def negative?
|
33
|
+
@amount < 0
|
34
|
+
end
|
35
|
+
|
36
|
+
def zero?
|
37
|
+
@amount.zero?
|
38
|
+
end
|
39
|
+
|
40
|
+
def opposite
|
41
|
+
self.class.new(self * -1)
|
42
|
+
end
|
43
|
+
|
44
|
+
def cents(ratio = 100)
|
45
|
+
@amount * BigDecimal.new(ratio.to_s)
|
46
|
+
end
|
47
|
+
|
48
|
+
def + other; self.class.new(@amount + BigDecimal.new(other.to_s)); end
|
49
|
+
def - other; self.class.new(@amount - BigDecimal.new(other.to_s)); end
|
50
|
+
def / other; self.class.new(@amount / BigDecimal.new(other.to_s)); end
|
51
|
+
def * other; self.class.new(@amount * BigDecimal.new(other.to_s)); end
|
52
|
+
|
53
|
+
# FIXME: needs polishing
|
54
|
+
def with_currency iso_code
|
55
|
+
currency = currency(iso_code) || raise(ArgumentError, "Unknown currency: #{iso_code.inspect}")
|
56
|
+
|
57
|
+
left, right = to_s.split(".")
|
58
|
+
sign = left.slice!("-")
|
59
|
+
|
60
|
+
left = left.reverse.scan(/.{1,3}/).map(&:reverse).reverse. # split every 3 digits right-to-left
|
61
|
+
join(currency.thousands_separator)
|
62
|
+
|
63
|
+
formatted = [sign, left, currency.decimal_mark, right].join
|
64
|
+
|
65
|
+
formatted.public_send(
|
66
|
+
currency.symbol_first ? :prepend : :concat,
|
67
|
+
currency.symbol
|
68
|
+
)
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
def currency iso_code
|
74
|
+
::CurrencyData.find(iso_code)
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,118 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require "crazy_money"
|
4
|
+
|
5
|
+
describe CrazyMoney do
|
6
|
+
let(:one_third) { BigDecimal.new(1) / BigDecimal.new(3) }
|
7
|
+
|
8
|
+
describe ".zero" do
|
9
|
+
specify { expect(CrazyMoney.zero).to eq(CrazyMoney.new(0)) }
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "#to_s" do
|
13
|
+
specify { expect(CrazyMoney.new(0).to_s).to eq("0.00") }
|
14
|
+
specify { expect(CrazyMoney.new("13.37").to_s).to eq("13.37") }
|
15
|
+
specify { expect(CrazyMoney.new("13.372").to_s).to eq("13.37") }
|
16
|
+
specify { expect(CrazyMoney.new("13.377").to_s).to eq("13.38") }
|
17
|
+
specify { expect(CrazyMoney.new(one_third).to_s).to eq("0.33") }
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "#==" do
|
21
|
+
specify { expect(CrazyMoney.new(0) == 0).to be_true }
|
22
|
+
specify { expect(CrazyMoney.new(one_third) == one_third).to be_true }
|
23
|
+
specify { expect(CrazyMoney.new("1.23456789") == 1.23456789).to be_true }
|
24
|
+
specify { expect(CrazyMoney.new(1.23456789) == 1.23456789).to be_true }
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "arithmetic operations" do
|
28
|
+
shared_examples_for "it returns a CrazyMoney object" do
|
29
|
+
specify { expect(@result).to be_a CrazyMoney }
|
30
|
+
end
|
31
|
+
|
32
|
+
before do
|
33
|
+
@a = CrazyMoney.new(2)
|
34
|
+
@b = CrazyMoney.new(3)
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "#+" do
|
38
|
+
before { @result = @a + @b }
|
39
|
+
it_behaves_like "it returns a CrazyMoney object"
|
40
|
+
specify { expect(@result).to eq(5) }
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "#-" do
|
44
|
+
before { @result = @a - @b }
|
45
|
+
it_behaves_like "it returns a CrazyMoney object"
|
46
|
+
specify { expect(@result).to eq(-1) }
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "#*" do
|
50
|
+
before { @result = @a * @b }
|
51
|
+
it_behaves_like "it returns a CrazyMoney object"
|
52
|
+
specify { expect(@result).to eq(6) }
|
53
|
+
end
|
54
|
+
|
55
|
+
describe "#/" do
|
56
|
+
before { @result = @a / @b }
|
57
|
+
it_behaves_like "it returns a CrazyMoney object"
|
58
|
+
specify { expect(@result).to eq(BigDecimal.new(2) / BigDecimal.new(3)) }
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe "#positive?" do
|
63
|
+
specify { expect(CrazyMoney.new(-1)).to_not be_positive }
|
64
|
+
specify { expect(CrazyMoney.new( 0)).to_not be_positive }
|
65
|
+
specify { expect(CrazyMoney.new( 1)).to be_positive }
|
66
|
+
end
|
67
|
+
|
68
|
+
describe "#negative?" do
|
69
|
+
specify { expect(CrazyMoney.new(-1)).to be_negative }
|
70
|
+
specify { expect(CrazyMoney.new( 0)).to_not be_negative }
|
71
|
+
specify { expect(CrazyMoney.new( 1)).to_not be_negative }
|
72
|
+
end
|
73
|
+
|
74
|
+
describe "#zero?" do
|
75
|
+
specify { expect(CrazyMoney.new(0)).to be_zero }
|
76
|
+
specify { expect(CrazyMoney.new(0.1)).to_not be_zero }
|
77
|
+
end
|
78
|
+
|
79
|
+
describe "#opposite" do
|
80
|
+
specify { expect(CrazyMoney.new(0).opposite).to eq(0) }
|
81
|
+
specify { expect(CrazyMoney.new(1.23).opposite).to eq(-1.23) }
|
82
|
+
specify { expect(CrazyMoney.new(-20).opposite).to eq(20) }
|
83
|
+
specify { expect(CrazyMoney.new(0).opposite).to be_a CrazyMoney }
|
84
|
+
end
|
85
|
+
|
86
|
+
describe "#cents" do
|
87
|
+
specify { expect(CrazyMoney.new(1.23).cents).to eq(123) }
|
88
|
+
end
|
89
|
+
|
90
|
+
describe "comparison" do
|
91
|
+
specify { expect(CrazyMoney.new(10) > 0).to be_true }
|
92
|
+
specify { expect(CrazyMoney.new(10) > CrazyMoney.new(0)).to be_true }
|
93
|
+
|
94
|
+
specify { expect([0, CrazyMoney.new(10)].max).to eq(10) }
|
95
|
+
specify { expect([CrazyMoney.new(0), CrazyMoney.new(10)].max).to eq(10) }
|
96
|
+
end
|
97
|
+
|
98
|
+
describe "#with_currency" do
|
99
|
+
specify { expect(CrazyMoney.new(one_third).with_currency("NZD")).to eq("$0.33") }
|
100
|
+
|
101
|
+
specify { expect(CrazyMoney.new("13.37").with_currency("NZD")).to eq("$13.37") }
|
102
|
+
specify { expect(CrazyMoney.new("-13.37").with_currency("NZD")).to eq("$-13.37") }
|
103
|
+
specify { expect(CrazyMoney.new("0").with_currency("NZD")).to eq("$0.00") }
|
104
|
+
|
105
|
+
specify { expect(CrazyMoney.new("123").with_currency("NZD")).to eq("$123.00") }
|
106
|
+
specify { expect(CrazyMoney.new("-123").with_currency("NZD")).to eq("$-123.00") }
|
107
|
+
|
108
|
+
specify { expect(CrazyMoney.new("13.37").with_currency("EUR")).to eq("€13,37") }
|
109
|
+
specify { expect(CrazyMoney.new("-13.37").with_currency("EUR")).to eq("€-13,37") }
|
110
|
+
specify { expect(CrazyMoney.new("0").with_currency("EUR")).to eq("€0,00") }
|
111
|
+
|
112
|
+
specify { expect(CrazyMoney.new("-1234567.89").with_currency("USD")).to eq("$-1,234,567.89") }
|
113
|
+
specify { expect(CrazyMoney.new("-1234567.89").with_currency("EUR")).to eq("€-1.234.567,89") }
|
114
|
+
|
115
|
+
specify { expect(CrazyMoney.new("13.37").with_currency("SEK")).to eq("13,37kr") }
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: crazy_money
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Cédric Félizard
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-02-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: currency_data
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.0.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.0.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
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: rspec
|
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: Simple money wrapper
|
70
|
+
email:
|
71
|
+
- cedric@felizard.fr
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- bench/div.rb
|
83
|
+
- crazy_money.gemspec
|
84
|
+
- lib/crazy_money.rb
|
85
|
+
- spec/crazy_money_spec.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.2.0
|
107
|
+
signing_key:
|
108
|
+
specification_version: 4
|
109
|
+
summary: Crazy Money
|
110
|
+
test_files:
|
111
|
+
- spec/crazy_money_spec.rb
|