brdinheiro 0.0.1 → 2.0.0

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.
data/CHANGELOG CHANGED
@@ -0,0 +1 @@
1
+ Version 0.0.1 -> Versão beta da gem.
data/README CHANGED
@@ -0,0 +1,52 @@
1
+ == Como usar o Dinheiro em seu ActiveRecord?
2
+
3
+ * Arquivo 001_create_lancamentos.rb:
4
+
5
+ class CreateLancamentos < ActiveRecord::Migration
6
+ def self.up
7
+ create_table :lancamentos do |t|
8
+ t.column :descricao, :string, :null => false
9
+ t.column :valor, :decimal, :precision => 14, :scale => 2
10
+ t.column :mensalidade, :decimal, :precision => 14, :scale => 2
11
+ end
12
+ end
13
+
14
+ def self.down
15
+ drop_table :lancamentos
16
+ end
17
+ end
18
+
19
+ * Arquivo lancamento.rb:
20
+
21
+ class Lancamento < ActiveRecord::Base
22
+ usar_como_dinheiro :valor, :mensalidade
23
+ end
24
+
25
+ * No console (script/console):
26
+
27
+ Loading development environment.
28
+ >> lancamento = Lancamento.new
29
+ => #<Lancamento:0x9652cd8 @attributes={"descricao"=>nil,
30
+ "valor"=>#<BigDecimal:9657008,'0.0',4(4)>,
31
+ "mensalidade"=>#<BigDecimal:9656e8c,'0.0',4(4)>},
32
+ @new_record=true>
33
+ >> lancamento.valor = 100
34
+ => 100
35
+ >> lancamento.valor
36
+ => #<Dinheiro:0x9650f3c @quantia=10000>
37
+ >> lancamento.valor.real
38
+ => "R$ 100,00"
39
+ >> lancamento.valor = 100.50
40
+ => 100.5
41
+ >> lancamento.valor.real
42
+ => "R$ 100,50"
43
+ >> lancamento.valor = "250.50"
44
+ => "250.50"
45
+ >> lancamento.valor.real
46
+ => "R$ 250,50"
47
+ >> lancamento.valor = 354.58.reais
48
+ => #<Dinheiro:0x9646384 @quantia=35458>
49
+ >> lancamento.valor.real
50
+ => "R$ 354,58"
51
+ >> exit
52
+
data/Rakefile CHANGED
@@ -29,17 +29,16 @@ Rake::TestTask.new { |t|
29
29
  }
30
30
 
31
31
 
32
- # Generate the RDoc documentation
33
- # Rake::RDocTask.new { |rdoc|
34
- # rdoc.rdoc_dir = 'doc'
35
- # rdoc.title = "Action Mailer -- Easy email delivery and testing"
36
- # rdoc.options << '--line-numbers' << '--inline-source' << '-A cattr_accessor=object'
37
- # rdoc.options << '--charset' << 'utf-8'
38
- # rdoc.template = "#{ENV['template']}.rb" if ENV['template']
39
- # rdoc.rdoc_files.include('README', 'CHANGELOG')
40
- # rdoc.rdoc_files.include('lib/action_mailer.rb')
41
- # rdoc.rdoc_files.include('lib/action_mailer/*.rb')
42
- # }
32
+ #Generate the RDoc documentation
33
+ Rake::RDocTask.new { |rdoc|
34
+ rdoc.rdoc_dir = 'doc'
35
+ rdoc.title = "Brazilian Rails -- Dinheiro"
36
+ rdoc.options << '--line-numbers' << '--inline-source' << '-A cattr_accessor=object'
37
+ rdoc.options << '--charset' << 'utf-8'
38
+ rdoc.template = "#{ENV['template']}.rb" if ENV['template']
39
+ rdoc.rdoc_files.include('README', 'CHANGELOG')
40
+ rdoc.rdoc_files.include('lib/**/*')
41
+ }
43
42
 
44
43
 
45
44
  # Create compressed packages
@@ -50,20 +49,21 @@ spec = Gem::Specification.new do |s|
50
49
  s.description = %q{brdinheiro é uma das gems que compoem o Brazilian Rails}
51
50
  s.version = PKG_VERSION
52
51
 
