ecr_money 3.6.6 → 3.6.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ class Money::Formatter::CurrencyName
2
+ include Money::Formatter
3
+
4
+ def initialize(options)
5
+ @options = options
6
+ end
7
+
8
+ def class_format
9
+ "#{formatted} #{currency.to_s}" if rules[:with_currency]
10
+ end
11
+
12
+ def next_formatter
13
+ Money::Formatter::CurrencySymbol
14
+ end
15
+ end
@@ -0,0 +1,50 @@
1
+ class Money::Formatter::CurrencySymbol
2
+ include Money::Formatter
3
+
4
+ def initialize(options)
5
+ @options = options
6
+ @symbol_value, @symbol_position = symbol_value(rules), symbol_position
7
+ end
8
+
9
+ def symbol_not_defined_by_user?
10
+ if rules.has_key? :symbol
11
+ return rules[:symbol] === false || rules[:symbol].nil? || rules[:symbol] == ''
12
+ end
13
+ true
14
+ end
15
+
16
+ def class_format
17
+ return formatted if symbol_not_defined_by_user?
18
+ add_symbol if @symbol_value && !@symbol_value.empty?
19
+ end
20
+
21
+ def next_formatter
22
+ Money::Formatter::DecimalMark
23
+ end
24
+
25
+ private
26
+ def add_symbol
27
+ place_symbol_in_correct_position
28
+ end
29
+
30
+ def place_symbol_in_correct_position
31
+ @symbol_position == :before ? "#{@symbol_value}#{formatted}" :
32
+ "#{formatted} #{@symbol_value}"
33
+ end
34
+
35
+ def symbol_value(rules)
36
+ return symbol_from_rule(rules[:symbol]) if rules.has_key?(:symbol)
37
+ return symbol
38
+ end
39
+
40
+ def symbol_from_rule(symbol_in_rule)
41
+ return symbol if symbol_in_rule === true
42
+ return symbol_in_rule if symbol_in_rule
43
+ return ''
44
+ end
45
+
46
+ def symbol_position
47
+ return :before if currency.symbol_first?
48
+ return :after
49
+ end
50
+ end
@@ -0,0 +1,38 @@
1
+ class Money::Formatter::DecimalMark
2
+ include Money::Formatter
3
+
4
+ def initialize(options)
5
+ @options = options
6
+ end
7
+
8
+ def class_format
9
+ return formatted if rule_decimal_mark === false
10
+ return add_mark if cents > 0
11
+ return formatted
12
+ end
13
+
14
+ def next_formatter
15
+ Money::Formatter::DisplayFree
16
+ end
17
+
18
+ private
19
+ def mark_specified_by_user?
20
+ rule_decimal_mark && rule_decimal_mark != true
21
+ end
22
+
23
+ def rule_decimal_mark
24
+ return rules[:decimal_mark] if rules
25
+ end
26
+
27
+ def my_subunit
28
+ "#{subunit}".gsub!('.','')
29
+ end
30
+
31
+ def mark
32
+ (mark_specified_by_user? ? rule_decimal_mark : decimal_mark)
33
+ end
34
+
35
+ def add_mark
36
+ formatted.insert(formatted.index("#{unit}") + unit.length, mark)
37
+ end
38
+ end
@@ -0,0 +1,19 @@
1
+ class Money::Formatter::DisplayFree
2
+ include Money::Formatter
3
+
4
+ def initialize(options)
5
+ @options = options
6
+ @display_free = rules[:display_free] || false if rules
7
+ end
8
+
9
+ def class_format
10
+ return formatted if cents > 0
11
+ return @display_free if @display_free.respond_to?(:to_str)
12
+ return "free" if @display_free
13
+ return formatted
14
+ end
15
+
16
+ def next_formatter
17
+ Money::Formatter::ThousandSeparator
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ class Money::Formatter::NoCents
2
+ include Money::Formatter
3
+
4
+ def initialize(options)
5
+ @options = options
6
+ end
7
+
8
+ def class_format
9
+ if rules
10
+ return unit if rules[:no_cents]
11
+ end
12
+ return formatted
13
+ end
14
+
15
+ def next_formatter
16
+ nil
17
+ end
18
+ end
@@ -0,0 +1,35 @@
1
+ class Money::Formatter::ThousandSeparator
2
+ include Money::Formatter
3
+
4
+ def initialize(options)
5
+ @options = options
6
+ end
7
+
8
+ def class_format
9
+ return add_separator if rules.has_key? :thousands_separator
10
+ formatted
11
+ end
12
+
13
+ def next_formatter
14
+ Money::Formatter::NoCents
15
+ end
16
+
17
+ private
18
+ def separator
19
+ return thousands_separator if use_default_separator?
20
+ return '' if separator_not_set?
21
+ return rules[:thousands_separator]
22
+ end
23
+
24
+ def add_separator
25
+ formatted.gsub(/(\d)(?=(?:\d{3})+(?:[^\d]|$))/, "\\1#{separator}")
26
+ end
27
+
28
+ def use_default_separator?
29
+ rules[:thousands_separator] === true
30
+ end
31
+
32
+ def separator_not_set?
33
+ rules[:thousands_separator] === false || rules[:thousands_separator].nil?
34
+ end
35
+ end
@@ -0,0 +1,26 @@
1
+ module Money::Formatter
2
+ def format
3
+ new_string = self.class_format
4
+ params = @options.dup.merge(:previous => self, :formatted_string => new_string)
5
+ return next_formatter.new(params).format if next_formatter
6
+ return new_string
7
+ end
8
+
9
+ def method_missing(method, *args, &block)
10
+ begin
11
+ @options[:previous].send(method, args, &block)
12
+ rescue
13
+ @options[:previous].send(method, &block)
14
+ end
15
+ end
16
+
17
+ def formatted
18
+ @options[:formatted_string]
19
+ end
20
+
21
+ def rules
22
+ @options[:rules] || {}
23
+ end
24
+ end
25
+
26
+ Dir.glob(File.dirname(__FILE__) + '/formatter/*', &method(:require))
data/lib/money/money.rb CHANGED
@@ -22,6 +22,10 @@ class Money
22
22
 
