money 6.0.1.beta3 → 6.0.1.beta4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cf2817efb2d831c4dc20a21ad35e2668ef18f877
4
- data.tar.gz: ab6fb4c372bae304d46f672517844816e2d9788e
3
+ metadata.gz: 9f4c3a30959c99ab2533096b20f089c4d0700c90
4
+ data.tar.gz: 698dd9f7be9922faf38f75644aa18f380fabd546
5
5
  SHA512:
6
- metadata.gz: f6a4ce82b5f4d9624d9b1568a39357b0d35d190f2c6b94f30bce534f2b2daffa103e40c9da450788c36c2d994e33eeb1554eb1931655c29fa0cd474ceb971402
7
- data.tar.gz: f08fda3dc489e7031a6937c28a32678bdef279b8e0e8725c832019576f380da9ee4b217aa6a5d7055985dcdff49cec1d6e48117a0e49843ad5792683290356aa
6
+ metadata.gz: a18278df5e345754a702754fdf42b3d22645c7fb84af4ef45a996d58d18815322b96eefea624d2e36baae9f07502f3a63c907ead755984428cf14197cd26bdbe
7
+ data.tar.gz: 791835e4c32da05b0ffb916378da511f6d6b7ad51867b82c8b54102873f27512fea91c853a6ba565bf8dcdfbe97c632a76c66061da6c0dd30a202f29a210cbdb
data/lib/money.rb CHANGED
@@ -1,17 +1,9 @@
1
1
  require "bigdecimal"
2
2
  require "bigdecimal/util"
3
- require "i18n" rescue LoadError
3
+ require "i18n"
4
4
  require "money/currency"
5
5
  require "money/money"
6
6
  require "money/core_extensions/numeric"
7
7
  require "money/core_extensions/string"
8
8
  require "money/core_extensions/symbol"
9
9
  require "money/deprecations"
10
-
11
- class Money
12
- class << self
13
- attr_accessor :silence_core_extensions_deprecations
14
- end
15
-
16
- self.silence_core_extensions_deprecations = false
17
- end
@@ -2,6 +2,6 @@ require "money/core_extensions/numeric"
2
2
  require "money/core_extensions/string"
3
3
  require "money/core_extensions/symbol"
4
4
 
5
- Money.deprecate "as of Money 6.1.0 you must `require 'monetize/core_extensions'` to use core extensions."
5
+ Money.deprecate "as of Money 6.1.0 you must `require 'monetize/core_extensions'` to use core extensions. Please start using the Monetize gem from https://github.com/RubyMoney/monetize if you are not already doing so."
6
6
 
7
7
  Money.silence_core_extensions_deprecations = true
@@ -1,9 +1,7 @@
1
1
  class Numeric
2
2
  alias_method :_to_money, :to_money
3
3
  def to_money(*args)
4
- unless Money.silence_core_extensions_deprecations
5
- Money.deprecate "as of Money 6.1.0 you must `require 'monetize/core_extensions'` to use Numeric#to_money."
6
- end
4
+ Money.deprecate "as of Money 6.1.0 you must `require 'monetize/core_extensions'` to use Numeric#to_money. Please start using the Monetize gem from https://github.com/RubyMoney/monetize if you are not already doing so"
7
5
  _to_money(*args)
8
6
  end
9
7
  end
@@ -1,15 +1,13 @@
1
1
  class String
2
2
  alias_method :_to_money, :to_money
3
3
  def to_money(*args)
4
- Money.deprecate "as of Money 6.1.0 you must `require 'monetize/core_extensions'` to use String#to_money."
4
+ Money.deprecate "as of Money 6.1.0 you must `require 'monetize/core_extensions'` to use String#to_money. Please start using the Monetize gem from https://github.com/RubyMoney/monetize if you are not already doing so"
5
5
  _to_money(*args)
6
6
  end
7
7
 
8
8
  alias_method :_to_currency, :to_currency
9
9
  def to_currency(*args)
10
- unless Money.silence_core_extensions_deprecations
11
- Money.deprecate "as of Money 6.1.0 you must `require 'monetize/core_extensions'` to use String#to_currency."
12
- end
10
+ Money.deprecate "as of Money 6.1.0 you must `require 'monetize/core_extensions'` to use String#to_currency. Please start using the Monetize gem from https://github.com/RubyMoney/monetize if you are not already doing so"
13
11
  _to_currency(*args)
14
12
  end
15
13
  end