53
- s.author = "Marcos Tapajós"
52
+ s.authors = ["Marcos Tapajós", "Celestino Gomes", "Andre Kupkosvki", "Vinícius Teles"]
54
53
  s.email = "tapajos@gmail.com"
55
54
  s.rubyforge_project = "brdinheiro"
56
55
  s.homepage = "http://www.improveit.com.br/software_livre/brazilian_rails"
57
56
 
58
57
  s.add_dependency('actionpack', '>= 1.4.2')
59
58
  s.add_dependency('activerecord', '>= 1.15.3')
59
+ s.add_dependency('brnumeros', '>= 0.0.1')
60
60
 
61
61
  s.has_rdoc = true
62
- s.requirements << 'none'
62
+ s.requirements << 'brnumeros'
63
63
  s.require_path = 'lib'
64
- # s.autorequire = 'brdinheiro'
64
+ #s.autorequire = 'brnumeros'
65
65
 
66
- s.files = [ "Rakefile", "README", "CHANGELOG", "TODO", "MIT-LICENSE" ]
66
+ s.files = [ "Rakefile", "README", "CHANGELOG", "MIT-LICENSE" ]
67
67
  s.files = s.files + Dir.glob( "lib/**/*" ).delete_if { |item| item.include?( "\.svn" ) }
68
68
  s.files = s.files + Dir.glob( "test/**/*" ).delete_if { |item| item.include?( "\.svn" ) }
69
69
  end
@@ -1,57 +1,4 @@
1
- # == Como usar o Dinheiro em seu ActiveRecord?
2
- #
3
- # * Arquivo 001_create_lancamentos.rb:
4
- #
5
- # class CreateLancamentos < ActiveRecord::Migration
6
- # def self.up
7
- # create_table :lancamentos do |t|
8
- # t.column :descricao, :string, :null => false
9
- # t.column :valor, :decimal, :precision => 14, :scale => 2
10
- # t.column :mensalidade, :decimal, :precision => 14, :scale => 2
11
- # end
12
- # end
13
- #
14
- # def self.down
15
- # drop_table :lancamentos
16
- # end
17
- # end
18
- #
19
- # * Arquivo lancamento.rb:
20
- #
21
- # class Lancamento < ActiveRecord::Base
22
- # usar_como_dinheiro :valor, :mensalidade
23
- # end
24
- #
25
- # * No console (script/console):
26
- #
27
- # Loading development environment.
28
- # >> lancamento = Lancamento.new
29
- # => #<Lancamento:0x9652cd8 @attributes={"descricao"=>nil,
30
- # "valor"=>#<BigDecimal:9657008,'0.0',4(4)>,
31
- # "mensalidade"=>#<BigDecimal:9656e8c,'0.0',4(4)>},
32
- # @new_record=true>
33
- # >> lancamento.valor = 100
34
- # => 100
35
- # >> lancamento.valor
36
- # => #<Dinheiro:0x9650f3c @quantia=10000>
37
- # >> lancamento.valor.real
38
- # => "R$ 100,00"
39
- # >> lancamento.valor = 100.50
40
- # => 100.5
41
- # >> lancamento.valor.real
42
- # => "R$ 100,50"
43
- # >> lancamento.valor = "250.50"
44
- # => "250.50"
45
- # >> lancamento.valor.real
46
- # => "R$ 250,50"
47
- # >> lancamento.valor = 354.58.reais
48
- # => #<Dinheiro:0x9646384 @quantia=35458>
49
- # >> lancamento.valor.real
50
- # => "R$ 354,58"
51
- # >> exit
52
- #
53
1
  class Dinheiro
54
-
55
2
  include Comparable
56
3
 
57
4
  attr_reader :quantia
@@ -151,17 +98,11 @@ class Dinheiro
151
98
  # 6.reais.parcelar(2.reais) == DisivaPorNaoEscalarError
152
99
  # 6.reais.parcelar(0) == ZeroDivisionError
153
100
  def parcelar(numero_de_parcelar)
154
- Fixnum
155
101
  raise DivisaPorNaoEscalarError unless numero_de_parcelar.kind_of?(Integer)