23
23
  attr_reader :exact_number
24
24
 
25
+ attr_reader :unit
26
+
27
+ attr_reader :subunit
28
+
25
29
  # Class Methods
26
30
  class << self
27
31
  # Each Money object is associated to a bank object, which is responsible
@@ -392,6 +396,7 @@ class Money
392
396
  @cents = exact_number.round.to_i
393
397
  @currency = Currency.wrap(currency)
394
398
  @bank = bank
399
+ @unit, @subunit = @exact_number.to_f.abs.divmod(@currency.subunit_to_unit).map{|o| o.to_s}
395
400
  end
396
401
 
397
402
  # Returns the value of the money in dollars,
@@ -841,6 +846,12 @@ class Money
841
846
  # @example
842
847
  # s = Money.ca_dollar(570).format(:html => true, :with_currency => true)
843
848
  # s #=> "$5.70 <span class=\"currency\">CAD</span>"
849
+ def huh
850
+ rules = normalize_formatting_rules(rules)
851
+ puts rules.inspect
852
+ Money::Formatter::CurrencyName.new(:previous => self, :formatted_string => self.cents.to_s, :rules => rules).format
853
+ end
854
+
844
855
  def format(*rules)
845
856
  # support for old format parameters
846
857
  rules = normalize_formatting_rules(rules)
@@ -874,6 +885,9 @@ class Money
874
885
  else
875
886
  "#{self.to_s}"
876
887
  end
888
+ if rules[:decimals] && !rules[:no_cents]
889
+ formatted = formatted + exact_number.to_f.to_s.split('.')[1]
890
+ end
877
891
 
878
892
  symbol_position =
879
893
  if rules.has_key?(:symbol_position)
@@ -888,9 +902,12 @@ class Money
888
902
  formatted = (symbol_position == :before ? "#{symbol_value}#{formatted}" : "#{formatted} #{symbol_value}")
889
903
  end
890
904
 
