lib/brcobranca/currency.rb in brcobranca-3.2.0 vs lib/brcobranca/currency.rb in brcobranca-4.0.0

- old
+ new

@@ -3,17 +3,17 @@ # @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 => "") + 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? + def to_number(_options = {}) + return gsub(/,/, '.').to_f if self.numeric? nil end def numeric? self =~ /^(\+|-)?[0-9]+((\.|,)[0-9]+)?$/ ? true : false @@ -26,43 +26,43 @@ 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] : "" + 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 + position == 'before' ? unit + number : number + unit rescue number end end - def with_delimiter(delimiter=",", separator=".") + 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) + def with_precision(precision = 3) number = self "%01.#{precision}f" % number end end end end -[ Numeric, String].each do |klass| +[Numeric, String].each do |klass| klass.class_eval { include Brcobranca::Currency::Number } end -[ String ].each do |klass| +[String].each do |klass| klass.class_eval { include Brcobranca::Currency::String } end