156
- return @quantia/numero_de_parcelar if numero_de_parcelar == 0
157
- soma_parcial = Dinheiro.new(0)
158
- parcelas = []
159
- (numero_de_parcelar-1).times do
160
- parcela = Dinheiro.new(transforma_em_string_que_represente_a_quantia(@quantia/numero_de_parcelar))
161
- parcelas << parcela
162
- soma_parcial += parcela
163
- end
164
- parcelas << Dinheiro.new(transforma_em_string_que_represente_a_quantia(@quantia - quantia_de(soma_parcial)))
102
+ resto = @quantia % numero_de_parcelar
103
+ valor_menor = Dinheiro.new((@quantia/numero_de_parcelar)/100.0)
104
+ valor_maior = Dinheiro.new((@quantia/numero_de_parcelar+1)/100.0)
105
+ [valor_menor] * (numero_de_parcelar - resto) + [valor_maior] * resto
165
106
  end
166
107
 
167
108
  # Escreve o valor por extenso.
@@ -237,11 +178,7 @@ class Dinheiro
237
178
  end
238
179
  end
239
180
 
240
- # Retorna um BigDecinal.
241
- def valor_decimal
242
- BigDecimal.new quantia_sem_separacao_milhares.gsub(',','.')
243
- end
244
-
181
+ # Method missing para retorna um BigDecinal quando chamada .
245
182
  def method_missing(symbol, *args) #:nodoc:
246
183
  #Ex: Chama ao método valor_decimal()
247
184
  if (symbol.to_s =~ /^(.*)_decimal$/) && args.size == 0
@@ -41,3 +41,5 @@ module DinheiroUtil
41
41
  end
42
42
  end
43
43
 
44
+ Numeric.send(:include, DinheiroUtil)
45
+ String.send(:include, DinheiroUtil)
@@ -4,4 +4,3 @@ end
4
4
 
5
5
  cria_excecao("DinheiroInvalidoError < ArgumentError", "O valor deve estar preenchido e no formato correto. Ex.: 100.00 .")
6
6
  cria_excecao("DivisaPorNaoEscalarError < ArgumentError", "So eh possivel dividir dinheiro por numeros.")
7
-
@@ -0,0 +1,24 @@
1
+ class NilClass
2
+
3
+ def valor
4
+ 0.real
5
+ end
6
+
7
+ def contabil
8
+ 0.contabil
9
+ end
10
+
11
+ def para_dinheiro
12
+ 0.real
13
+ end
14
+
15
+ def reais
16
+ 0.real
17
+ end
18
+
19
+ def real
20
+ 0.real
21
+ end
22
+
23
+ end
24
+
@@ -1,8 +1,8 @@
1
1
  module BrDinheiro
2
2
  module VERSION #:nodoc:
3
- MAJOR = 0
3
+ MAJOR = 2
4
4
  MINOR = 0
5
- TINY = 1
5
+ TINY = 0
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
data/lib/brdinheiro.rb CHANGED
@@ -5,13 +5,13 @@ $:.unshift(File.dirname(__FILE__)) unless
5
5
  dinheiro_util
6
6
  dinheiro_active_record
7
7
  excecoes
8
- number_portuguese).each {|req| require File.dirname(__FILE__) + "/brdinheiro/#{req}"}
8
+ nil_class).each {|req| require File.dirname(__FILE__) + "/brdinheiro/#{req}"}
9
9
 
10
-
11
- require 'bigdecimal'
12
- require 'rubygems'
13
- require 'active_record'
14
- require 'activesupport'
10
+ %w(bigdecimal
11
+ rubygems
12
+ active_record
13
+ activesupport
14
+ brnumeros).each {|req| require req }
15
15
 
16
16
  String.send(:include, DinheiroUtil)
17
17
  ActiveRecord::Base.send :include, DinheiroActiveRecord
