eeng-money 0.0.1
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/.DS_Store +0 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +8 -0
- data/acts_as_money.gemspec +21 -0
- data/lib/acts_as_money/money.rb +39 -0
- data/lib/acts_as_money/version.rb +3 -0
- data/lib/acts_as_money.rb +23 -0
- data/test/.DS_Store +0 -0
- data/test/acts_as_money/money_test.rb +82 -0
- data/test/test_helper.rb +23 -0
- metadata +99 -0
data/.DS_Store
ADDED
Binary file
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Emmanuel Nicolau
|
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,29 @@
|
|
1
|
+
# Money
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'money'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install money
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'acts_as_money/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "eeng-money"
|
8
|
+
gem.version = ActsAsMoney::VERSION
|
9
|
+
gem.authors = ["Emmanuel Nicolau"]
|
10
|
+
gem.email = ["emmanicolau@gmail.com"]
|
11
|
+
gem.description = %q{Single currency money backed by BigDecimal}
|
12
|
+
gem.summary = %q{Single currency money backed by BigDecimal}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
gem.add_dependency "activerecord", '>= 3.0.0'
|
20
|
+
gem.add_development_dependency "sqlite3"
|
21
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
|
3
|
+
module ActsAsMoney
|
4
|
+
class Money < DelegateClass(BigDecimal)
|
5
|
+
attr_reader :amount, :decimals
|
6
|
+
|
7
|
+
def initialize amount, decimals = 2
|
8
|
+
@decimals = decimals
|
9
|
+
amount = 0 unless amount
|
10
|
+
@amount = BigDecimal.new(amount.to_s).round(@decimals)
|
11
|
+
super @amount
|
12
|
+
end
|
13
|
+
|
14
|
+
%w(+ - * /).each do |op|
|
15
|
+
class_eval %Q{
|
16
|
+
def #{op} other
|
17
|
+
other = BigDecimal.new(other.to_s) if !other.is_a?(BigDecimal) && !other.is_a?(Money)
|
18
|
+
Money.new super, decimals
|
19
|
+
end
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
def hash
|
24
|
+
@amount.hash
|
25
|
+
end
|
26
|
+
|
27
|
+
def eql? other
|
28
|
+
@amount == other.amount
|
29
|
+
end
|
30
|
+
|
31
|
+
def to_s
|
32
|
+
"$%.#{@decimals}f" % @amount
|
33
|
+
end
|
34
|
+
|
35
|
+
def inspect
|
36
|
+
to_s
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require "acts_as_money/version"
|
2
|
+
require "acts_as_money/money"
|
3
|
+
|
4
|
+
module ActsAsMoney
|
5
|
+
def self.included(base)
|
6
|
+
base.extend(ClassMethods)
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def money(*attr_names)
|
11
|
+
attr_names.each do |attr_name|
|
12
|
+
generator = lambda { |x|
|
13
|
+
decimals = columns.detect { |c| c.name == attr_name.to_s }.scale
|
14
|
+
Money.new(x, decimals)
|
15
|
+
}
|
16
|
+
composed_of attr_name, class_name: "ActsAsMoney::Money", mapping: [attr_name, :amount],
|
17
|
+
allow_nil: true, converter: generator, constructor: generator
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
ActiveRecord::Base.class_eval { include ActsAsMoney }
|
data/test/.DS_Store
ADDED
Binary file
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../test_helper"
|
2
|
+
|
3
|
+
module ActsAsMoney
|
4
|
+
class Product < ActiveRecord::Base
|
5
|
+
money :price, :tax, :discount
|
6
|
+
end
|
7
|
+
|
8
|
+
class AssignmentMoneyTest < Test::Unit::TestCase
|
9
|
+
def test_rounding_new
|
10
|
+
assert_equal 19.64, Product.new(:price => (78.55 * 0.25).to_d).price
|
11
|
+
assert_equal 0.67, Product.new(:tax => 2.0/3.0).tax
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_rounding_persisted
|
15
|
+
assert_equal 0.67, Product.create(:tax => 2.0/3.0).reload.tax
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_nils
|
19
|
+
assert_nil Product.new(:price => nil).price
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_other_types
|
23
|
+
assert_equal 10, Product.new(:price => 10).price
|
24
|
+
assert_equal 10.24, Product.new(:price => "10.235").price
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_should_use_bigdecimal
|
28
|
+
assert_equal BigDecimal, Product.new(:price => 1).price.amount.class
|
29
|
+
assert_equal Money, Product.new(:price => 1).price.class
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_should_use_scale_from_column_for_decimales_pieces
|
33
|
+
assert_equal 3, Product.new(:discount => 2).discount.decimals
|
34
|
+
assert_equal 3, Product.create(:discount => 2).reload.discount.decimals
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class OperationsTest < Test::Unit::TestCase
|
39
|
+
def test_rounding_multiplication
|
40
|
+
assert_equal 19.64, Money.new(78.55) * 0.25
|
41
|
+
assert_equal 4.28, Money.new(28.5) * 0.15
|
42
|
+
assert_equal 12.0897, Money.new(10.9906, 4) * 1.1
|
43
|
+
assert_equal 12.092, Product.new(:discount => 10.993).discount * 1.1
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_rounding_division
|
47
|
+
assert_equal 0.67, Product.new(:price => 2).price / 3.0
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_should_return_money
|
51
|
+
assert_equal Money, (Product.new(:price => 1).price * 2).class
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_sum_should_return_money
|
55
|
+
res = Money.new(3, 4) + Money.new(2, 4)
|
56
|
+
assert_equal Money, res.class
|
57
|
+
assert_equal 4, res.decimals
|
58
|
+
assert_equal 5, res
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
class ComparissonTest < Test::Unit::TestCase
|
63
|
+
def test_should_be_equal
|
64
|
+
assert Money.new(1) == Money.new(1)
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_should_be_uniq
|
68
|
+
assert_equal 1, [Money.new(1), Money.new(1)].uniq.size
|
69
|
+
assert_equal 2, [Money.new(1), Money.new(1.1)].uniq.size
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
class ToStringTest < Test::Unit::TestCase
|
74
|
+
def test_when_nil
|
75
|
+
assert_equal "$0.00", Money.new(nil).to_s
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_when_not_null
|
79
|
+
assert_equal "$3.25", Money.new(3.25).to_s
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'active_record'
|
3
|
+
require "#{File.dirname(__FILE__)}/../lib/acts_as_money"
|
4
|
+
|
5
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
6
|
+
|
7
|
+
class Test::Unit::TestCase #:nodoc:
|
8
|
+
def setup
|
9
|
+
ActiveRecord::Schema.define(:version => 1) do
|
10
|
+
create_table :products do |t|
|
11
|
+
t.column :price, :decimal, :precision => 12, :scale => 2
|
12
|
+
t.column :tax, :decimal, :precision => 12, :scale => 2
|
13
|
+
t.column :discount, :decimal, :precision => 12, :scale => 3
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def teardown
|
19
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
20
|
+
ActiveRecord::Base.connection.drop_table(table)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: eeng-money
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Emmanuel Nicolau
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-14 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.0.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: sqlite3
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Single currency money backed by BigDecimal
|
47
|
+
email:
|
48
|
+
- emmanicolau@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .DS_Store
|
54
|
+
- .gitignore
|
55
|
+
- Gemfile
|
56
|
+
- LICENSE.txt
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- acts_as_money.gemspec
|
60
|
+
- lib/acts_as_money.rb
|
61
|
+
- lib/acts_as_money/money.rb
|
62
|
+
- lib/acts_as_money/version.rb
|
63
|
+
- test/.DS_Store
|
64
|
+
- test/acts_as_money/money_test.rb
|
65
|
+
- test/test_helper.rb
|
66
|
+
homepage: ''
|
67
|
+
licenses: []
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
hash: -1914526830248602656
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
segments:
|
88
|
+
- 0
|
89
|
+
hash: -1914526830248602656
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 1.8.23
|
93
|
+
signing_key:
|
94
|
+
specification_version: 3
|
95
|
+
summary: Single currency money backed by BigDecimal
|
96
|
+
test_files:
|
97
|
+
- test/.DS_Store
|
98
|
+
- test/acts_as_money/money_test.rb
|
99
|
+
- test/test_helper.rb
|