Class: Money
Constant Summary
collapse
- @@configure =
[ Config.new("EUR", { 'USD' => 1.11, 'Bitcoin' => 0.0047 } ),
Config.new("USD", { 'EUR' => (1/1.11), 'Bitcoin' => (0.0047/1.11) } ),
Config.new("Bitcoin", { 'USD' => (1.11/0.0047), 'EUR' => (1/0.0047) } )]
Instance Attribute Summary collapse
Attributes inherited from Config
#conversions, #default_currency
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(value, name) ⇒ Money
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/money.rb', line 15
def initialize(value, name)
if value.is_a? Numeric
@amount = value
else
raise "No es posible instanciar el objeto, #{value} es un #{value.class},
debe ser un numero Ej: 50 , 5.05"
end
if name.is_a? String
@@configure.each do |config|
next unless config.default_currency == name
@currency = name
return
end
raise "El parametro #{name} no se encuentra en la configuracion"
else
raise "No es posible instanciar el objeto, #{name} es un #{name.class},
debe ser un string Ej: \"EUR\" , \"USD\", \"Bitcoin\""
end
end
|
Instance Attribute Details
#amount ⇒ Object
Returns the value of attribute amount.
10
11
12
|
# File 'lib/money.rb', line 10
def amount
@amount
end
|
#currency ⇒ Object
Returns the value of attribute currency.
10
11
12
|
# File 'lib/money.rb', line 10
def currency
@currency
end
|
Class Method Details
120
121
122
|
# File 'lib/money.rb', line 120
def self.configure
@@configure
end
|
Instance Method Details
#*(x) ⇒ Object
86
87
88
|
# File 'lib/money.rb', line 86
def *(x)
return "#{@amount * x} #{@currency}"
end
|
#+(x) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/money.rb', line 56
def +(x)
unless x.currency == @currency
@@configure.each do |config|
if config.default_currency == x.currency
aux = ( (@amount + config.conversions[@currency] * x.amount) * 100).round/100.0
return "#{aux} #{@currency}"
end
end
end
aux = ((@amount + x.amount) * 100).round/100.0
return "#{aux} #{@currency}"
end
|
#-(x) ⇒ Object
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/money.rb', line 69
def -(x)
unless x.currency == @currency
@@configure.each do |config|
if config.default_currency == x.currency
aux = ((@amount - config.conversions[@currency] * x.amount) * 100).round/100.0
return "#{aux} #{@currency}"
end
end
end
aux = ((@amount - x.amount) * 100).round/100.0
return "#{aux} #{@currency}"
end
|
#/(x) ⇒ Object
82
83
84
|
# File 'lib/money.rb', line 82
def /(x)
return "#{@amount / x} #{@currency}"
end
|
#<(x) ⇒ Object
110
111
112
113
114
115
116
117
118
|
# File 'lib/money.rb', line 110
def <(x)
if not x.currency == @currency
@@configure.each do |config|
return ( config.default_currency == x.currency and @amount < (x.amount * config.conversions[@currency]) )
end
end
return ( @amount < x.amount )
end
|
#==(x) ⇒ Object
90
91
92
93
94
95
96
97
98
|
# File 'lib/money.rb', line 90
def ==(x)
if not x.currency == @currency
@@configure.each do |config|
return ( config.default_currency == x.currency and @amount == (x.amount * config.conversions[@currency]) )
end
end
return ( x.amount == @amount )
end
|
#>(x) ⇒ Object
100
101
102
103
104
105
106
107
108
|
# File 'lib/money.rb', line 100
def >(x)
if not x.currency == @currency
@@configure.each do |config|
return ( config.default_currency == x.currency and @amount > (x.amount * config.conversions[@currency]) )
end
end
return ( @amount > x.amount )
end
|
#convert_to(name) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/money.rb', line 39
def convert_to(name)
if name.is_a? String
@@configure.each do |config|
next unless config.default_currency == currency
@amount = (config.conversions[name] * @amount *100).round/100.0
@currency = name
return self
end
raise "El parametro #{name} no se encuentra en la configuracion"
else
raise "No es posible realizar la conversion de #{@currency} a #{name} este no se encuentra en la configuracion
Ej: \"EUR\" , \"USD\", \"Bitcoin\""
end
end
|
#inspect ⇒ Object
35
36
37
|
# File 'lib/money.rb', line 35
def inspect
"#{@amount} #{@currency}"
end
|