@@ -0,0 +1,32 @@
1
+ require 'active_record'
2
+
3
+ module ActiveRecord
4
+ class BaseWithoutTable < Base
5
+ self.abstract_class = true
6
+
7
+ def create_or_update
8
+ errors.empty?
9
+ end
10
+
11
+ def save
12
+ self.valid?
13
+ end
14
+
15
+ class << self
16
+ def columns()
17
+ @columns ||= []
18
+ end
19
+
20
+ def column(name, sql_type = nil, default = nil, null = true)
21
+ columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
22
+ reset_column_information
23
+ end
24
+
25
+ # Do not reset @columns
26
+ def reset_column_information
27
+ generated_methods.each { |name| undef_method(name) }
28
+ @column_names = @columns_hash = @content_columns = @dynamic_methods_hash = @read_methods = nil
29
+ end
30
+ end
31
+ end
32
+ end
@@ -1,11 +1,11 @@
1
- require File.dirname(__FILE__) + '/test_helper.rb'
2
-
3
- class TestBrDinheiro < Test::Unit::TestCase
4
-
5
- def setup
6
- end
7
-
8
- def test_truth
9
- assert true
10
- end
11
- end
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestBrDinheiro < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def test_truth
9
+ assert true
10
+ end
11
+ end
@@ -1,49 +1,49 @@
1
- # require File.dirname(__FILE__) + '/test_helper'
2
- #
3
- # class Carteira < ActiveRecord::Base
4
- #
5
- # usar_como_dinheiro :saldo
6
- #
7
- # end
8
- #
9
- # class DinheiroActiveRecordTest < Test::Unit::TestCase
10
- #
11
- # def setup
12
- # @carteira = Carteira.new
13
- # end
14
- #
15
- # def teste_se_aceita_dinheiro
16
- # @carteira.saldo = 8.reais
17
- # assert @carteira.save
18
- # assert_equal 8.reais, @carteira.saldo
19
- # end
20
- #
21
- # def teste_se_aceita_numero
22
- # @carteira.saldo = 30
23
- # assert @carteira.save
24
- # assert_equal 30.reais, @carteira.saldo
25
- # end
26
- #
27
- # def teste_se_rejeita_valor_invalido
28
- # @carteira.saldo = 30
29
- # assert @carteira.save
30
- # @carteira.saldo = 'bla'
31
- # assert !@carteira.save
32
- # assert_equal "O valor deve estar preenchido e no formato correto. Ex.: 100.00 .", @carteira.errors['saldo']
33
- # end
34
- #
35
- # def teste_se_trata_nulo_corretamente
36
- # assert_nil @carteira.saldo
37
- # @carteira.saldo = nil
38
- # assert_nil @carteira.saldo
39
- # @carteira.save
40
- # assert_nil @carteira.saldo
41
- # end
42
- #
43
- # def test_se_cria_carteira_corretamente_quando_recebe_parametros
44
- # carteira = Carteira.new(:saldo => "1")
45
- # assert_equal 1.real, carteira.saldo
46
- # end
47
- #
48
- #
49
- # end
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+ require File.dirname(__FILE__) + '/active_record/base_without_table'
3
+
4
+ class Carteira < ActiveRecord::BaseWithoutTable
5
+ column :saldo, :decimal
6
+
7
+ usar_como_dinheiro :saldo
8
+ end
9
+
10
+ class DinheiroActiveRecordTest < Test::Unit::TestCase
11
+
12
+ def setup
13
+ @carteira = Carteira.new
14
+ end
15
+
16
+ def teste_se_aceita_dinheiro
17
+ @carteira.saldo = 8.reais
18
+ assert @carteira.save
19
+ assert_equal 8.reais, @carteira.saldo
20
+ end
21
+
22
+ def teste_se_aceita_numero
23
+ @carteira.saldo = 30
24
+ assert @carteira.save
25
+ assert_equal 30.reais, @carteira.saldo
26
+ end
27
+
28
+ def teste_se_rejeita_valor_invalido
29
+ @carteira.saldo = 30
30
+ assert @carteira.save
31
+ @carteira.saldo = 'bla'
32
+ assert_false @carteira.save
33
+ assert_equal "O valor deve estar preenchido e no formato correto. Ex.: 100.00 .", @carteira.errors['saldo']
34
+ end
35
+
36
+ def teste_se_trata_nulo_corretamente
37
+ assert_nil @carteira.saldo
38
+ @carteira.saldo = nil
39
+ assert_nil @carteira.saldo
40
+ @carteira.save
41
+ assert_nil @carteira.saldo
42
+ end
43
+
44
+ def test_se_cria_carteira_corretamente_quando_recebe_parametros
45
+ carteira = Carteira.new(:saldo => "1")
46
+ assert_equal 1.real, carteira.saldo
47
+ end
48
+
49
+ end
@@ -91,9 +91,9 @@ class DinheiroTest < Test::Unit::TestCase
91
91
  [Dinheiro.new(33.33), Dinheiro.new(33.33), Dinheiro.new(33.34)] => Dinheiro.new(100).parcelar(3),
