crazy_money 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 12d32e285aa7e2ce6a53feee1d214718ad5df46d
4
- data.tar.gz: fcaa7be42922ae7fc23b1143cfdb4b496edf05ab
3
+ metadata.gz: 4e522634c0557a8ca099b26039e636b01264fafb
4
+ data.tar.gz: 43fded46f56621a24692d6fc2ee5a93fec20d38b
5
5
  SHA512:
6
- metadata.gz: cce29b0c6b9ebae13e85ffefc8ed7b10cb290b4934895397ec054199672b221a0a95ccf997157146bd273061667d379d764e25c37ca3b97b4a57eee01ad1ef71
7
- data.tar.gz: 8c492358bd8c5e5fb0e223d1671800aa440d12d902f6899c437cd3427e7b6c9879c2f200c33d98cf3b2392d37cdf46b3a33cfa15cda0f8736ea3461302149866
6
+ metadata.gz: 6cf5442027916f0f7b1d1d0dec4f6b0fada91fef7354cab787ab5033fdf03cfda93607f18149b1e5f918162a8b6afe3da80774b7fce3959541a89f3ce887a9af
7
+ data.tar.gz: 551da890dc3a1b473583192798324bc9e60f9e9d9c9351eb47aa4aafae3b259e97071b3b152fd9603b1a94b6724a69391718eae134304fef3dcce892b8a1b9bb
data/.travis.yml CHANGED
@@ -1,6 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.9.3
4
3
  - 2.0.0
5
4
  - 2.1
6
5
  notifications:
data/crazy_money.gemspec CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "crazy_money"
7
- spec.version = "1.1.1"
7
+ spec.version = "1.2.0"
8
8
  spec.authors = ["Cédric Félizard"]
9
9
  spec.email = ["cedric@felizard.fr"]
10
10
  spec.description = %q{Simple money wrapper}
@@ -17,9 +17,10 @@ Gem::Specification.new do |spec|
17
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
18
  spec.require_paths = ["lib"]
19
19
 
20
- spec.add_runtime_dependency "currency_data", "~> 1.0.1"
20
+ spec.add_runtime_dependency "i18n", "~> 0.6.11"
21
+ spec.add_runtime_dependency "currency_data", "~> 1.1.0"
21
22
 
22
23
  spec.add_development_dependency "bundler", "~> 1.3"
23
24
  spec.add_development_dependency "rake"
24
- spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "rspec", "~> 3"
25
26
  end
data/lib/crazy_money.rb CHANGED
@@ -1,9 +1,16 @@
1
1
  require "bigdecimal"
2
+ require "i18n"
2
3
  require "currency_data"
3
4
 
4
5
  class CrazyMoney
5
6
  include Comparable
6
7
 
8
+ module Configuration
9
+ class << self
10
+ attr_accessor :current_currency
11
+ end
12
+ end
13
+
7
14
  def self.zero
8
15
  new 0
9
16
  end
@@ -12,8 +19,12 @@ class CrazyMoney
12
19
  @amount = BigDecimal.new(amount.to_s)
13
20
  end
14
21
 
15
- def to_s
16
- sprintf("%.2f", @amount)
22
+ def to_s(decimal_places: 2)
23
+ if current_currency = Configuration.current_currency
24
+ decimal_places = currency(current_currency).decimal_places
25
+ end
26
+
27
+ sprintf("%.#{decimal_places}f", @amount)
17
28
  end
18
29
 
19
30
  def inspect
@@ -58,22 +69,28 @@ class CrazyMoney
58
69
  def with_currency iso_code
59
70
  currency = currency(iso_code) || raise(ArgumentError, "Unknown currency: #{iso_code.inspect}")
60
71
 
61
- left, right = to_s.split(".")
72
+ left, right = to_s(decimal_places: currency.decimal_places).split(".")
73
+ decimal_mark = right.nil? ? "" : currency.decimal_mark
62
74
  sign = left.slice!("-")
63
75
 
64
76
  left = left.reverse.scan(/.{1,3}/).map(&:reverse).reverse. # split every 3 digits right-to-left
65
- join(currency.thousands_separator)
77
+ join(thousands_separator)
66
78
 
67
- formatted = [sign, left, currency.decimal_mark, right].join
79
+ formatted = [sign, left, decimal_mark, right].join
68
80
 
69
- formatted.public_send(
70
- currency.symbol_first ? :prepend : :concat,
71
- currency.symbol
72
- )
81
+ if currency.symbol_first
82
+ [currency.prefered_symbol, formatted]
83
+ else
84
+ [formatted, " ", currency.prefered_symbol]
85
+ end.join
73
86
  end
74
87
 
75
88
  private
76
89
 
90
+ def thousands_separator
91
+ I18n.t("number.currency.format.thousands_separator", default: " ")
92
+ end
93
+
77
94
  def currency iso_code
78
95
  ::CurrencyData.find(iso_code)
79
96
  end
@@ -114,10 +114,11 @@ describe CrazyMoney do
114
114
  specify { expect(CrazyMoney.new("-13.37").with_currency("EUR")).to eq("€-13,37") }
115
115
  specify { expect(CrazyMoney.new("0").with_currency("EUR")).to eq("€0,00") }
116
116
 
117
- specify { expect(CrazyMoney.new("-1234567.89").with_currency("USD")).to eq("$-1,234,567.89") }
118
- specify { expect(CrazyMoney.new("-1234567.89").with_currency("EUR")).to eq("€-1.234.567,89") }
117
+ specify { expect(CrazyMoney.new("-1234567.89").with_currency("USD")).to eq("$-1 234 567.89") }
118
+ specify { expect(CrazyMoney.new("-1234567.89").with_currency("EUR")).to eq("€-1 234 567,89") }
119
119
 
120
- specify { expect(CrazyMoney.new("13.37").with_currency("SEK")).to eq("13,37kr") }
120
+ specify { expect(CrazyMoney.new("13.37").with_currency("SEK")).to eq("13,37 kr") }
121
+ specify { expect(CrazyMoney.new("1337").with_currency("XPF")).to eq("1 337 CFP") }
121
122
  end
122
123
  end
123
124
 
metadata CHANGED
@@ -1,29 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crazy_money
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cédric Félizard
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-25 00:00:00.000000000 Z
11
+ date: 2014-07-21 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: i18n
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.6.11
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.6.11
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: currency_data
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
31
  - - "~>"
18
32
  - !ruby/object:Gem::Version
19
- version: 1.0.1
33
+ version: 1.1.0
20
34
  type: :runtime
21
35
  prerelease: false
22
36
  version_requirements: !ruby/object:Gem::Requirement
23
37
  requirements:
24
38
  - - "~>"
25
39
  - !ruby/object:Gem::Version
26
- version: 1.0.1
40
+ version: 1.1.0
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -56,16 +70,16 @@ dependencies:
56
70
  name: rspec
57
71
  requirement: !ruby/object:Gem::Requirement
58
72
  requirements:
59
- - - ">="
73
+ - - "~>"
60
74
  - !ruby/object:Gem::Version
61
- version: '0'
75
+ version: '3'
62
76
  type: :development
63
77
  prerelease: false
64
78
  version_requirements: !ruby/object:Gem::Requirement
65
79
  requirements:
66
- - - ">="
80
+ - - "~>"
67
81
  - !ruby/object:Gem::Version
68
- version: '0'
82
+ version: '3'
69
83
  description: Simple money wrapper
70
84
  email:
71
85
  - cedric@felizard.fr