905
+ d_mark = decimal_mark
906
+
891
907
  if rules.has_key?(:decimal_mark) and rules[:decimal_mark] and
892
908
  rules[:decimal_mark] != decimal_mark
893
909
  formatted.sub!(decimal_mark, rules[:decimal_mark])
910
+ d_mark = rules[:decimal_mark]
894
911
  end
895
912
 
896
913
  thousands_separator_value = thousands_separator
@@ -904,7 +921,18 @@ class Money
904
921
  end
905
922
 
906
923
  # Apply thousands_separator
907
- formatted.gsub!(/(\d)(?=(?:\d{3})+(?:[^\d]|$))/, "\\1#{thousands_separator_value}")
924
+ if (rules.has_key?(:decimal_mark) and rules[:decimal_mark]) or rules[:decimals]
925
+ decimals = rules[:decimals] || 2
926
+ integer, rational = formatted.split(d_mark)
927
+ rational, symbol = rational.split
928
+ integer.gsub!(/(\d)(?=(?:\d{3})+(?:[^\d]|$))/, "\\1#{thousands_separator_value}")
929
+ rational_slice = rational.slice(0, decimals)
930
+ (decimals - rational_slice.length).times { rational_slice << '0' }
931
+ formatted = (integer + d_mark + rational_slice + " #{symbol}").strip
932
+ else
933
+ formatted.gsub!(/(\d)(?=(?:\d{3})+(?:[^\d]|$))/, "\\1#{thousands_separator_value}")
934
+ end
935
+
908
936
 
909
937
  if rules[:with_currency]
910
938
  formatted << " "
@@ -922,14 +950,14 @@ class Money
922
950
  # @example
923
951
  # Money.ca_dollar(100).to_s #=> "1.00"
924
952
  def to_s
925
- unit, subunit = cents.abs.divmod(currency.subunit_to_unit).map{|o| o.to_s}
926
953
  if currency.decimal_places == 0
927
- return "-#{unit}" if cents < 0
928
- return unit
954
+ return "-#{@unit}" if exact_number.to_f < 0
955
+ return @unit
929
956
  end
930
- subunit = (("0" * currency.decimal_places) + subunit)[(-1*currency.decimal_places)..-1]
931
- return "-#{unit}#{decimal_mark}#{subunit}" if cents < 0
932
- "#{unit}#{decimal_mark}#{subunit}"
957
+ subunit = (("0" * currency.decimal_places) + @subunit.to_i.to_s)[(-1*currency.decimal_places)..-1]
958
+
959
+ return "-#{@unit}#{decimal_mark}#{subunit}" if exact_number.to_f < 0
960
+ "#{@unit}#{decimal_mark}#{subunit}"
933
961
  end
934
962
 
935
963
  # Return the amount of money as a float. Floating points cannot guarantee
data/lib/money.rb CHANGED
@@ -25,3 +25,5 @@ require 'i18n' rescue LoadError
25
25
  require 'money/currency'
26
26
  require 'money/money'
27
27
  require 'money/core_extensions'
28
+ require 'money/formatter'
29
+
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 3
7
7
  - 6
8
- - 6
9
- version: 3.6.6
8
+ - 7
9
+ version: 3.6.7
10
10
  platform: ruby
11
11
  authors:
12
12
  - "Alberto Pe\xC3\xB1a"
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-02-18 00:00:00 +00:00
18
+ date: 2011-02-24 00:00:00 +00:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -88,6 +88,13 @@ files:
88
88
  - lib/money/bank/variable_exchange.rb
89
89
  - lib/money/core_extensions.rb
90
90
  - lib/money/currency.rb
91
+ - lib/money/formatter/currency_name.rb
92
+ - lib/money/formatter/currency_symbol.rb
93
+ - lib/money/formatter/decimal_mark.rb
94
+ - lib/money/formatter/display_free.rb
95
+ - lib/money/formatter/no_cents.rb
96
+ - lib/money/formatter/thousand_separator.rb
97
+ - lib/money/formatter.rb
91
98
  - lib/money/money.rb
92
99
  - lib/money.rb
93
100
  - CHANGELOG.md