92
92
  [Dinheiro.new(50.00), Dinheiro.new(50)] => Dinheiro.new(100).parcelar(2),
93
93
  [Dinheiro.new(0.25), Dinheiro.new(0.25)] => Dinheiro.new(0.5).parcelar(2),
94
- [Dinheiro.new(0.16), Dinheiro.new(0.16),Dinheiro.new(0.18)] => Dinheiro.new(0.5).parcelar(3),
95
- [Dinheiro.new(0.33)] => Dinheiro.new(0.33).parcelar(1) ,
96
- [Dinheiro.new(0.33)] => Dinheiro.new(0.33).parcelar(1) ,
94
+ [Dinheiro.new(0.16), Dinheiro.new(0.17),Dinheiro.new(0.17)] => Dinheiro.new(0.5).parcelar(3),
95
+ [Dinheiro.new(0.33)] => Dinheiro.new(0.33).parcelar(1),
96
+ [Dinheiro.new(0.33)] => Dinheiro.new(0.33).parcelar(1),
97
97
  }
98
98
 
99
99
 
@@ -0,0 +1,29 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class NilClassTest < Test::Unit::TestCase
4
+
5
+ def test_real
6
+ assert_equal Dinheiro.new(0), nil.real
7
+ end
8
+
9
+ def test_reais
10
+ assert_equal Dinheiro.new(0), nil.reais
11
+ end
12
+
13
+ def test_para_dinheiro
14
+ assert_equal Dinheiro.new(0), nil.para_dinheiro
15
+ end
16
+
17
+ def test_valor
18
+ assert_equal Dinheiro.new(0), nil.valor
19
+ end
20
+
21
+ def test_contabil
22
+ assert_equal "0,00", nil.contabil
23
+ end
24
+
25
+
26
+
27
+
28
+
29
+ end
data/test/test_helper.rb CHANGED
@@ -14,3 +14,12 @@ def p80 text
14
14
  p text
15
15
  p '*'*80
16
16
  end
17
+
18
+ class Test::Unit::TestCase
19
+ def assert_false(boolean, message=nil)
20
+ _wrap_assertion do
21
+ assert_block("assert should not be called with a block.") { !block_given? }
22
+ assert_block(build_message(message, "<?> is not false.", boolean)) { !boolean }
23
+ end
24
+ end
25
+ end
metadata CHANGED
@@ -1,19 +1,23 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brdinheiro
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Marcos Tapaj\xC3\xB3s"
8
+ - Celestino Gomes
9
+ - Andre Kupkosvki
10
+ - "Vin\xC3\xADcius Teles"
8
11
  autorequire:
9
12
  bindir: bin
10
13
  cert_chain: []
11
14
 
12
- date: 2008-06-22 00:00:00 -03:00
15
+ date: 2008-08-31 00:00:00 -03:00
13
16
  default_executable:
14
17
  dependencies:
15
18
  - !ruby/object:Gem::Dependency
16
19
  name: actionpack
20
+ type: :runtime
17
21
  version_requirement:
18
22
  version_requirements: !ruby/object:Gem::Requirement
19
23
  requirements:
@@ -23,6 +27,7 @@ dependencies:
23
27
  version:
24
28
  - !ruby/object:Gem::Dependency
25
29
  name: activerecord
30
+ type: :runtime
26
31
  version_requirement:
27
32
  version_requirements: !ruby/object:Gem::Requirement
28
33
  requirements:
@@ -30,6 +35,16 @@ dependencies:
30
35
  - !ruby/object:Gem::Version
31
36
  version: 1.15.3
32
37
  version:
38
+ - !ruby/object:Gem::Dependency
39
+ name: brnumeros
40
+ type: :runtime
41
+ version_requirement:
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 0.0.1
47
+ version:
33
48
  description: "brdinheiro \xC3\xA9 uma das gems que compoem o Brazilian Rails"
34
49
  email: tapajos@gmail.com
35
50
  executables: []
@@ -42,19 +57,21 @@ files:
42
57
  - Rakefile
43
58
  - README
44
59
  - CHANGELOG
