Class: Dinheiro
- Inherits:
-
Object
- Object
- Dinheiro
- Includes:
- Comparable
- Defined in:
- lib/brdinheiro/dinheiro.rb
Constant Summary collapse
- FORMATO_VALIDO_BR =
/^([R|r]\$\s*)?(([+-]?\d{1,3}(\.?\d{3})*))?(\,\d{0,2})?$/
- FORMATO_VALIDO_EUA =
/^([R|r]\$\s*)?(([+-]?\d{1,3}(\,?\d{3})*))?(\.\d{0,2})?$/
- SEPARADOR_MILHAR =
"."
- SEPARADOR_FRACIONARIO =
","
- QUANTIDADE_DIGITOS =
3
- PRECISAO_DECIMAL =
100
Instance Attribute Summary collapse
-
#quantia ⇒ Object
readonly
Returns the value of attribute quantia.
Instance Method Summary collapse
-
#*(outro) ⇒ Object
Retorna a multiplicacao entre dinheiros.
-
#+(outro) ⇒ Object
Retorna a adicao entre dinheiros.
-
#-(outro) ⇒ Object
Retorna a subtracao entre dinheiros.
-
#/(outro) ⇒ Object
Retorna a divisao entre dinheiros.
-
#<=>(outro_dinheiro) ⇒ Object
Compara com outro dinheiro se eh maior ou menor.
-
#==(outro_dinheiro) ⇒ Object
Compara com outro dinheiro se eh igual.
-
#coerce(outro) ⇒ Object
:nodoc:.
-
#contabil ⇒ Object
Retorna uma string formatada sem sigla.
-
#initialize(quantia) ⇒ Dinheiro
constructor
A new instance of Dinheiro.
-
#method_missing(symbol, *args) ⇒ Object
Method missing para retorna um BigDecinal quando chamada .
-
#parcelar(numero_de_parcelar) ⇒ Object
Retorna um array de dinheiro com as parcelas.
-
#real ⇒ Object
(also: #reais)
Retorna a própria instância/.
-
#real_contabil ⇒ Object
(also: #reais_contabeis)
Retorna uma string formatada com sigla em valor contábil.
-
#real_formatado ⇒ Object
(also: #reais_formatado)
Retorna uma string formatada com sigla em valor monetário.
-
#to_extenso ⇒ Object
(also: #por_extenso, #por_extenso_em_reais)
Escreve o valor por extenso.
-
#to_f ⇒ Object
Retorna um Float.
-
#to_s ⇒ Object
Retorna o valor armazenado em string.
-
#zero? ⇒ Boolean
Verifica se o valor é zero.
Constructor Details
#initialize(quantia) ⇒ Dinheiro
Returns a new instance of Dinheiro.
13 14 15 |
# File 'lib/brdinheiro/dinheiro.rb', line 13 def initialize(quantia) self.quantia = quantia end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args) ⇒ Object
Method missing para retorna um BigDecinal quando chamada .
182 183 184 185 186 187 188 189 |
# File 'lib/brdinheiro/dinheiro.rb', line 182 def method_missing(symbol, *args) #:nodoc: #Ex: Chama ao método valor_decimal() if (symbol.to_s =~ /^(.*)_decimal$/) && args.size == 0 BigDecimal.new quantia_sem_separacao_milhares.gsub(',','.') else super.method_missing(symbol, *args) end end |
Instance Attribute Details
#quantia ⇒ Object
Returns the value of attribute quantia.
4 5 6 |
# File 'lib/brdinheiro/dinheiro.rb', line 4 def quantia @quantia end |
Instance Method Details
#*(outro) ⇒ Object
Retorna a multiplicacao entre dinheiros.
Exemplo:
5.reais * 2 == 10.reais
5.reais * 2.reais == 10.reais
75 76 77 78 |
# File 'lib/brdinheiro/dinheiro.rb', line 75 def *(outro) return Dinheiro.new(to_f * outro) unless outro.kind_of? Dinheiro outro * to_f end |
#+(outro) ⇒ Object
Retorna a adicao entre dinheiros.
Exemplo:
1.real + 1.real == 2.reais
1.real + 1 == 2.reais
57 58 59 |
# File 'lib/brdinheiro/dinheiro.rb', line 57 def +(outro) Dinheiro.new(transforma_em_string_que_represente_a_quantia(@quantia + quantia_de(outro))) end |
#-(outro) ⇒ Object
Retorna a subtracao entre dinheiros.
Exemplo:
10.reais - 2.reais == 8.reais
10.reais - 2 == 8.reais
66 67 68 |
# File 'lib/brdinheiro/dinheiro.rb', line 66 def -(outro) Dinheiro.new(transforma_em_string_que_represente_a_quantia(@quantia - quantia_de(outro))) end |
#/(outro) ⇒ Object
Retorna a divisao entre dinheiros.
Exemplo:
5.reais / 2 == (2.5).reais
5.reais / 2.reais == DivisaPorNaoEscalarError
5.reais / 0 == ZeroDivisionError
Veja também o método parcelar
88 89 90 91 92 |
# File 'lib/brdinheiro/dinheiro.rb', line 88 def /(outro) raise DivisaPorNaoEscalarError unless outro.kind_of?(Numeric) return @quantia/outro if outro == 0 Dinheiro.new(to_f / outro.to_f) end |
#<=>(outro_dinheiro) ⇒ Object
Compara com outro dinheiro se eh maior ou menor.
Exemplo:
1.real < 2.reais ==> true
1.real > 2.reais ==> false
2.real < 1.reais ==> false
2.real > 1.reais ==> true
47 48 49 50 |
# File 'lib/brdinheiro/dinheiro.rb', line 47 def <=>(outro_dinheiro) outro_dinheiro = Dinheiro.new(outro_dinheiro) unless outro_dinheiro.kind_of?(Dinheiro) @quantia <=> outro_dinheiro.quantia end |
#==(outro_dinheiro) ⇒ Object
Compara com outro dinheiro se eh igual.
Exemplo:
um_real = Dinheiro.new(1)
um_real == Dinheiro.new(1) ==> true
um_real == Dinheiro.new(2) ==> false
31 32 33 34 35 36 37 38 |
# File 'lib/brdinheiro/dinheiro.rb', line 31 def ==(outro_dinheiro) begin outro_dinheiro = Dinheiro.new(outro_dinheiro) unless outro_dinheiro.kind_of?(Dinheiro) rescue return false end @quantia == outro_dinheiro.quantia end |
#coerce(outro) ⇒ Object
:nodoc:
133 134 135 |
# File 'lib/brdinheiro/dinheiro.rb', line 133 def coerce(outro)#:nodoc: [ Dinheiro.new(outro), self ] end |
#contabil ⇒ Object
Retorna uma string formatada sem sigla.
Exemplo:
Dinheiro.new(1).contabil ==> '1,00'
Dinheiro.new(-1).contabil ==> '(1,00)'
173 174 175 176 177 178 179 |
# File 'lib/brdinheiro/dinheiro.rb', line 173 def contabil if @quantia >= 0 to_s else "(" + to_s[1..-1] + ")" end end |
#parcelar(numero_de_parcelar) ⇒ Object
Retorna um array de dinheiro com as parcelas
Exemplo:
6.reais.parcelar(2) == [3.reais, 3.reais]
6.reais.parcelar(2.reais) == DisivaPorNaoEscalarError
6.reais.parcelar(0) == ZeroDivisionError
100 101 102 103 104 105 106 |
# File 'lib/brdinheiro/dinheiro.rb', line 100 def parcelar(numero_de_parcelar) raise DivisaPorNaoEscalarError unless numero_de_parcelar.kind_of?(Integer) resto = @quantia % numero_de_parcelar valor_menor = Dinheiro.new((@quantia/numero_de_parcelar)/100.0) valor_maior = Dinheiro.new((@quantia/numero_de_parcelar+1)/100.0) [valor_menor] * (numero_de_parcelar - resto) + [valor_maior] * resto end |
#real ⇒ Object Also known as: reais
Retorna a própria instância/
138 139 140 |
# File 'lib/brdinheiro/dinheiro.rb', line 138 def real self end |
#real_contabil ⇒ Object Also known as: reais_contabeis
Retorna uma string formatada com sigla em valor contábil.
Exemplo:
Dinheiro.new(1).real_contabil ==> 'R$ 1,00'
Dinheiro.new(-1).real_contabil ==> 'R$ (1,00)'
161 162 163 |
# File 'lib/brdinheiro/dinheiro.rb', line 161 def real_contabil "R$ " + contabil end |
#real_formatado ⇒ Object Also known as: reais_formatado
Retorna uma string formatada com sigla em valor monetário. Exemplo:
Dinheiro.new(1).real_formatado ==> 'R$ 1,00'
Dinheiro.new(-1).real_formatado ==> 'R$ -1,00'
149 150 151 |
# File 'lib/brdinheiro/dinheiro.rb', line 149 def real_formatado "R$ #{to_s}" end |
#to_extenso ⇒ Object Also known as: por_extenso, por_extenso_em_reais
Escreve o valor por extenso.
Exemplo:
1.real.to_extenso ==> 'um real'
(100.58).to_extenso ==> 'cem reais e cinquenta e oito centavos'
113 114 115 |
# File 'lib/brdinheiro/dinheiro.rb', line 113 def to_extenso (@quantia/100.0).por_extenso_em_reais end |
#to_f ⇒ Object
Retorna um Float.
129 130 131 |
# File 'lib/brdinheiro/dinheiro.rb', line 129 def to_f to_s.gsub('.', '').gsub(',', '.').to_f end |
#to_s ⇒ Object
Retorna o valor armazenado em string.
Exemplo:
1000.to_s ==> '1.000,00'
21 22 23 |
# File 'lib/brdinheiro/dinheiro.rb', line 21 def to_s inteiro_com_milhar(parte_inteira) + parte_decimal end |
#zero? ⇒ Boolean
Verifica se o valor é zero.
124 125 126 |
# File 'lib/brdinheiro/dinheiro.rb', line 124 def zero? to_f.zero? end |