@@ -1,9 +1,7 @@
1
1
  class Symbol
2
2
  alias_method :_to_currency, :to_currency
3
3
  def to_currency(*args)
4
- unless Money.silence_core_extensions_deprecations
5
- Money.deprecate "as of Money 6.1.0 you must `require 'monetize/core_extensions'` to use Symbol#to_currency."
6
- end
4
+ Money.deprecate "as of Money 6.1.0 you must `require 'monetize/core_extensions'` to use Symbol#to_currency. Please start using the Monetize gem from https://github.com/RubyMoney/monetize if you are not already doing so"
7
5
  _to_currency(*args)
8
6
  end
9
7
  end
@@ -5,9 +5,11 @@ class Money
5
5
  #
6
6
  # @return [nil]
7
7
  def self.deprecate(message)
8
- file, line = caller(2).first.split(':', 2)
9
- line = line.to_i
8
+ unless Money.silence_core_extensions_deprecations
9
+ file, line = caller(2).first.split(':', 2)
10
+ line = line.to_i
10
11
 
11
- warn "DEPRECATION WARNING: #{message} (called from: #{file}:#{line})"
12
+ warn "DEPRECATION WARNING: #{message} (called from: #{file}:#{line})"
13
+ end
12
14
  end
13
15
  end
data/lib/money/money.rb CHANGED
@@ -18,10 +18,7 @@ require "money/money/formatting"
18
18
  #
19
19
  # @see http://en.wikipedia.org/wiki/Money
20
20
  class Money
21
- include Comparable
22
- include Arithmetic
23
- include Formatting
24
- include Parsing
21
+ include Comparable, Money::Arithmetic, Money::Formatting, Money::Parsing
25
22
 
26
23
  # Convenience method for fractional part of the amount. Synonym of #fractional
27
24
  #
@@ -67,17 +64,6 @@ class Money
67
64
  end
68
65
  end
69
66
 
70
- def as_d(num)
71
- if num.is_a?(Rational)
72
- num.to_d(self.class.conversion_precision)
73
- elsif num.respond_to?(:to_d)
74
- num.to_d
75
- else
76
- BigDecimal.new(num.to_s)
77
- end
78
- end
79
- private :as_d
80
-
81
67
  # @attr_reader [Currency] currency The currency the money is in.
82
68
  # @attr_reader [Money::Bank::*] bank The +Money::Bank+ based object used to
83
69
  # perform currency exchanges with.
@@ -103,7 +89,8 @@ class Money
103
89
  # @attr_accessor [Integer] conversion_precision Use this to specify
104
90
  # precision for converting Rational to BigDecimal
105
91
  attr_accessor :default_bank, :default_currency, :use_i18n,
106
- :assume_from_symbol, :infinite_precision, :conversion_precision
92
+ :assume_from_symbol, :infinite_precision, :conversion_precision,
93
+ :silence_core_extensions_deprecations
107
94
 
108
95
  # @attr_writer rounding_mode Use this to specify the rounding mode
109
96
  attr_writer :rounding_mode
@@ -144,6 +131,9 @@ class Money
144
131
 
145
132
  # Default the conversion of Rationals precision to 16
146
133
  self.conversion_precision = 16
134
+
135
+ # Default alerting about deprecations
136
+ self.silence_core_extensions_deprecations = false
147
137
  end
148
138
 
149
139
  def self.inherited(base)
@@ -393,41 +383,19 @@ class Money
393
383
  # @example
394
384
  # Money.ca_dollar(100).to_s #=> "1.00"
395
385
  def to_s
396
- unit, subunit = fractional().abs.divmod(currency.subunit_to_unit)
397
-
398
- unit_str = ""
399
- subunit_str = ""
400
- fraction_str = ""
401
-
402
- if self.class.infinite_precision
403
- subunit, fraction = subunit.divmod(BigDecimal("1"))
404
-
405
- unit_str = unit.to_i.to_s
406
- subunit_str = subunit.to_i.to_s
407
- fraction_str = fraction.to_s("F")[2..-1] # want fractional part "0.xxx"
408
-
409
- fraction_str = "" if fraction_str =~ /^0+$/
410
- else
411
- unit_str, subunit_str = unit.to_s, subunit.to_s
412
- end
413
-
414
- absolute_str = if currency.decimal_places == 0
415
- if fraction_str == ""
416
- unit_str
417
- else
418
- "#{unit_str}#{decimal_mark}#{fraction_str}"
419
- end
420
- else
421
- # need to pad subunit to right position,
422
- # for example 1 usd 3 cents should be 1.03 not 1.3
423
- subunit_str.insert(0, '0') while subunit_str.length < currency.decimal_places
386
+ unit, subunit, fraction = strings_from_fractional
424
387
 