45
- - TODO
46
60
  - MIT-LICENSE
47
61
  - lib/brdinheiro
48
62
  - lib/brdinheiro/dinheiro.rb
49
63
  - lib/brdinheiro/dinheiro_active_record.rb
50
64
  - lib/brdinheiro/dinheiro_util.rb
51
65
  - lib/brdinheiro/excecoes.rb
52
- - lib/brdinheiro/number_portuguese.rb
66
+ - lib/brdinheiro/nil_class.rb
53
67
  - lib/brdinheiro/version.rb
54
68
  - lib/brdinheiro.rb
69
+ - test/active_record
70
+ - test/active_record/base_without_table.rb
55
71
  - test/br_dinheiro_test.rb
56
72
  - test/dinheiro_active_record_test.rb
57
73
  - test/dinheiro_test.rb
74
+ - test/nil_class_test.rb
58
75
  - test/test_helper.rb
59
76
  has_rdoc: true
60
77
  homepage: http://www.improveit.com.br/software_livre/brazilian_rails
@@ -76,9 +93,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
93
  version: "0"
77
94
  version:
78
95
  requirements:
79
- - none
96
+ - brnumeros
80
97
  rubyforge_project: brdinheiro
81
- rubygems_version: 1.1.1
98
+ rubygems_version: 1.2.0
82
99
  signing_key:
83
100
  specification_version: 2
84
101
  summary: "brdinheiro \xC3\xA9 uma das gems que compoem o Brazilian Rails"
