-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
currency.rb
executable file
·66 lines (58 loc) · 2.13 KB
/
currency.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# @author Fernando Vieira do http://simplesideias.com.br
module Brcobranca #:nodoc:[all]
module Currency #:nodoc:[all]
# Implementação feita por Fernando Vieira do http://simplesideias.com.br
# post http://simplesideias.com.br/usando-number_to_currency-em-modelos-no-rails
BRL = {:delimiter => ".", :separator => ",", :unit => "R$", :precision => 2, :position => "before"}
USD = {:delimiter => ',', :separator => ".", :unit => "US$", :precision => 2, :position => "before"}
DEFAULT = BRL.merge(:unit => "")
module String #:nodoc:[all]
def to_number(options={})
return self.gsub(/,/, '.').to_f if self.numeric?
nil
end
def numeric?
self =~ /^(\+|-)?[0-9]+((\.|,)[0-9]+)?$/ ? true : false
end
end
module Number #:nodoc:[all]
def to_currency(options = {})
number = self
default = Brcobranca::Currency::DEFAULT
options = default.merge(options)
precision = options[:precision] || default[:precision]
unit = options[:unit] || default[:unit]
position = options[:position] || default[:position]
separator = precision > 0 ? options[:separator] || default[:separator] : ""
delimiter = options[:delimiter] || default[:delimiter]
begin
parts = number.with_precision(precision).split('.')
number = parts[0].to_i.with_delimiter(delimiter) + separator + parts[1].to_s
position == "before" ? unit + number : number + unit
rescue
number
end
end
def with_delimiter(delimiter=",", separator=".")
number = self
begin
parts = number.to_s.split(separator)
parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}")
parts.join separator
rescue
self
end
end
def with_precision(precision=3)
number = self
"%01.#{precision}f" % number
end
end
end
end
[ Numeric, String].each do |klass|
klass.class_eval { include Brcobranca::Currency::Number }
end
[ String ].each do |klass|
klass.class_eval { include Brcobranca::Currency::String }
end