Module: Konvert::Operations

Included in:
Money
Defined in:
lib/operations.rb

Instance Method Summary collapse

Instance Method Details

#*(operand) ⇒ Object



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

def *(operand)
  Money.new(self.amount * operand, self.currency)
end

#+(operand) ⇒ Object



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

def +(operand)
  perform_sum(self,operand,'add')
end

#-(operand) ⇒ Object



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

def -(operand)
  perform_sum(self,operand,'subtract')
end

#/(operand) ⇒ Object



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

def /(operand)
  Money.new(self.amount / operand, self.currency)
end

#<(operand) ⇒ Object



46
47
48
# File 'lib/operations.rb', line 46

def <(operand)
  return self.convert_to_base_amount < operand.convert_to_base_amount
end

#==(operand) ⇒ Object

Comparisons



38
39
40
# File 'lib/operations.rb', line 38

def ==(operand)
  return self.convert_to_base_amount == operand.convert_to_base_amount
end

#>(operand) ⇒ Object



42
43
44
# File 'lib/operations.rb', line 42

def >(operand)
  return self.convert_to_base_amount > operand.convert_to_base_amount
end

#add(val1, val2) ⇒ Object



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

def add(val1,val2)
  val1 + val2
end

#perform_sum(operand1, operand2, operation) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/operations.rb', line 4

def perform_sum(operand1,operand2,operation)
  amount1 = operand1.convert_to_base_amount
  amount2 = operand2.convert_to_base_amount
  sum_in_base_currency = send(operation, amount1, amount2)
  result = Money.new(sum_in_base_currency, Money.base_currency)
  operand1.currency == Money.base_currency ? result : result.convert_to(operand1.currency)
end

#subtract(val1, val2) ⇒ Object



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

def subtract(val1,val2)
  val1 - val2
end