425
- "#{unit_str}#{decimal_mark}#{subunit_str}#{fraction_str}"
426
- end
388
+ str = if currency.decimal_places == 0
389
+ if fraction == ""
390
+ unit
391
+ else
392
+ "#{unit}#{decimal_mark}#{fraction}"
393
+ end
394
+ else
395
+ "#{unit}#{decimal_mark}#{pad_subunit(subunit)}#{fraction}"
396
+ end
427
397
 
428
- absolute_str.tap do |str|
429
- str.insert(0, "-") if fractional() < 0
430
- end
398
+ fractional < 0 ? "-#{str}" : str
431
399
  end
432
400
 
433
401
  # Return the amount of money as a BigDecimal.
@@ -544,28 +512,6 @@ class Money
544
512
  amounts.collect { |fractional| Money.new(fractional, currency) }
545
513
  end
546
514
 
547
- def allocations_from_splits(splits)
548
- splits.inject(0) { |sum, n| sum + as_d(n) }
549
- end
550
- private :allocations_from_splits
551
-
552
- def amounts_from_splits(allocations, splits)
553
- left_over = fractional
554
-
555
- amounts = splits.map do |ratio|
556
- if self.class.infinite_precision
557
- fractional * ratio
558
- else
559
- (fractional * ratio / allocations).floor.tap do |frac|
560
- left_over -= frac
561
- end
562
- end
563
- end
564
-
565
- [amounts, left_over]
566
- end
567
- private :amounts_from_splits
568
-
569
515
  # Split money amongst parties evenly without loosing pennies.
570
516
  #
571
517
  # @param [Numeric] num number of parties.
@@ -584,24 +530,6 @@ class Money
584
530
  end
585
531
  end
586
532
 
587
- def split_infinite(num)
588
- amt = div(as_d(num))
589
- 1.upto(num).map{amt}
590
- end
591
- private :split_infinite
592
-
593
- def split_flat(num)
594
- low = Money.new(fractional / num, currency)
595
- high = Money.new(low.fractional + 1, currency)
596
-
597
- remainder = fractional % num
598
-
599
- Array.new(num).each_with_index.map do |_, index|
600
- index < remainder ? high : low
601
- end
602
- end
603
- private :split_flat
604
-
605
533
  # Round the monetary amount to smallest unit of coinage.
606
534
  #
607
535
  # @note
@@ -624,4 +552,80 @@ class Money
624
552
  self
625
553
  end
626
554
  end
555
+
556
+ private
557
+
558
+ def as_d(num)
559
+ if num.is_a?(Rational)
560
+ num.to_d(self.class.conversion_precision)
561
+ else
562
+ num.to_d
563
+ end
564
+ rescue NoMethodError
565
+ BigDecimal.new(num.to_s)
566
+ end
567
+
568
+ def strings_from_fractional
569
+ unit, subunit = fractional().abs.divmod(currency.subunit_to_unit)
570
+
571
+ if self.class.infinite_precision
572
+ strings_for_infinite_precision(unit, subunit)
573
+ else
574
+ strings_for_base_precision(unit, subunit)
575
+ end
576
+ end
577
+
578
+ def strings_for_infinite_precision(unit, subunit)
579
+ subunit, fraction = subunit.divmod(BigDecimal("1"))
580
+ fraction = fraction.to_s("F")[2..-1] # want fractional part "0.xxx"
581
+ fraction = "" if fraction =~ /^0+$/
582
+
583
+ [unit.to_i.to_s, subunit.to_i.to_s, fraction]
584
+ end
585
+
586
+ def strings_for_base_precision(unit, subunit)
587
+ [unit.to_s, subunit.to_s, ""]
588
+ end
589
+
590
+ def pad_subunit(subunit)
591
+ cnt = currency.decimal_places
592
+ padding = "0" * cnt
593
+ "#{padding}#{subunit}"[-1 * cnt, cnt]
594
+ end
595
+
596
+ def allocations_from_splits(splits)
597
+ splits.inject(0) { |sum, n| sum + as_d(n) }
598
+ end
599
+
600
+ def amounts_from_splits(allocations, splits)
601
+ left_over = fractional
602
+
603
+ amounts = splits.map do |ratio|
604
+ if self.class.infinite_precision
605
+ fractional * ratio
606
+ else
607
+ (fractional * ratio / allocations).floor.tap do |frac|
608
+ left_over -= frac
609
+ end
610
+ end
611
+ end
612
+
613
+ [amounts, left_over]
614
+ end
615
+
616
+ def split_infinite(num)
617
+ amt = div(as_d(num))
618
+ 1.upto(num).map{amt}
619
+ end
620
+
621
+ def split_flat(num)
622
+ low = Money.new(fractional / num, currency)
623
+ high = Money.new(low.fractional + 1, currency)
624
+
625
+ remainder = fractional % num
626
+
627
+ Array.new(num).each_with_index.map do |_, index|
628
+ index < remainder ? high : low
629
+ end
630
+ end
627
631
  end
