Class: Money

Inherits:
Object
  • Object
show all
Defined in:
lib/wahrung/money.rb,
lib/wahrung/arithmetic.rb,
lib/wahrung/comparison.rb

Overview

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amount, currency = Money.configuration.default_currency) ⇒ Money

Returns a new instance of Money.



24
25
26
27
# File 'lib/wahrung/money.rb', line 24

def initialize(amount, currency = Money.configuration.default_currency)
  @amount   = amount
  @currency = currency
end

Class Attribute Details

.configurationObject

rubocop:disable Lint/DuplicateMethods



12
13
14
# File 'lib/wahrung/money.rb', line 12

def configuration
  @configuration
end

Instance Attribute Details

#amountObject

Returns the value of attribute amount.



5
6
7
# File 'lib/wahrung/money.rb', line 5

def amount
  @amount
end

#currencyObject

Returns the value of attribute currency.



5
6
7
# File 'lib/wahrung/money.rb', line 5

def currency
  @currency
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

Yields:



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

def self.configure
  yield configuration
end

.resetObject



20
21
22
# File 'lib/wahrung/money.rb', line 20

def self.reset
  @configuration = Configuration.new
end

Instance Method Details

#*(other) ⇒ Object



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

def *(other)
  formatted_result @amount * other
end

#+(other) ⇒ Object



3
4
5
# File 'lib/wahrung/arithmetic.rb', line 3

def +(other)
  formatted_result @amount + equalized_currency(other)
end

#-(other) ⇒ Object



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

def -(other)
  formatted_result @amount - equalized_currency(other)
end

#/(other) ⇒ Object



11
12
13
14
# File 'lib/wahrung/arithmetic.rb', line 11

def /(other)
  return "0 #{@currency}" if other.zero?
  formatted_result @amount / other
end

#<(other) ⇒ Object



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

def <(other)
  @amount < other.amount
end

#==(other) ⇒ Object



3
4
5
# File 'lib/wahrung/comparison.rb', line 3

def ==(other)
  @currency == other.currency && @amount == other.amount
end

#>(other) ⇒ Object



11
12
13
# File 'lib/wahrung/comparison.rb', line 11

def >(other)
  @amount > other.amount
end

#convert_to(currency) ⇒ Object



33
34
35
36
37
# File 'lib/wahrung/money.rb', line 33

def convert_to(currency)
  @amount   = format_result((@amount * conversion(currency)).to_f)
  @currency = currency
  self
end

#inspectObject



29
30
31
# File 'lib/wahrung/money.rb', line 29

def inspect
  formatted_result
end