moneygm 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/moneygm.rb +83 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 071bcb08ef1e518a2d89046b03c4b3970244e560
4
+ data.tar.gz: c0c86e86347728539c6b1555182598fea292948f
5
+ SHA512:
6
+ metadata.gz: 83fd94f3ff8e28e1b33c19827e9a3e1908f042ee6d287144fbf0718968a1de7cf3341dec1ee48bf7ebfc58c00af5c6c32411cd7ce683c53ca9c7fba3f1e2d8bb
7
+ data.tar.gz: a2eaf4a7850fded87b84a886b165dd44cb4911d16ddde66d6868cab1e5f1832bc87b5f0232ebd054def1efd07ec8e304c74105b1a705fb435cd2fa52a1371230
data/lib/moneygm.rb ADDED
@@ -0,0 +1,83 @@
1
+ class Money
2
+ attr_accessor :amount, :currency, :conversion
3
+ def initialize(amount, currency = nil)
4
+ @amount = amount
5
+ @currency = currency || "EUR" #Si no hay moneda definida, la default es EUR
6
+ @conversion = { 'USD' => 1.11, 'Bitcoin' => 0.0047 } if @currency == "EUR"
7
+ @conversion = { 'EUR' => 0.90, 'Bitcoin' => 0.00423 } if @currency == "USD"
8
+ @conversion = { 'EUR' => 212.76, 'USD' => 236.16 } if @currency == "Bitcoin"
9
+ end
10
+
11
+ def inspect
12
+ "#{@amount} #{@currency}"
13
+ end
14
+
15
+ # Retorna una copia del objeto con la conversion solicitada, si la moneda no existe retorna el obj inicial
16
+ def convert_to(currency_to_convert)
17
+ obj_copy = self.dup #copio el objeto
18
+ return obj_copy if self.conversion[currency_to_convert].nil? || currency_to_convert == self.currency
19
+ case currency_to_convert
20
+ when "USD"
21
+ obj_copy.amount = (obj_copy.amount * obj_copy.conversion['USD']).round(2)
22
+ when "Bitcoin"
23
+ obj_copy.amount = (obj_copy.amount * obj_copy.conversion['Bitcoin']).round(2)
24
+ when "EUR"
25
+ obj_copy.amount = (obj_copy.amount * obj_copy.conversion['EUR']).round(2)
26
+ end
27
+ obj_copy.currency = currency_to_convert
28
+ return obj_copy
29
+ end
30
+
31
+ # OPERACIONES ARITMETICAS
32
+
33
+ # Para la suma y la resta se asume que siempre seran dos objetos de tipo Money y que si las
34
+ # monedas son diferentes la conversion se hara a EUR
35
+ def + (object_to_add)
36
+ if self.currency == object_to_add.currency
37
+ value = self.amount + object_to_add.amount
38
+ "#{value} #{self.currency}"
39
+ else
40
+ value = self.convert_to('EUR').amount + object_to_add.convert_to('EUR').amount
41
+ "#{value} EUR"
42
+ end
43
+ end
44
+
45
+ def - (obj_to_substract)
46
+ if self.currency == obj_to_substract.currency
47
+ value = self.amount - obj_to_substract.amount
48
+ # se coloca el valor absoluto (value.abs) ya que si restas dos monedas (ej 2 billetes) no queda un saldo negativo, sin importar el orden de los factores
49
+ "#{value.abs} #{self.currency}"
50
+ else
51
+ value = self.convert_to('EUR').amount - obj_to_substract.convert_to('EUR').amount
52
+ "#{value.abs} EUR"
53
+ end
54
+ end
55
+
56
+ # Para la multiplicacion y la division se asume que los parametros seran valores numericos
57
+ # y no un objeto
58
+ def * (multiplier)
59
+ value = self.amount * multiplier
60
+ "#{value} #{self.currency}"
61
+ end
62
+
63
+ def / (divider)
64
+ value = self.amount / divider
65
+ "#{value} #{self.currency}"
66
+ end
67
+
68
+ # COMPARACIONES
69
+
70
+ # Solo se consideran los operadores == , >, <
71
+ def == (obj)
72
+ value = self.convert_to('EUR').amount == obj.convert_to('EUR').amount ? true : false
73
+ end
74
+
75
+ def > (obj)
76
+ value = self.convert_to('EUR').amount > obj.convert_to('EUR').amount ? true : false
77
+ end
78
+
79
+ def < (obj)
80
+ value = self.convert_to('EUR').amount < obj.convert_to('EUR').amount ? true : false
81
+ end
82
+ end
83
+
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: moneygm
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Gipsy Martinez
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-08-19 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple money onversor gem
14
+ email: gipsy.martinez@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/moneygm.rb
20
+ homepage: ''
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.6.10
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Money Conversor
44
+ test_files: []