@@ -25,7 +25,7 @@ class Money
25
25
  def ==(other_money)
26
26
  if other_money.respond_to?(:to_money)
27
27
  unless other_money.is_a?(Money)
28
- Money.deprecate "as of Money 6.1.0 you must `require 'monetize/core_extensions'` to compare Money to core classes." unless Money.silence_core_extensions_deprecations
28
+ Money.deprecate "as of Money 6.1.0 you must `require 'monetize/core_extensions'` to compare Money to core classes. Please start using the Monetize gem from https://github.com/RubyMoney/monetize if you are not already doing so"
29
29
  end
30
30
  other_money = other_money.to_money
31
31
  fractional == other_money.fractional && currency == other_money.currency
@@ -58,8 +58,8 @@ class Money
58
58
  end
59
59
 
60
60
  def check_compare_deprecate(val)
61
- unless val.is_a?(Money) || Money.silence_core_extensions_deprecations
62
- Money.deprecate "as of Money 6.1.0 you must `require 'monetize/core_extensions'` to compare Money to core classes."
61
+ unless val.is_a?(Money)
62
+ Money.deprecate "as of Money 6.1.0 you must `require 'monetize/core_extensions'` to compare Money to core classes. Please start using the Monetize gem from https://github.com/RubyMoney/monetize if you are not already doing so"
63
63
  end
64
64
  end
65
65
 
@@ -1,37 +1,30 @@
1
1
  # encoding: UTF-8
2
2
  class Money
3
3
  module Formatting
4
-
5
- if Object.const_defined?("I18n")
4
+ def self.included(base)
6
5
  [
7
6
  [:thousands_separator, :delimiter, "."],
8
7
  [:decimal_mark, :separator, ","]
9
8
  ].each do |method, name, character|
10
- define_method(method) do
11
- if self.class.use_i18n
12
- I18n.t(
13
- :"number.currency.format.#{name}",
14
- :default => I18n.t(
15
- :"number.format.#{name}",
16
- :default => (currency.send(method) || character)
17
- )
18
- )
19
- else
20
- currency.send(method) || character
21
- end
22
- end
23
- end
24
- else
25
- def thousands_separator
26
- currency.thousands_separator || ","
9
+ define_i18n_method(method, name, character)
27
10
  end
11
+ end
28
12
 
29
- def decimal_mark
30
- currency.decimal_mark || "."
13
+ def self.define_i18n_method(method, name, character)
14
+ define_method(method) do
15
+ if self.class.use_i18n
16
+ I18n.t(
17
+ :"number.currency.format.#{name}", :default => I18n.t(
18
+ :"number.format.#{name}",
19
+ :default => (currency.send(method) || character)
20
+ )
21
+ )
22
+ else
23
+ currency.send(method) || character
24
+ end
31
25
  end
26
+ alias_method name, method
32
27
  end
33
- alias_method :delimiter, :thousands_separator
34
- alias_method :separator, :decimal_mark
35
28
 
36
29
  # Creates a formatted price string according to several rules.
37
30
  #
data/lib/money/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Money
2
- VERSION = "6.0.1.beta3"
2
+ VERSION = "6.0.1.beta4"
3
3
  end
@@ -3,15 +3,49 @@
3
3
  require "spec_helper"
4
4
 
5
5
  describe Money do
6
- describe "#deprecate" do
7
- let(:error_message) { "Deprecated method triggered here" }
8
-
6
+ describe "::deprecate" do
9
7
  it "should send a deprecation message with caller" do
