Class: TqCurencyManager::Money

Inherits:
Object
  • Object
show all
Defined in:
lib/tq_curency_manager.rb

Constant Summary collapse

USD_FOR_EUR =

Init & getters

1.11
BITCOIN_FOR_EUR =
0.0047
@@rates =
{
	'EUR' => 1,
	'USD' => USD_FOR_EUR,
	'Bitcoin' => BITCOIN_FOR_EUR
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amount, curency) ⇒ Money

Returns a new instance of Money.



16
17
18
19
20
21
22
# File 'lib/tq_curency_manager.rb', line 16

def initialize amount, curency
	@amount = amount.to_f
	@curency = curency
	if !@@rates.key? curency
		raise ArgumentError.new("Not valid curency")
	end
end

Class Method Details

.conversion_rates(base_curency, rates) ⇒ Object

Setters



52
53
54
55
56
57
58
# File 'lib/tq_curency_manager.rb', line 52

def self.conversion_rates base_curency, rates
	@@rates[base_curency] = 1
	rates.each do |k,v|
		@@rates[k] = v.to_f
	end
	@@rates
end

.ratesObject



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

def self.rates
	@@rates
end

Instance Method Details

#*(float) ⇒ Object



87
88
89
90
# File 'lib/tq_curency_manager.rb', line 87

def *(float)
	amount = self.amount * float
	self.class.new(amount, self.curency)
end

#+(instance) ⇒ Object



77
78
79
80
# File 'lib/tq_curency_manager.rb', line 77

def +(instance)
	amount = self.amount + instance.convert_to(self.curency).amount
	self.class.new(amount, self.curency)
end

#-(instance) ⇒ Object



82
83
84
85
# File 'lib/tq_curency_manager.rb', line 82

def -(instance)
	amount = self.amount - instance.convert_to(self.curency).amount
	self.class.new(amount, self.curency)
end

#/(float) ⇒ Object



92
93
94
95
# File 'lib/tq_curency_manager.rb', line 92

def /(float)
	amount = self.amount / float
	self.class.new(amount, self.curency)
end

#<(instance) ⇒ Object



102
103
104
105
# File 'lib/tq_curency_manager.rb', line 102

def <(instance)
amount = instance.convert_to(self.curency).amount
self.amount < amount
end

#==(instance) ⇒ Object

Comparaison methods



73
74
75
# File 'lib/tq_curency_manager.rb', line 73

def ==(instance)
	self.convert_to('EUR').inspect == instance.convert_to('EUR').inspect
end

#>(instance) ⇒ Object



97
98
99
100
# File 'lib/tq_curency_manager.rb', line 97

def >(instance)
	amount = instance.convert_to(self.curency).amount
	self.amount > amount
end

#amountObject



28
29
30
31
32
33
34
35
# File 'lib/tq_curency_manager.rb', line 28

def amount
	# we just give different rounding with bitcoin
	if curency == 'Bitcoin'
		@amount.round(8)
	else
		@amount.round(2)
	end
end

#convert_to(curency) ⇒ Object

Curency conversion



62
63
64
65
66
67
68
69
# File 'lib/tq_curency_manager.rb', line 62

def convert_to(curency)
	if @@rates[curency]
		self.class.new(amount_convert(curency), curency)
	else
		puts 'error : you have to initialize the curency convertion for ' + curency
		nil
	end
end

#curencyObject



37
38
39
# File 'lib/tq_curency_manager.rb', line 37

def curency
	@curency
end

#inspectObject

‘%.Xf’ % allows to have X digits after the point



42
43
44
45
46
47
48
# File 'lib/tq_curency_manager.rb', line 42

def inspect
	if curency == 'Bitcoin'
		'%.8f' % amount + " " + curency
	else
		'%.2f' % amount + " " + curency
	end
end