moneytize 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +3 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +22 -0
- data/LICENSE.txt +22 -0
- data/README.md +20 -0
- data/lib/moneytize.rb +95 -0
- data/lib/version.rb +3 -0
- data/moneytize.gemspec +31 -0
- data/tests/tc_moneytize.rb +74 -0
- metadata +70 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 02fd4f54325cb10dd242d68fea3c73620a1960dc
|
4
|
+
data.tar.gz: 6b1903d2e77d011fb3427848e746971a8ca7d0d1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6d1e63658bb7338f77df3714ad3f6e3eff04833ffebef406602e5d5eabbe10d931b454d805fee491858733963a18b091be99ae40db6679a1664c45eb4d7e10b1
|
7
|
+
data.tar.gz: ea02d22b1798eb24b18fb9bbcd4da0ab83c7de0d3ce41d3eb3110f7f1b837a34f1553dbd9d5a4c3442d0f896fc868408a842ac4d0a90aee1122bd323cdd1c599
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
moneytize (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
byebug (3.1.2)
|
10
|
+
columnize (~> 0.8)
|
11
|
+
debugger-linecache (~> 1.2)
|
12
|
+
columnize (0.8.9)
|
13
|
+
debugger-linecache (1.2.0)
|
14
|
+
rake (10.1.0)
|
15
|
+
|
16
|
+
PLATFORMS
|
17
|
+
ruby
|
18
|
+
|
19
|
+
DEPENDENCIES
|
20
|
+
byebug (~> 3.1.2)
|
21
|
+
moneytize!
|
22
|
+
rake
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Jaime Andres
|
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,20 @@
|
|
1
|
+
# MONEYTIZE
|
2
|
+
Did you ever have the requirement:
|
3
|
+
> Can you format the value as currency with "," as decimal separator
|
4
|
+
|
5
|
+
or
|
6
|
+
|
7
|
+
> Can you display " ' " as millions separator
|
8
|
+
|
9
|
+
It's not too hard to parse a numeric value to format it the way you want,
|
10
|
+
but why would you like to repeat the same process every time you need to parse
|
11
|
+
a numeric value as currency.
|
12
|
+
|
13
|
+
*_THAT'S WHY MONEYTIZE EXISTS_*
|
14
|
+
|
15
|
+
Just call it with the value to be formatted and the options you expect:
|
16
|
+
```
|
17
|
+
Moneytize::Currency.format(123456.9803, { currency_symbol: '$', decimal: ',' })
|
18
|
+
```
|
19
|
+
|
20
|
+
Check the [documentation](#) to see the options you have.
|
data/lib/moneytize.rb
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'version'
|
2
|
+
|
3
|
+
# Moneytize is a simple formatter for currency, it allows to format any currency the way you like,
|
4
|
+
# for example, you can decide to separate thousands with ',' and decimals with '.'.
|
5
|
+
# Please consider that to avoid unnecessary complexity there are a few valid separators: ',', '.', ' ' and '¢'.
|
6
|
+
# For millions separator you can only choose to use it or not, the separator to be used is "'"
|
7
|
+
module Moneytize
|
8
|
+
|
9
|
+
# This class is in charge to format any numeric value to the expected currency format.
|
10
|
+
class Currency
|
11
|
+
|
12
|
+
# Converts any numeric value to a formatted string based on the conditions (separators) send
|
13
|
+
# to the method,
|
14
|
+
#
|
15
|
+
# The default values are:
|
16
|
+
# decimals: '.'
|
17
|
+
# thousands: ''
|
18
|
+
# millions: false
|
19
|
+
# currency_symbol: ''
|
20
|
+
#
|
21
|
+
# @param value [Numeric] the value to be formatted
|
22
|
+
# @param options [Hash] collection of values to be used as separators:
|
23
|
+
# { currency_symbol: '|$|USD|etc|', decimal: "|,|.| |¢|",
|
24
|
+
# thoushands: "|,|.| |", millions: true|false }
|
25
|
+
#
|
26
|
+
# @return [String] the numeric value formatted to string
|
27
|
+
def self.format(value, options = {})
|
28
|
+
currency = Currency.new
|
29
|
+
|
30
|
+
currency.prepare_separators(options)
|
31
|
+
currency.format_currency_for(value)
|
32
|
+
end
|
33
|
+
|
34
|
+
# Prepare decimals, thousands and millions separators based on parameters passed and defaults
|
35
|
+
def prepare_separators(options)
|
36
|
+
fetch_separators(options)
|
37
|
+
validate_separators
|
38
|
+
end
|
39
|
+
|
40
|
+
# Formats numeric value to string based on separators previously setted
|
41
|
+
def format_currency_for(value)
|
42
|
+
currency = format('%0.2f', value).split('.')
|
43
|
+
currency[0] = thousands(millions(currency[0])).join(@millions).reverse!
|
44
|
+
|
45
|
+
@currency_symbol.concat(currency.join(@decimal))
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def fetch_separators(options)
|
51
|
+
@currency_symbol = options.fetch(:currency_symbol, '')
|
52
|
+
@decimal = options.fetch(:decimal, '.')
|
53
|
+
@thousands = options.fetch(:thousands, '')
|
54
|
+
@millions = "'" if options[:millions]
|
55
|
+
end
|
56
|
+
|
57
|
+
def validate_separators
|
58
|
+
allowed_symbols = ['.', ',', ' ']
|
59
|
+
|
60
|
+
@decimal = '.' unless allowed_symbols.push('¢').include?(@decimal)
|
61
|
+
@thousands = '' unless allowed_symbols.include?(@thousands)
|
62
|
+
|
63
|
+
if @thousands == @decimal
|
64
|
+
@decimal = '.' if @thousands == ',' or @thousands == ' '
|
65
|
+
@decimal = ',' if @thousands == '.'
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def millions(amount)
|
70
|
+
slice_amount(amount.reverse!, 6)
|
71
|
+
end
|
72
|
+
|
73
|
+
def thousands(amounts)
|
74
|
+
amounts = [amounts] if amounts.is_a?(String)
|
75
|
+
|
76
|
+
amounts.map! { |amount| slice_thousands(amount) }
|
77
|
+
end
|
78
|
+
|
79
|
+
def slice_thousands(amount)
|
80
|
+
thousand = slice_amount(amount, 3)
|
81
|
+
thousand.is_a?(String) ? thousand : thousand.join(@thousands)
|
82
|
+
end
|
83
|
+
|
84
|
+
def slice_amount(amount, unit_size)
|
85
|
+
return amount unless amount.size > unit_size
|
86
|
+
|
87
|
+
range = unit_size - 1
|
88
|
+
min_size = unit_size + 1
|
89
|
+
|
90
|
+
final_amount = []
|
91
|
+
final_amount << amount.slice!(0..range) until amount.size < min_size
|
92
|
+
final_amount << amount
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
data/lib/version.rb
ADDED
data/moneytize.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |gem|
|
8
|
+
gem.name = 'moneytize'
|
9
|
+
gem.version = Moneytize::VERSION
|
10
|
+
gem.homepage = "http://moneytize.me"
|
11
|
+
gem.licenses = ["MIT"]
|
12
|
+
gem.authors = ['DiabloUrbano']
|
13
|
+
gem.email = ['andresdavila6@gmail.com']
|
14
|
+
gem.description = 'formats any numeric value to the expected currency format, choose dot, comma, space or even ¢ for cents. Just check it!'
|
15
|
+
gem.summary = 'format your currency the way you expect.'
|
16
|
+
|
17
|
+
gem.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
18
|
+
gem.test_files = gem.files.grep(/^(tests|spec|features)/)
|
19
|
+
gem.require_paths = ['lib']
|
20
|
+
|
21
|
+
gem.has_rdoc = 'yard'
|
22
|
+
|
23
|
+
# ruby version
|
24
|
+
gem.required_ruby_version = '>= 1.9.3'
|
25
|
+
|
26
|
+
if RUBY_VERSION == '1.9.3'
|
27
|
+
gem.add_development_dependency 'debugger', '~> 1.6.6'
|
28
|
+
elsif RUBY_VERSION >= '2.0.0'
|
29
|
+
gem.add_development_dependency 'byebug', '~> 3.1.2'
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require_relative '../lib/moneytize'
|
3
|
+
|
4
|
+
class TestMoneytize < Test::Unit::TestCase
|
5
|
+
def test_format_without_decimals
|
6
|
+
currency = Moneytize::Currency.format(1025)
|
7
|
+
assert_equal('1025.00', currency)
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_format_default
|
11
|
+
currency = Moneytize::Currency.format(1025.54)
|
12
|
+
assert_equal('1025.54', currency)
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_format_with_different_symbol
|
16
|
+
currency = Moneytize::Currency.format(1025.54, currency_symbol: 'USD')
|
17
|
+
assert_equal('USD1025.54', currency)
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_format_with_different_decimal_separator
|
21
|
+
currency = Moneytize::Currency.format(1025.54, decimal: ',')
|
22
|
+
assert_equal('1025,54', currency)
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_format_with_different_decimal_separator
|
26
|
+
currency = Moneytize::Currency.format(1025.54, decimal: '¢', currency_symbol: '$')
|
27
|
+
assert_equal('$1025¢54', currency)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_format_with_different_thousand_separator
|
31
|
+
currency = Moneytize::Currency.format(1025.54, thousands: '.', currency_symbol: '$')
|
32
|
+
assert_equal('$1.025,54', currency)
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_format_with_different_thousand_separator_comma
|
36
|
+
currency = Moneytize::Currency.format(1025.54, thousands: ',', decimal: ',', currency_symbol: '$')
|
37
|
+
assert_equal('$1,025.54', currency)
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_format_with_different_thousand_separator_and_decimal_separator
|
41
|
+
currency = Moneytize::Currency.format(1025.54, decimal: ',', thousands: '.', currency_symbol: '$')
|
42
|
+
assert_equal('$1.025,54', currency)
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_format_with_millions
|
46
|
+
currency = Moneytize::Currency.format(14578025.54823424, currency_symbol: '$')
|
47
|
+
assert_equal('$14578025.55', currency)
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_format_with_millions_separator
|
51
|
+
currency = Moneytize::Currency.format(14578025.54823424, millions: true, currency_symbol: '$')
|
52
|
+
assert_equal("$14'578025.55", currency)
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_format_with_millions_and_thousands_separator
|
56
|
+
currency = Moneytize::Currency.format(14578025.54823424, millions: true, thousands: '.', currency_symbol: '$')
|
57
|
+
assert_equal("$14'578.025,55", currency)
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_billions_with_format_with_millions_and_thousands_separator
|
61
|
+
currency = Moneytize::Currency.format(14897456578025.54823424, millions: true, thousands: '.', currency_symbol: '$')
|
62
|
+
assert_equal("$14'897.456'578.025,55", currency)
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_format_with_hundreds
|
66
|
+
currency = Moneytize::Currency.format(125.55, millions: true, thousands: '.', currency_symbol: '$')
|
67
|
+
assert_equal('$125,55', currency)
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_it_should_allow_only_specific_separators_or_use_default_values
|
71
|
+
currency = Moneytize::Currency.format(12589080.78, millions: false, thousands: '*', decimal: '%', currency_symbol: '$')
|
72
|
+
assert_equal('$12589080.78', currency)
|
73
|
+
end
|
74
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: moneytize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- DiabloUrbano
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-07-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: byebug
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.1.2
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.1.2
|
27
|
+
description: formats any numeric value to the expected currency format, choose dot,
|
28
|
+
comma, space or even ¢ for cents. Just check it!
|
29
|
+
email:
|
30
|
+
- andresdavila6@gmail.com
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- ".gitignore"
|
36
|
+
- Gemfile
|
37
|
+
- Gemfile.lock
|
38
|
+
- LICENSE.txt
|
39
|
+
- README.md
|
40
|
+
- lib/moneytize.rb
|
41
|
+
- lib/version.rb
|
42
|
+
- moneytize.gemspec
|
43
|
+
- tests/tc_moneytize.rb
|
44
|
+
homepage: http://moneytize.me
|
45
|
+
licenses:
|
46
|
+
- MIT
|
47
|
+
metadata: {}
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: 1.9.3
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
requirements: []
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 2.2.2
|
65
|
+
signing_key:
|
66
|
+
specification_version: 4
|
67
|
+
summary: format your currency the way you expect.
|
68
|
+
test_files:
|
69
|
+
- tests/tc_moneytize.rb
|
70
|
+
has_rdoc: yard
|