Permalink
Browse files

Removes the currency module, there's no need for it, and it's bad code.

Moved the parts needed to boleto::base, specs green
  • Loading branch information...
nofxx committed Feb 19, 2013
1 parent 820d2bc commit a395ffff676196521e112e9b880a7d40909e35be
View
@@ -5,7 +5,7 @@ gemspec
gem "rake"
-group :test do
- gem "rspec", :require => false
- gem "ruby-debug", :require => false, :platform => :ruby_18
-end
+#group :test do
+# gem "rspec", :require => false
+# gem "ruby-debug", :require => false #, :platform => :ruby_18
+#end
View
@@ -1,53 +0,0 @@
-PATH
- remote: .
- specs:
- brcobranca (3.1.2)
- activemodel (~> 3)
- parseline (>= 1.0.3)
- rghost (~> 0.8)
- rghost_barcode (~> 0.9)
-
-GEM
- remote: http://rubygems.org/
- specs:
- activemodel (3.2.11)
- activesupport (= 3.2.11)
- builder (~> 3.0.0)
- activesupport (3.2.11)
- i18n (~> 0.6)
- multi_json (~> 1.0)
- builder (3.0.4)
- columnize (0.3.6)
- diff-lcs (1.1.3)
- i18n (0.6.1)
- linecache (0.46)
- rbx-require-relative (> 0.0.4)
- multi_json (1.5.0)
- parseline (1.0.3)
- rake (0.9.2.2)
- rbx-require-relative (0.0.9)
- rghost (0.8.7.9)
- rghost_barcode (0.9)
- rspec (2.9.0)
- rspec-core (~> 2.9.0)
- rspec-expectations (~> 2.9.0)
- rspec-mocks (~> 2.9.0)
- rspec-core (2.9.0)
- rspec-expectations (2.9.1)
- diff-lcs (~> 1.1.3)
- rspec-mocks (2.9.0)
- ruby-debug (0.10.4)
- columnize (>= 0.1)
- ruby-debug-base (~> 0.10.4.0)
- ruby-debug-base (0.10.4)
- linecache (>= 0.3)
-
-PLATFORMS
- ruby
- x86-mingw32
-
-DEPENDENCIES
- brcobranca!
- rake
- rspec
- ruby-debug
View
@@ -1,4 +1,4 @@
-# -*- encoding: utf-8 -*-
+# -*- coding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "brcobranca/version"
@@ -26,9 +26,9 @@ Gem::Specification.new do |s|
s.add_runtime_dependency(%q<parseline>, [">= 1.0.3"])
s.add_runtime_dependency(%q<activemodel>, ["~> 3"])
- s.post_install_message = %[
- ===========================================================================
- Visite http://www.boletorails.com.br para ver exemplos!
- ===========================================================================
- ]
-end
+ # s.post_install_message = %[
+ # ===========================================================================
+ # Visite http://www.boletorails.com.br para ver exemplos!
+ # ===========================================================================
+ # ]
+end
View
@@ -4,7 +4,6 @@
require 'brcobranca/limpeza'
require 'brcobranca/formatacao'
require 'brcobranca/calculo_data'
-require 'brcobranca/currency'
begin
require 'date'
@@ -120,4 +119,3 @@ module Retorno
autoload :RetornoCnab240, 'brcobranca/retorno/retorno_cnab240'
end
end
-
@@ -1,10 +1,13 @@
-# -*- encoding: utf-8 -*-
+# -*- coding: utf-8 -*-
# @author Kivanio Barbosa
module Brcobranca
module Boleto
# Classe base para todas as classes de boletos
class Base
extend Template::Base
+ BRL = {:delimiter => ".", :separator => ",", :unit => "R$", :precision => 2, :position => "before"}
+ USD = {:delimiter => ',', :separator => ".", :unit => "US$", :precision => 2, :position => "before"}
+ DEFAULT_CURR = BRL.merge(:unit => "")
# Configura gerador de arquivo de boleto e código de barras.
extend define_template(Brcobranca.configuration.gerador)
@@ -145,6 +148,14 @@ def valor_documento
'%.2f' % (self.quantidade.to_f * self.valor.to_f)
end
+ def valor_print
+ to_currency valor
+ end
+
+ def valor_documento_print
+ to_currency valor_documento
+ end
+
# Data de vencimento baseado na <b>data_documento + dias_vencimento</b>
#
# @return [Date]
@@ -202,6 +213,40 @@ def codigo_barras_segunda_parte
private
+ def to_currency(number, options = {})
+ default = DEFAULT_CURR
+ 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
+
# Monta a primeira parte do código de barras, que é a mesma para todos bancos.
# @return [String] 18 caracteres numéricos.
def codigo_barras_primeira_parte
@@ -1,4 +1,4 @@
-# -*- encoding: utf-8 -*-
+# -*- coding: utf-8 -*-
begin
require 'rghost'
@@ -156,7 +156,7 @@ def modelo_generico_cabecalho(doc, boleto)
doc.moveto :x => '16.5 cm' , :y => '23 cm'
doc.show boleto.nosso_numero_boleto
doc.moveto :x => '16.5 cm' , :y => '22.2 cm'
- doc.show boleto.valor_documento.to_currency
+ doc.show boleto.valor_documento_print
doc.moveto :x => '1.4 cm' , :y => '20.9 cm'
doc.show "#{boleto.sacado} - #{boleto.sacado_documento.formata_documento}"
doc.moveto :x => '1.4 cm' , :y => '20.6 cm'
@@ -200,9 +200,9 @@ def modelo_generico_rodape(doc, boleto)
doc.moveto :x => '8 cm' , :y => '13.5 cm'
doc.show boleto.quantidade
doc.moveto :x => '11 cm' , :y => '13.5 cm'
- doc.show boleto.valor.to_currency
+ doc.show boleto.valor_print
doc.moveto :x => '16.5 cm' , :y => '13.5 cm'
- doc.show boleto.valor_documento.to_currency
+ doc.show boleto.valor_documento_print
doc.moveto :x => '0.7 cm' , :y => '12.7 cm'
doc.show boleto.instrucao1
doc.moveto :x => '0.7 cm' , :y => '12.3 cm'
@@ -226,4 +226,3 @@ def modelo_generico_rodape(doc, boleto)
end
end
end
-
View
@@ -1,66 +0,0 @@
-# @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
@@ -1,4 +1,4 @@
-# -*- encoding: utf-8 -*-
+# -*- coding: utf-8 -*-
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe Brcobranca::Boleto::Bradesco do
@@ -189,4 +189,4 @@
end
end
-end
+end
@@ -1,4 +1,4 @@
-# -*- encoding: utf-8 -*-
+# -*- coding: utf-8 -*-
require File.dirname(__FILE__) + '/../spec_helper.rb'
module Brcobranca #:nodoc:[all]
@@ -181,5 +181,82 @@ module Boleto #:nodoc:[all]
end
end
+
+ # describe String do
+ # it "should return true if value seted is a valid ruby number" do
+ # "1234".numeric?.should be_true
+ # "123.4".numeric?.should be_true
+ # "123,4".numeric?.should be_true
+ # "1234.03".numeric?.should be_true
+ # "1234,03".numeric?.should be_true
+ # "-1234".numeric?.should be_true
+ # "-123.4".numeric?.should be_true
+ # "-123,4".numeric?.should be_true
+ # "-1234.03".numeric?.should be_true
+ # "-1234,03".numeric?.should be_true
+ # "+1234".numeric?.should be_true
+ # "+123.4".numeric?.should be_true
+ # "+123,4".numeric?.should be_true
+ # "+1234.03".numeric?.should be_true
+ # "+1234,03".numeric?.should be_true
+ # end
+
+ # it "should return false if value seted is NOT a valid ruby number" do
+ # "1234,".numeric?.should be_false
+ # "1234.".numeric?.should be_false
+ # "1,234.03".numeric?.should be_false
+ # "1.234.03".numeric?.should be_false
+ # "1,234,03".numeric?.should be_false
+ # "12.340,03".numeric?.should be_false
+ # "1234ab".numeric?.should be_false
+ # "ab1213".numeric?.should be_false
+ # "ffab".numeric?.should be_false
+ # end
+
+ # it "should convert value seted to valid ruby number" do
+ # "1234".to_number.should eql(1234.0)
+ # "123.4".to_number.should eql(123.4)
+ # "123,4".to_number.should eql(123.4)
+ # "1234.03".to_number.should eql(1234.03)
+ # "1234,03".to_number.should eql(1234.03)
+ # end
+
+ # it "should return nil when is not possible convert value seted" do
+ # "1234,".to_number.should be_nil
+ # "1234.".to_number.should be_nil
+ # "1,234.03".to_number.should be_nil
+ # "1.234.03".to_number.should be_nil
+ # "1,234,03".to_number.should be_nil
+ # "12.340,03".to_number.should be_nil
+ # "1234ab".to_number.should be_nil
+ # "ab1213".to_number.should be_nil
+ # "ffab".to_number.should be_nil
+ # end
+ # end
+
+ # describe Number do
+ # it "should convert value seted to currency value" do
+ # 1234.to_currency.should eql("1.234,00")
+ # 123.4.to_currency.should eql("123,40")
+ # 1234.03.to_currency.should eql("1.234,03")
+ # 1234.03.to_currency(options = {:unit => "R$ "}).should eql("R$ 1.234,03")
+ # 1234.03.to_currency(options = {:unit => "R$ ",:separator => "."}).should eql("R$ 1.234.03")
+ # 1234.03.to_currency(options = {:unit => "R$ ",:separator => ".",:delimiter => ','}).should eql("R$ 1,234.03")
+ # 1234.03.to_currency(options = {:unit => "R$ ", :precision => 3}).should eql("R$ 1.234,030")
+ # end
+
+ # it "should convert value seted using delimiter and separator" do
+ # 1234.with_delimiter.should eql("1,234")
+ # 123.4.with_delimiter.should eql("123.4")
+ # 1234.03.with_delimiter.should eql("1,234.03")
+ # end
+
+ # it "should convert value seted using precision " do
+ # 1234.with_precision.should eql("1234.000")
+ # 123.4.with_precision.should eql("123.400")
+ # 1234.03.with_precision.should eql("1234.030")
+ # end
+ # end
+
end
-end
+end
Oops, something went wrong.

0 comments on commit a395fff

Please sign in to comment.