Class: Dinheiro

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/dinheiro.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(quantia) ⇒ Dinheiro



16
17
18
# File 'lib/dinheiro.rb', line 16

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 .



198
199
200
201
202
203
204
205
# File 'lib/dinheiro.rb', line 198

def method_missing(symbol, *args) # :nodoc:
  # Ex: Chama ao método valor_decimal()
  if (symbol.to_s =~ /^(.*)_decimal$/) && args.empty?
    BigDecimal quantia_sem_separacao_milhares.gsub(',', '.')
  else
    super.method_missing(symbol, *args)
  end
end

Instance Attribute Details

#quantiaObject

Returns the value of attribute quantia.



7
8
9
# File 'lib/dinheiro.rb', line 7

def quantia
  @quantia
end

Instance Method Details

#*(other) ⇒ Object

Retorna a multiplicacao entre dinheiros.

Exemplo:

5.reais * 2 == 10.reais
5.reais * 2.reais == 10.reais


88
89
90
91
92
# File 'lib/dinheiro.rb', line 88

def *(other)
  return Dinheiro.new(to_f * other) unless other.is_a? Dinheiro

  other * to_f
end

#+(other) ⇒ Object

Retorna a adicao entre dinheiros.

Exemplo:

1.real + 1.real == 2.reais
1.real + 1 == 2.reais


70
71
72
# File 'lib/dinheiro.rb', line 70

def +(other)
  Dinheiro.new(transforma_em_string_que_represente_a_quantia(@quantia + quantia_de(other)))
end

#-(other) ⇒ Object

Retorna a subtracao entre dinheiros.

Exemplo:

10.reais - 2.reais == 8.reais
10.reais - 2 == 8.reais


79
80
81
# File 'lib/dinheiro.rb', line 79

def -(other)
  Dinheiro.new(transforma_em_string_que_represente_a_quantia(@quantia - quantia_de(other)))
end

#/(other) ⇒ 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

Raises:

  • (DivisaPorNaoEscalarError)


102
103
104
105
106
107
# File 'lib/dinheiro.rb', line 102

def /(other)
  raise DivisaPorNaoEscalarError unless other.is_a?(Numeric)
  return @quantia / other if other.zero?

  Dinheiro.new(to_f / other)
end

#<=>(other) ⇒ 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


60
61
62
63
# File 'lib/dinheiro.rb', line 60

def <=>(other)
  other = Dinheiro.new(other) unless other.is_a?(Dinheiro)
  @quantia <=> other.quantia
end

#==(other) ⇒ 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


44
45
46
47
48
49
50
51
# File 'lib/dinheiro.rb', line 44

def ==(other)
  begin
    other = Dinheiro.new(other) unless other.is_a?(Dinheiro)
  rescue StandardError
    return false
  end
  @quantia == other.quantia
end

#as_jsonObject

Retorna o valor em Float quando uma coleção ou objeto é convertido para JSON

Exemplo:

produto = Produto.find 1
produto.to_json // {"nome": "MacBook", "valor": 3500.0}


26
27
28
# File 'lib/dinheiro.rb', line 26

def as_json
  to_f
end

#coerce(outro) ⇒ Object

:nodoc:



149
150
151
# File 'lib/dinheiro.rb', line 149

def coerce(outro) # :nodoc:
  [Dinheiro.new(outro), self]
end

#contabilObject

Retorna uma string formatada sem sigla.

Exemplo:

Dinheiro.new(1).contabil ==> '1,00'
Dinheiro.new(-1).contabil ==> '(1,00)'


189
190
191
192
193
194
195
# File 'lib/dinheiro.rb', line 189

def contabil
  if @quantia >= 0
    to_s
  else
    "(#{to_s[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

Raises:

  • (DivisaPorNaoEscalarError)


115
116
117
118
119
120
121
122
# File 'lib/dinheiro.rb', line 115

def parcelar(numero_de_parcelar)
  raise DivisaPorNaoEscalarError unless numero_de_parcelar.is_a?(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

#realObject Also known as: reais

Retorna a própria instância/



154
155
156
# File 'lib/dinheiro.rb', line 154

def real
  self
end

#real_contabilObject 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)'


177
178
179
# File 'lib/dinheiro.rb', line 177

def real_contabil
  "R$ #{contabil}"
end

#real_formatadoObject 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'


165
166
167
# File 'lib/dinheiro.rb', line 165

def real_formatado
  "R$ #{self}"
end

#to_extensoObject 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'


129
130
131
# File 'lib/dinheiro.rb', line 129

def to_extenso
  (@quantia / 100.0).por_extenso_em_reais
end

#to_fObject

Retorna um Float.



145
146
147
# File 'lib/dinheiro.rb', line 145

def to_f
  to_s.gsub('.', '').gsub(',', '.').to_f
end

#to_sObject

Retorna o valor armazenado em string.

Exemplo:

1000.to_s ==> '1.000,00'


34
35
36
# File 'lib/dinheiro.rb', line 34

def to_s
  inteiro_com_milhar(parte_inteira) + parte_decimal
end

#zero?Boolean

Verifica se o valor é zero.



140
141
142
# File 'lib/dinheiro.rb', line 140

def zero?
  to_f.zero?
end