class Money

Arithmetical operations belonging to Money +, -, /, *.

Comparison operations belonging to Money >, <, ==.

rubocop:disable Style/Documentation

Attributes

configuration[RW]
amount[RW]
currency[RW]

Public Class Methods

configure() { |configuration| ... } click to toggle source
# File lib/wahrung/money.rb, line 16
def self.configure
  yield configuration
end
new(amount, currency = Money.configuration.default_currency) click to toggle source
# File lib/wahrung/money.rb, line 24
def initialize(amount, currency = Money.configuration.default_currency)
  @amount   = amount
  @currency = currency
end
reset() click to toggle source
# File lib/wahrung/money.rb, line 20
def self.reset
  @configuration = Configuration.new
end

Public Instance Methods

*(other) click to toggle source
# File lib/wahrung/arithmetic.rb, line 16
def *(other)
  formatted_result @amount * other
end
+(other) click to toggle source
# File lib/wahrung/arithmetic.rb, line 3
def +(other)
  formatted_result @amount + equalized_currency(other)
end
-(other) click to toggle source
# File lib/wahrung/arithmetic.rb, line 7
def -(other)
  formatted_result @amount - equalized_currency(other)
end
/(other) click to toggle source
# File lib/wahrung/arithmetic.rb, line 11
def /(other)
  return "0 #{@currency}" if other.zero?
  formatted_result @amount / other
end
<(other) click to toggle source
# File lib/wahrung/comparison.rb, line 7
def <(other)
  @amount < other.amount
end
==(other) click to toggle source
# File lib/wahrung/comparison.rb, line 3
def ==(other)
  @currency == other.currency && @amount == other.amount
end
>(other) click to toggle source
# File lib/wahrung/comparison.rb, line 11
def >(other)
  @amount > other.amount
end
convert_to(currency) click to toggle source
# File lib/wahrung/money.rb, line 33
def convert_to(currency)
  @amount   = format_result((@amount * conversion(currency)).to_f)
  @currency = currency
  self
end
inspect() click to toggle source
# File lib/wahrung/money.rb, line 29
def inspect
  formatted_result
end

Private Instance Methods

conversion(currency = @currency) click to toggle source
# File lib/wahrung/money.rb, line 61
def conversion(currency = @currency)
  Money.configuration.conversions[currency]
end
different_currencies(other) click to toggle source
# File lib/wahrung/money.rb, line 49
def different_currencies(other)
  other.currency != @currency
end
equalize_currencies(other) click to toggle source
# File lib/wahrung/money.rb, line 45
def equalize_currencies(other)
  other.amount * conversion
end
equalized_currency(other) click to toggle source
# File lib/wahrung/money.rb, line 41
def equalized_currency(other)
  different_currencies(other) ? equalize_currencies(other) : other.amount
end
format_result(amount) click to toggle source
# File lib/wahrung/money.rb, line 53
def format_result(amount)
  format '%.2f', amount
end
formatted_result(amount = @amount, currency = @currency) click to toggle source
# File lib/wahrung/money.rb, line 57
def formatted_result(amount = @amount, currency = @currency)
  "#{format_result amount} #{currency}"
end