Class: Hundred

Inherits:
Object
  • Object
show all
Defined in:
lib/hundred.rb,
lib/hundred/version.rb

Constant Summary collapse

MoneyError =
Class.new(StandardError)
DEFAULT_CURRENCY =
'EUR'
DEFAULT_PRECISION =
2
VERSION =
"0.1.1"
@@rates =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amount, currency = DEFAULT_CURRENCY) ⇒ Hundred

Returns a new instance of Hundred.



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

def initialize(amount, currency = DEFAULT_CURRENCY)
  @amount = amount
  @currency = currency
end

Instance Attribute Details

#amountObject

Returns the value of attribute amount.



19
20
21
# File 'lib/hundred.rb', line 19

def amount
  @amount
end

#currencyObject

Returns the value of attribute currency.



19
20
21
# File 'lib/hundred.rb', line 19

def currency
  @currency
end

Class Method Details

.conversion_rates(currency, rates) ⇒ Object



21
22
23
# File 'lib/hundred.rb', line 21

def self.conversion_rates(currency, rates)
  @@rates.merge!(currency => rates)
end

Instance Method Details

#*(other) ⇒ Object



63
64
65
66
67
# File 'lib/hundred.rb', line 63

def *(other)
  new_amount = amount * other

  self.class.new(new_amount, currency)
end

#+(other) ⇒ Object



49
50
51
52
53
54
# File 'lib/hundred.rb', line 49

def +(other)
  new_amount = with_precision(convert_to(DEFAULT_CURRENCY).amount) +
               with_precision(other.convert_to(DEFAULT_CURRENCY).amount)

  self.class.new(new_amount)
end

#-(other) ⇒ Object



56
57
58
59
60
61
# File 'lib/hundred.rb', line 56

def -(other)
  new_amount = with_precision(convert_to(DEFAULT_CURRENCY).amount) -
               with_precision(other.convert_to(DEFAULT_CURRENCY).amount)

  self.class.new(new_amount)
end

#/(other) ⇒ Object



69
70
71
72
73
# File 'lib/hundred.rb', line 69

def /(other)
  new_amount = amount / other

  self.class.new(new_amount, currency)
end

#<(other) ⇒ Object



80
81
82
83
# File 'lib/hundred.rb', line 80

def <(other)
  with_precision(convert_to(DEFAULT_CURRENCY).amount) <
    with_precision(other.convert_to(DEFAULT_CURRENCY).amount)
end

#==(other) ⇒ Object



44
45
46
47
# File 'lib/hundred.rb', line 44

def ==(other)
  with_precision(convert_to(DEFAULT_CURRENCY).amount) ==
    with_precision(other.convert_to(DEFAULT_CURRENCY).amount)
end

#>(other) ⇒ Object



75
76
77
78
# File 'lib/hundred.rb', line 75

def >(other)
  with_precision(convert_to(DEFAULT_CURRENCY).amount) >
    with_precision(other.convert_to(DEFAULT_CURRENCY).amount)
end

#convert_to(curr) ⇒ Object



38
39
40
41
42
# File 'lib/hundred.rb', line 38

def convert_to(curr)
  return self if currency == curr

  self.class.new(amount * @@rates[currency][curr], curr)
end

#inspectObject



30
31
32
# File 'lib/hundred.rb', line 30

def inspect
  "#{'%.2f' % amount} #{currency}"
end