easy_money 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +108 -0
- data/Rakefile +52 -0
- data/VERSION +1 -0
- data/easy_money.gemspec +51 -0
- data/lib/easy_money.rb +124 -0
- data/test/helper.rb +16 -0
- data/test/test_easy_money.rb +87 -0
- metadata +73 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Allen Fair
|
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,108 @@
|
|
1
|
+
= Easy Money!
|
2
|
+
|
3
|
+
A Ruby mixin library to add simple money display and editing using data stored as an integer in
|
4
|
+
cents or other precision or currency.
|
5
|
+
Easy Money is a simpler alternative to the "money" gem, and easier to use, hence the name.
|
6
|
+
|
7
|
+
Float types are not good for storing and computing financial data because of rounding and conversion
|
8
|
+
between floats and decimal precision and slower performance compared to integer arithmetic. Integers
|
9
|
+
are preferred in this case, with a dollar amount ($1.00) stored as cents (100) internally.
|
10
|
+
|
11
|
+
|
12
|
+
Easy Money will also handle any float data stored as integer in the database for this reason, such
|
13
|
+
as FM Radio Frequencies (88.1 FM) or an NTRP Tennis Rating (4.5).
|
14
|
+
|
15
|
+
== Quick Start
|
16
|
+
Easy Money works in any Ruby class, it does not require Active Record or other ORM.
|
17
|
+
Its default setting allow it to work on US Dollar amounts stored in cents, but is easily configured.
|
18
|
+
|
19
|
+
First, install this from gemcutter
|
20
|
+
gem install easy_money
|
21
|
+
|
22
|
+
Next, you need to configure your app to use it, by either:
|
23
|
+
require 'rubygems' # Rails application, no framework
|
24
|
+
require 'easy_money' #
|
25
|
+
|
26
|
+
config.gem 'easy_money' # Rails 2.x, in your ./config/environment.rb
|
27
|
+
|
28
|
+
gem 'easy_money' # Rails 3.x, in your ./Gemfile
|
29
|
+
|
30
|
+
Mixin the EasyMoney module into the (model) class you need, and declare the
|
31
|
+
attributes (columns) you wish you have the easy_money helpers
|
32
|
+
|
33
|
+
class Ledger
|
34
|
+
include EasyMoney
|
35
|
+
attr_accessor :amount, :euro # Integer value of cents
|
36
|
+
easy_money :amount # Creates amount_money() and amount_money=() methods
|
37
|
+
easy_money :amount, :units=>"dollars", :unit=>'$', :negative=>'%.2f CR'
|
38
|
+
# Creates amount_dollars() and amount_dollars=() methods
|
39
|
+
end
|
40
|
+
|
41
|
+
ledger = Ledger.new
|
42
|
+
ledger.amount = 100 # 100 cents = $1.00
|
43
|
+
ledger.amount_money #=> "1.00"
|
44
|
+
ledger.amount_money = "-123.45"
|
45
|
+
ledger.amount #=> -12345 (cents)
|
46
|
+
ledger.amount_money(:negative=>'%.2f CR', :zero=>'Free') # Uses these formats
|
47
|
+
ledger.amount_dollars #=> "$123.45 CR"
|
48
|
+
|
49
|
+
# Track the bets of the Gamesters of Triskelion on their drill thrall competitions.
|
50
|
+
class ProviderWagers < ActiveRecord::Base
|
51
|
+
include EasyMoney
|
52
|
+
easy_money :quatloos, :units=>'quatloos', :precision=>3,
|
53
|
+
:zero=>'even', :nil=>'no bet', :negative=>'%.3f Loss', :unit=>'Q',
|
54
|
+
:negative_regex=>/^(-?)(.+\d)\s*Loss/i
|
55
|
+
# creates amount_quatloos(), amount_quatloos=()
|
56
|
+
end
|
57
|
+
|
58
|
+
# in your views
|
59
|
+
<%= f.text_field :amount_quatloos %> # -12000 => "Q12.000 Loss"
|
60
|
+
|
61
|
+
Options for Easy Money calls:
|
62
|
+
* :money_method - Use this as the alternative name to the money-access methods
|
63
|
+
* :units - Use this as an alternative suffix name to the money methods ('dollars' gives 'xx_dollars')
|
64
|
+
* :precision - The number of digits implied after the decimal, default is 2
|
65
|
+
* :separator - The character to use after the integer part, default is '.'
|
66
|
+
* :delimiter - The character to use between every 3 digits of the integer part, default none
|
67
|
+
* :positive - The sprintf format to use for positive numbers, default is based on precision
|
68
|
+
* :negative - The sprintf format to use for negative numbers, default is same as :positive
|
69
|
+
* :zero - The sprintf format to use for zero, default is same as :positive
|
70
|
+
* :nil - The sprintf format to use for nil values, default none
|
71
|
+
* :unit - Prepend this to the front of the money value, say '$', default none
|
72
|
+
* :blank - Return this value when the money string is empty or has no digits on assignment
|
73
|
+
* :negative_regex - A Regular Expression used to determine if a number is negative (and without a - sign), defaults to having a "CR" after the number
|
74
|
+
|
75
|
+
== Easy Money Formatters
|
76
|
+
You can also call or build your own custom conversions. Ensure that
|
77
|
+
you can convert between the integer and money forms if you need to.
|
78
|
+
|
79
|
+
The "money" type is a string, sutitable for human editing, and will convert back into
|
80
|
+
integer type. If you override the formatting options, test that your money result
|
81
|
+
string will convert properly back to the original integer value.
|
82
|
+
|
83
|
+
include EasyMoney
|
84
|
+
...
|
85
|
+
puts EasyMoney.money_to_integer( money_string, :option=>value, ... )
|
86
|
+
puts EasyMoney.integer_to_money( cents_integer, :option=>value, ... )
|
87
|
+
puts EasyMoney.integer_to_float( cents_integer, :option=>value, ... )
|
88
|
+
puts EasyMoney.float_to_integer( money_float, :option=>value, ... )
|
89
|
+
|
90
|
+
EasyMoney.integer_to_float( nil, blank:0.0 ) #=> 0.0 [Ruby 1.9.1 Syntax]
|
91
|
+
EasyMoney.integer_to_float( 12345, :precision=>3 ) #=> 12.345
|
92
|
+
EasyMoney.float_to_integer(12.345111, :precision=>3 ) #=> 12345
|
93
|
+
|
94
|
+
The options to these methods are the same as the #easy_money declarations
|
95
|
+
|
96
|
+
|
97
|
+
== To Do
|
98
|
+
|
99
|
+
* Validate Money string format
|
100
|
+
* Global configuration for currency settings
|
101
|
+
* Explore rounding vs. truncation issues
|
102
|
+
* Use a Locale and I18n on money formatting, without introducing dependencies (perhaps by injection)?
|
103
|
+
* Explore inegration of Rails' NumberHelper methods, but not require ActionView
|
104
|
+
* Custom converters (useful for say, converting bytes to Gigabytes)?
|
105
|
+
|
106
|
+
== Copyright
|
107
|
+
|
108
|
+
Copyright (c) 2010 Allen Fair. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "easy_money"
|
8
|
+
gem.summary = %Q{Easy Money handling for Rails/ActiveRecord}
|
9
|
+
gem.description = %Q{Easy Money handling for Rails/ActiveRecord}
|
10
|
+
gem.email = "allen.fair@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/afair/easy_money"
|
12
|
+
gem.authors = ["Allen Fair"]
|
13
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
14
|
+
end
|
15
|
+
Jeweler::GemcutterTasks.new
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'rake/testtask'
|
21
|
+
Rake::TestTask.new(:test) do |test|
|
22
|
+
test.libs << 'lib' << 'test'
|
23
|
+
test.pattern = 'test/**/test_*.rb'
|
24
|
+
test.verbose = true
|
25
|
+
end
|
26
|
+
|
27
|
+
begin
|
28
|
+
require 'rcov/rcovtask'
|
29
|
+
Rcov::RcovTask.new do |test|
|
30
|
+
test.libs << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
rescue LoadError
|
35
|
+
task :rcov do
|
36
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
task :test => :check_dependencies
|
41
|
+
|
42
|
+
task :default => :test
|
43
|
+
|
44
|
+
require 'rake/rdoctask'
|
45
|
+
Rake::RDocTask.new do |rdoc|
|
46
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
47
|
+
|
48
|
+
rdoc.rdoc_dir = 'rdoc'
|
49
|
+
rdoc.title = "easy_money #{version}"
|
50
|
+
rdoc.rdoc_files.include('README*')
|
51
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
52
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/easy_money.gemspec
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{easy_money}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Allen Fair"]
|
12
|
+
s.date = %q{2010-02-23}
|
13
|
+
s.description = %q{Easy Money handling for Rails/ActiveRecord}
|
14
|
+
s.email = %q{allen.fair@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"easy_money.gemspec",
|
27
|
+
"lib/easy_money.rb",
|
28
|
+
"test/helper.rb",
|
29
|
+
"test/test_easy_money.rb"
|
30
|
+
]
|
31
|
+
s.homepage = %q{http://github.com/afair/easy_money}
|
32
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
33
|
+
s.require_paths = ["lib"]
|
34
|
+
s.rubygems_version = %q{1.3.6}
|
35
|
+
s.summary = %q{Easy Money handling for Rails/ActiveRecord}
|
36
|
+
s.test_files = [
|
37
|
+
"test/helper.rb",
|
38
|
+
"test/test_easy_money.rb"
|
39
|
+
]
|
40
|
+
|
41
|
+
if s.respond_to? :specification_version then
|
42
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
43
|
+
s.specification_version = 3
|
44
|
+
|
45
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
46
|
+
else
|
47
|
+
end
|
48
|
+
else
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
data/lib/easy_money.rb
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
# EasyMoney - Ruby class mixin library to add money helpers to attribute/methods
|
2
|
+
module EasyMoney
|
3
|
+
|
4
|
+
def self.included(base) #:nodoc:
|
5
|
+
base.extend( ClassMethods )
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
|
10
|
+
# Creates an money instance method for the given method, named "#{method}_money" which returns
|
11
|
+
# a formatted money string, and a #{method}_money= method used to set an edited money string.
|
12
|
+
# The original method stores the value as integer (cents, or other precision/currency setting). Options:
|
13
|
+
# * :money_method - Use this as the alternative name to the money-access methods
|
14
|
+
# * :units - Use this as an alternative suffix name to the money methods ('dollars' gives 'xx_dollars')
|
15
|
+
# * :precision - The number of digits implied after the decimal, default is 2
|
16
|
+
# * :separator - The character to use after the integer part, default is '.'
|
17
|
+
# * :delimiter - The character to use between every 3 digits of the integer part, default none
|
18
|
+
# * :positive - The sprintf format to use for positive numbers, default is based on precision
|
19
|
+
# * :negative - The sprintf format to use for negative numbers, default is same as :positive
|
20
|
+
# * :zero - The sprintf format to use for zero, default is same as :positive
|
21
|
+
# * :nil - The sprintf format to use for nil values, default none
|
22
|
+
# * :unit - Prepend this to the front of the money value, say '$', default none
|
23
|
+
# * :blank - Return this value when the money string is empty or has no digits on assignment
|
24
|
+
# * :negative_regex - A Regular Expression used to determine if a number is negative (and without a - sign)
|
25
|
+
#
|
26
|
+
def easy_money(method, *args)
|
27
|
+
opt = args.last.is_a?(Hash) ? args.pop : {}
|
28
|
+
money_method = opt.delete(:money_method) || "#{method}_#{opt.delete(:units)||'money'}"
|
29
|
+
|
30
|
+
class_eval %Q(
|
31
|
+
def #{money_method}(*args)
|
32
|
+
opt = args.last.is_a?(Hash) ? args.pop : {}
|
33
|
+
EasyMoney.integer_to_money( #{method}, #{opt.inspect}.merge(opt))
|
34
|
+
end
|
35
|
+
|
36
|
+
def #{money_method}=(v, *args)
|
37
|
+
opt = args.last.is_a?(Hash) ? args.pop : {}
|
38
|
+
self.#{method} = EasyMoney.money_to_integer( v, #{opt.inspect}.merge(opt))
|
39
|
+
end
|
40
|
+
)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Returns the money string of the given integer value. Uses relevant options from #easy_money
|
45
|
+
def self.integer_to_money(value, *args)
|
46
|
+
opt = args.last.is_a?(Hash) ? args.pop : {}
|
47
|
+
opt[:positive] ||= "%.#{opt[:precision]||2}f"
|
48
|
+
pattern =
|
49
|
+
if value.nil?
|
50
|
+
value = 0
|
51
|
+
opt[:nil] || opt[:positive]
|
52
|
+
else
|
53
|
+
case value <=> 0
|
54
|
+
when 1 then opt[:positive]
|
55
|
+
when 0 then opt[:zero] || opt[:positive]
|
56
|
+
else
|
57
|
+
value = -value if opt[:negative] && opt[:negative] != opt[:positive]
|
58
|
+
opt[:negative] || opt[:positive]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
value = self.format_money( value, pattern, opt)
|
62
|
+
value = opt[:unit]+value if opt[:unit]
|
63
|
+
value.gsub!(/\./,opt[:separator]) if opt[:separator]
|
64
|
+
if opt[:delimiter] && (m = value.match(/^(\D*)(\d+)(.*)/))
|
65
|
+
# Adapted From Rails' ActionView::Helpers::NumberHelper
|
66
|
+
n = m[2].gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{opt[:delimiter]}")
|
67
|
+
value=m[1]+n+m[3]
|
68
|
+
end
|
69
|
+
value
|
70
|
+
end
|
71
|
+
|
72
|
+
def self.integer_to_float(value, *args)
|
73
|
+
opt = args.last.is_a?(Hash) ? args.pop : {}
|
74
|
+
return (opt[:blank]||nil) if value.nil?
|
75
|
+
value = 1.0 * value / (10**(opt[:precision]||2))
|
76
|
+
end
|
77
|
+
|
78
|
+
# Returns the integer of the given money string. Uses relevant options from #easy_money
|
79
|
+
def self.money_to_integer(value, *args)
|
80
|
+
opt = args.last.is_a?(Hash) ? args.pop : {}
|
81
|
+
value.gsub!(opt[:delimiter],'') if opt[:delimiter]
|
82
|
+
value.gsub!(opt[:separator],'.') if opt[:separator]
|
83
|
+
value.gsub!(/^[^\d\.\-\,]+/,'')
|
84
|
+
return (opt[:blank]||nil) unless value =~ /\d/
|
85
|
+
m = value.to_s.match(opt[:negative_regex]||/^(-?)(.+\d)\s*cr/i)
|
86
|
+
value = value.match(/^-/) ? m[2] : "-#{m[2]}" if m && m[2]
|
87
|
+
|
88
|
+
# Money string ("123.45") to proper integer withough passing through the float transformation
|
89
|
+
match = value.match(/(-?\d*)\.?(\d*)/)
|
90
|
+
return 0 unless match
|
91
|
+
value = match[1].to_i * (10 ** (opt[:precision]||2))
|
92
|
+
cents = match[2]
|
93
|
+
cents = cents + '0' while cents.length < (opt[:precision]||2)
|
94
|
+
cents = cents.to_s[0,opt[:precision]||2]
|
95
|
+
value += cents.to_i * (value<0 ? -1 : 1)
|
96
|
+
value
|
97
|
+
end
|
98
|
+
|
99
|
+
# Returns the integer (cents) value from a Float
|
100
|
+
def self.float_to_integer(value, *args)
|
101
|
+
opt = args.last.is_a?(Hash) ? args.pop : {}
|
102
|
+
return (opt[:blank]||nil) if value.nil?
|
103
|
+
value = (value.to_f*(10**((opt[:precision]||2)+1))).to_i/10 # helps rounding 4.56 -> 455 ouch!
|
104
|
+
end
|
105
|
+
|
106
|
+
# Replacing the sprintf function to deal with money as float. "... %[flags]m ..."
|
107
|
+
def self.format_money(value, pattern="%.2m", *args)
|
108
|
+
opt = args.last.is_a?(Hash) ? args.pop : {}
|
109
|
+
sign = value < 0 ? -1 : 1
|
110
|
+
dollars, cents = value.abs.divmod( 10 ** (opt[:precision]||2))
|
111
|
+
dollars *= sign
|
112
|
+
parts = pattern.match(/^(.*)%([-\. \d+0]*)[fm](.*)/)
|
113
|
+
return pattern unless parts
|
114
|
+
intdec = parts[2].match(/(.*)\.(\d*)/)
|
115
|
+
dprec, cprec = intdec ? [intdec[1], intdec[2]] : ['', '']
|
116
|
+
dollars = sprintf("%#{dprec}d", dollars)
|
117
|
+
cents = '0' + cents.to_s while cents.to_s.length < (opt[:precision]||2)
|
118
|
+
cents = cents.to_s[0,cprec.to_i]
|
119
|
+
cents = cents + '0' while cents.length < cprec.to_i
|
120
|
+
value = parts[1] + "#{(dollars.to_i==0 && sign==-1) ? '-' : '' }#{dollars}#{cents>' '? '.':''}#{cents}" + parts[3]
|
121
|
+
value
|
122
|
+
end
|
123
|
+
|
124
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
5
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
6
|
+
require 'easy_money'
|
7
|
+
|
8
|
+
class Test::Unit::TestCase
|
9
|
+
end
|
10
|
+
|
11
|
+
class Sample
|
12
|
+
include EasyMoney
|
13
|
+
attr_accessor :price, :balance
|
14
|
+
easy_money :price
|
15
|
+
easy_money :balance
|
16
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestEasyMoney < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_include
|
6
|
+
sample = Sample.new
|
7
|
+
flunk "no price_money method" unless sample.respond_to?(:price_money)
|
8
|
+
flunk "no price_money= method" unless sample.respond_to?(:price_money=)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_method_money
|
12
|
+
s = Sample.new
|
13
|
+
[ 10000, 123456, 0, -1 -9876 ].each do |p|
|
14
|
+
s.price = p
|
15
|
+
m = s.price_money
|
16
|
+
s.price_money = m
|
17
|
+
flunk "Assignment error: p=#{p} m=#{m} price=#{s.price}" unless s.price = p
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_method_money=
|
22
|
+
s = Sample.new
|
23
|
+
{ "0.00"=>0, "12.34"=>1234, "-1.2345"=>-123, "12"=>1200, "4.56CR"=>-456 }.each do |m,p|
|
24
|
+
s.price_money = m
|
25
|
+
flunk "Assignment error: p=#{p} m=#{m} price=#{s.price}" unless s.price = p
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_integer_to_money
|
30
|
+
assert EasyMoney.integer_to_money(123) == '1.23'
|
31
|
+
assert EasyMoney.integer_to_money(-12333) == '-123.33'
|
32
|
+
assert EasyMoney.integer_to_money(0) == '0.00'
|
33
|
+
assert EasyMoney.integer_to_money(nil, :nil=>'?') == '?'
|
34
|
+
assert EasyMoney.integer_to_money(-1, :negative=>'%.2f CR') == '0.01 CR'
|
35
|
+
assert EasyMoney.integer_to_money(0, :zero=>'free') == 'free'
|
36
|
+
assert EasyMoney.integer_to_money(100, :unit=>'$') == '$1.00'
|
37
|
+
assert EasyMoney.integer_to_money(100, :separator=>',') == '1,00'
|
38
|
+
assert EasyMoney.integer_to_money(12345678900, :separator=>',', :delimiter=>'.') == '123.456.789,00'
|
39
|
+
assert EasyMoney.integer_to_money(333, :precision=>3) == '0.333'
|
40
|
+
assert EasyMoney.integer_to_money(111, :precision=>1) == '11.1'
|
41
|
+
assert EasyMoney.integer_to_money(111, :precision=>0) == '111'
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_money_to_integer
|
45
|
+
assert EasyMoney.money_to_integer('1.23' ) == 123
|
46
|
+
assert EasyMoney.money_to_integer('0.00' ) == 0
|
47
|
+
assert EasyMoney.money_to_integer('-1.23' ) == -123
|
48
|
+
assert EasyMoney.money_to_integer('1.23 CR' ) == -123
|
49
|
+
assert EasyMoney.money_to_integer('$-2.34 CR' ) == 234
|
50
|
+
assert EasyMoney.money_to_integer(' 1.234' ) == 123
|
51
|
+
assert EasyMoney.money_to_integer('$1' ) == 100
|
52
|
+
assert EasyMoney.money_to_integer('1' ) == 100
|
53
|
+
assert EasyMoney.money_to_integer('' ) == nil
|
54
|
+
assert EasyMoney.money_to_integer('1,00', :separator=>',',:delimiter=>'.') == 100
|
55
|
+
assert EasyMoney.money_to_integer('$123.456.789,00 CR', :separator=>',',:delimiter=>'.') == -12345678900
|
56
|
+
assert EasyMoney.money_to_integer('4.44', :precision=>4 ) == 44400
|
57
|
+
assert EasyMoney.money_to_integer('4.44', :precision=>0 ) == 4
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_float_to_integer
|
61
|
+
assert EasyMoney.float_to_integer(1.00 ) == 100
|
62
|
+
assert EasyMoney.float_to_integer(1.001 ) == 100
|
63
|
+
assert EasyMoney.float_to_integer(-1.23 ) == -123
|
64
|
+
assert EasyMoney.float_to_integer(9.0 ) == 900
|
65
|
+
assert EasyMoney.float_to_integer(nil ) == nil
|
66
|
+
assert EasyMoney.float_to_integer(0.00 ) == 0
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_integer_to_float
|
70
|
+
assert EasyMoney.integer_to_float(1 ) == 0.01
|
71
|
+
assert EasyMoney.integer_to_float(0 ) == 0.0
|
72
|
+
assert EasyMoney.integer_to_float(-100 ) == -1.00
|
73
|
+
assert EasyMoney.integer_to_float(nil ) == nil
|
74
|
+
assert EasyMoney.integer_to_float(9999888, :precision=>3 ) == 9999.888
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_format_money
|
78
|
+
assert EasyMoney.format_money(12345) == '123.45'
|
79
|
+
assert EasyMoney.format_money(12345, "%07.2m") == '0000123.45'
|
80
|
+
assert EasyMoney.format_money(12345, "%07.3m") == '0000123.450'
|
81
|
+
assert EasyMoney.format_money(12345, "%m") == '123'
|
82
|
+
assert EasyMoney.format_money(12345, "free") == 'free'
|
83
|
+
assert EasyMoney.format_money(-12345) == '-123.45'
|
84
|
+
assert EasyMoney.format_money(-12345, "%07.1m") == '-000123.4'
|
85
|
+
assert EasyMoney.format_money(-1) == '-0.01'
|
86
|
+
end
|
87
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: easy_money
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Allen Fair
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-02-23 00:00:00 -05:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Easy Money handling for Rails/ActiveRecord
|
22
|
+
email: allen.fair@gmail.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- LICENSE
|
29
|
+
- README.rdoc
|
30
|
+
files:
|
31
|
+
- .document
|
32
|
+
- .gitignore
|
33
|
+
- LICENSE
|
34
|
+
- README.rdoc
|
35
|
+
- Rakefile
|
36
|
+
- VERSION
|
37
|
+
- easy_money.gemspec
|
38
|
+
- lib/easy_money.rb
|
39
|
+
- test/helper.rb
|
40
|
+
- test/test_easy_money.rb
|
41
|
+
has_rdoc: true
|
42
|
+
homepage: http://github.com/afair/easy_money
|
43
|
+
licenses: []
|
44
|
+
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options:
|
47
|
+
- --charset=UTF-8
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
segments:
|
62
|
+
- 0
|
63
|
+
version: "0"
|
64
|
+
requirements: []
|
65
|
+
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.3.6
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: Easy Money handling for Rails/ActiveRecord
|
71
|
+
test_files:
|
72
|
+
- test/helper.rb
|
73
|
+
- test/test_easy_money.rb
|