8
+ error_message = "Deprecated method triggered here"
9
+
10
10
  Money.should_receive(:warn).with do |message|
11
11
  message.should =~ /DEPRECATION WARNING: #{error_message} \(called from:.*:\d+\)/
12
12
  end
13
13
 
14
14
  Money.deprecate(error_message)
15
15
  end
16
+
17
+ context "when silenced" do
18
+ it "should not warn" do
19
+ Money.should_not_receive(:warn)
20
+
21
+ while_silenced { Money.deprecate("anything") }
22
+ end
23
+ end
24
+ end
25
+
26
+ describe "core extensions" do
27
+ it "does not print deprecations when silenced" do
28
+ while_silenced do
29
+ expect_no_deprecation_for { "$1.00".to_money }
30
+ expect_no_deprecation_for { "USD".to_currency }
31
+ expect_no_deprecation_for { 1.to_money }
32
+ expect_no_deprecation_for { :USD.to_currency }
33
+ end
34
+ end
35
+
36
+ def expect_no_deprecation_for(&block)
37
+ Money.should_not_receive(:warn)
38
+ yield
39
+ end
40
+ end
41
+
42
+ def while_silenced(&block)
43
+ begin
44
+ old_setting = Money.silence_core_extensions_deprecations
45
+ Money.silence_core_extensions_deprecations = true
46
+ yield
47
+ ensure
48
+ Money.silence_core_extensions_deprecations = old_setting
49
+ end
16
50
  end
17
51
  end
data/spec/money_spec.rb CHANGED
@@ -43,6 +43,11 @@ describe Money do
43
43
  it { should eq initializing_value }
44
44
  end
45
45
 
46
+ context "given the initializing value doesn't respond to .to_d" do
47
+ let(:initializing_value) { :"1" }
48
+ it { should eq Money.new(1) }
49
+ end
50
+
46
51
  context 'given a currency is not provided' do
47
52
  subject(:money) { Money.new(initializing_value) }
48
53
  its(:currency) { should eq Money.default_currency }
@@ -209,7 +214,7 @@ describe Money do
209
214
  m.fractional.should be_a(Fixnum)
210
215
  end
211
216
  end
212
-
217
+
213
218
  context "loading a serialized Money via YAML" do
214
219
 
215
220
  let(:serialized) { <<YAML
@@ -341,7 +346,7 @@ YAML
341
346
  def m.amount
342
347
  5
343
348
  end
344
-
349
+
345
350
  m.dollars.should == 5
346
351
  end
347
352
  end
@@ -436,6 +441,10 @@ YAML
436
441
  it "suppresses fractional cents when there is none" do
437
442
  Money.new(1.0, "USD").to_s.should == "0.01"
438
443
  end
444
+
445
+ it "shows fractional if needed when :subunut_to_unit == 1" do
446
+ Money.new(10_00.1, "CLP").to_s.should == "1000,1"
447
+ end
439
448
  end
440
449
  end
441
450
 
@@ -643,4 +652,32 @@ YAML
643
652
  (MoneyChild.new(1000) - Money.new(500)).should eq Money.new(500)
644
653
  end
645
654
  end
655
+
656
+ describe "#as_*" do
657
+ before do
658
+ Money.default_bank = Money::Bank::VariableExchange.new
659
+ Money.add_rate("EUR", "USD", 1)
660
+ Money.add_rate("EUR", "CAD", 1)
661
+ Money.add_rate("USD", "EUR", 1)
662
+ end
663
+
664
+ after do
665
+ Money.default_bank = Money::Bank::VariableExchange.instance
666
+ end
667
+
668
+ specify "as_us_dollar converts Money object to USD" do
669
+ obj = Money.new(1, "EUR")
670
+ obj.as_us_dollar.should == Money.new(1, "USD")
671
+ end
672
+
673
+ specify "as_ca_dollar converts Money object to CAD" do
674
+ obj = Money.new(1, "EUR")
675
+ obj.as_ca_dollar.should == Money.new(1, "CAD")
676
+ end
677
+
678
+ specify "as_euro converts Money object to EUR" do
679
+ obj = Money.new(1, "USD")
680
+ obj.as_euro.should == Money.new(1, "EUR")
681
+ end
682
+ end
646
683
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: money
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.0.1.beta3
4
+ version: 6.0.1.beta4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shane Emmons
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-06 00:00:00.000000000 Z
11
+ date: 2014-01-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n