Module: Operations

Includes:
Comparable
Included in:
Money
Defined in:
lib/smart_money/operations.rb

Instance Method Summary collapse

Instance Method Details

#*(value) ⇒ Object



24
25
26
# File 'lib/smart_money/operations.rb', line 24

def *(value)
  Money.new @amount*value, @currency if value.is_a?(Numeric)
end

#+(other) ⇒ Object

Raises:

  • (TypeError)


14
15
16
17
# File 'lib/smart_money/operations.rb', line 14

def +(other)
  raise TypeError unless other.is_a?(Money)
  Money.new @amount+other.convert_to(@currency).amount, @currency
end

#-(other) ⇒ Object

Raises:

  • (TypeError)


19
20
21
22
# File 'lib/smart_money/operations.rb', line 19

def -(other)
  raise TypeError unless other.is_a?(Money)
  Money.new @amount-other.convert_to(@currency).amount, @currency
end

#/(value) ⇒ Object



28
29
30
# File 'lib/smart_money/operations.rb', line 28

def /(value)
  Money.new @amount/value, @currency if value.is_a?(Numeric)
end

#<=>(other) ⇒ Object

Raises:

  • (TypeError)


37
38
39
40
41
42
43
# File 'lib/smart_money/operations.rb', line 37

def <=>(other)
  raise TypeError unless other.is_a?(Money)
  if @currency != other.currency
    other = other.convert_to(@currency)
  end
  @amount.round(2) <=> other.amount.round(2)
end

#==(other) ⇒ Object

Raises:

  • (TypeError)


32
33
34
35
# File 'lib/smart_money/operations.rb', line 32

def ==(other)
  raise TypeError unless other.is_a?(Money)
  @amount.round(2)==other.convert_to(@currency).amount.round(2)
end

#convert_to(new_currency) ⇒ Object



5
6
7
8
# File 'lib/smart_money/operations.rb', line 5

def convert_to(new_currency)
  new_amount = Money.convert(@amount, @currency.upcase, new_currency.upcase).round(2)
  Money.new(new_amount, new_currency)
end

#inspectObject



10
11
12
# File 'lib/smart_money/operations.rb', line 10

def inspect
  "#{@amount} #{@currency}"
end