data/TODO DELETED
@@ -1,5 +0,0 @@
1
- # gem brdinheiro
2
-
3
- ## testes do dinheiro_active_record
4
- ## Ajustar o README
5
- ## Verificar como regrar a documentação.
@@ -1,163 +0,0 @@
1
- module Extenso
2
- @@unidade = {
3
- 0 => "zero",
4
- 1 => "um",
5
- 2 => "dois",
6
- 3 => "três",
7
- 4 => "quatro",
8
- 5 => "cinco",
9
- 6 => "seis",
10
- 7 => "sete",
11
- 8 => "oito",
12
- 9 => "nove"
13
- }
14
- @@dezena = {
15
- 10 => "dez",
16
- 11 => "onze",
17
- 12 => "doze",
18
- 13 => "treze",
19
- 14 => "quatorze",
20
- 15 => "quinze",
21
- 16 => "dezesseis",
22
- 17 => "dezessete",
23
- 18 => "dezoito",
24
- 19 => "dezenove",
25
- 20 => "vinte",
26
- 30 => "trinta",
27
- 40 => "quarenta",
28
- 50 => "cinquenta",
29
- 60 => "sessenta",
30
- 70 => "setenta",
31
- 80 => "oitenta",
32
- 90 => "noventa" }
33
- @@centena = {100 => "cento",
34
- 200 => "duzentos",
35
- 300 => "trezentos",
36
- 400 => "quatrocentos",
37
- 500 => "quinhentos",
38
- 600 => "seiscentos",
39
- 700 => "setecentos",
40
- 800 => "oitocentos",
41
- 900 => "novecentos" }
42
-
43
- # Escreve o numero por extenso.
44
- #
45
- # Exemplo:
46
- # 1.por_extenso ==> 'um'
47
- # 100.por_extenso ==> 'cem'
48
- # 158.por_extenso ==> 'cento e cinquenta e oito'
49
- def por_extenso
50
- Extenso.por_extenso(self)
51
- end
52
-
53
- # Alias para o metodo por_extenso
54
- alias_method :to_extenso, :por_extenso
55
-
56
- # Escreve o numero por extenso.
57
- #
58
- # Exemplo:
59
- # Extenso.por_extenso(1) ==> "um"
60
- # Extenso.por_extenso(100) ==> "cem"
61
- # Extenso.por_extenso(158) ==> "cento e cinquenta e oito"
62
- def Extenso.por_extenso(numero)
63
- negativo=(numero<0)?"menos ":""
64
- n=numero.to_i.abs
65
- return case n
66
- when 0..9: negativo + @@unidade[n].to_s
67
- when 10..19: negativo + @@dezena[n].to_s
68
- when 20..99:
69
- v=n % 10
70
- if v== 0
71
- negativo + @@dezena[n].to_s
72
- else
73
- "#{negativo}#{@@dezena[n-v]} e #{por_extenso(v)}"
74
- end
75
- when 100
76
- negativo+"cem"
77
- when 101..999
78
- v=n % 100
79
- if v== 0
80
- negativo + @@centena[n].to_s
81
- else
82
- "#{negativo}#{@@centena[n-v]} e #{por_extenso(v)}"
83
- end
84
- when 1000..999999
85
- m,c=n/1000,n%1000
86
- %(#{negativo}#{por_extenso(m)} mil#{c > 0 ? " e #{por_extenso(c)}":''})
87
- when 1_000_000..999_999_999
88
- mi,m=n/1_000_000,n%1_000_000
89
- %(#{negativo}#{por_extenso(mi)} milh#{mi > 1 ? 'ões':'ão'}#{m > 0 ? " e #{por_extenso(m)}" : ''})
90
- when 1_000_000_000..999_999_999_999
91
- bi,mi=n/1_000_000_000,n%1_000_000_000
92
- %(#{negativo}#{por_extenso(bi)} bilh#{bi > 1 ? 'ões':'ão'}#{mi > 0 ? " e #{por_extenso(mi)}" : ''})
93
- when 1_000_000_000_000..999_999_999_999_999
94
- tri,bi=n/1_000_000_000_000,n%1_000_000_000_000
95
- %(#{negativo}#{por_extenso(tri)} trilh#{tri > 1 ? 'ões':'ão'}#{bi > 0 ? " e #{por_extenso(bi)}" : ''})
96
- else
97
- raise "Valor excede o permitido: #{n}"
98
- end
99
- end
100
- end
101
-
102
- module ExtensoReal
103
- include Extenso
104
-
105
- # Escreve por extenso em reais.
106
- #
107
- # Exemplo:
108
- # 1.por_extenso_em_reais ==> 'um real'
109
- # (100.58).por_extenso_em_reais ==> 'cem reais e cinquenta e oito centavos'
110
- def por_extenso_em_reais
111
- ExtensoReal.por_extenso_em_reais(self)
112
- end
113
-
114
- # Alias para por_extenso_em_reais
115
- alias_method :to_extenso_real, :por_extenso_em_reais
116
-
117
- # Escreve o numero por extenso em reais.
118
- #
119
- # Exemplo:
120
- # Extenso.por_extenso_em_reais(1) ==> "um real"
121
- # Extenso.por_extenso_em_reais(100) ==> "cem reais"
122
- # Extenso.por_extenso_em_reais(100.58) ==> "cem reais e cinquenta e oito centavos"
123
- def ExtensoReal.por_extenso_em_reais(valor)
124
- negativo=(valor<0)?" negativo":""
125
- negativos=(valor<0)?" negativos":""
126
- valor = valor.abs
127
- return 'grátis' if valor == 0
128
- case valor
129
- when Integer
130
- extenso = Extenso.por_extenso(valor)
131
- if extenso =~ /^(.*)(ão$|ões$)/
132
- complemento = 'de '
133
- else
134
- complemento = ''
135
- end
136
- %(#{extenso} #{valor <= 1 ? "real#{negativo}": "#{complemento}reais#{negativos}"})
137
- when Float
138
- real,cents=("%.2f" % valor).split(/\./).map{ |m| m.to_i}
139
- valor_cents=Extenso.por_extenso(cents%100)
140
- valor_cents+= case cents.to_i%100
141
- when 0: ""
142
- when 1: " centavo"
143
- when 2..99: " centavos"
144
- end
145
-
146
- if real.to_i > 0
147
- "#{ExtensoReal.por_extenso_em_reais(real.to_i)}#{cents > 0 ? ' e ' + valor_cents + negativos : real.to_i > 1 ? negativos : negativo }"
148
- else
149
- if (cents.to_i%100)==1
150
- "#{valor_cents}#{negativo}"
151
- else
152
- "#{valor_cents}#{negativos}"
153
- end
154
- end
155
- else
156
- ExtensoReal.por_extenso_em_reais(valor.to_s.strip.gsub(/[^\d]/,'.').to_f)
157
- end
158
- end
159
- end
160
-
161
- Numeric.send(:include, DinheiroUtil)
162
- Numeric.send(:include, ExtensoReal)
163
-