operation_currency 0.0.1

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/money.rb +123 -0
  3. metadata +45 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 211a16ee6af9f725146f41546f1916f96260d34f
4
+ data.tar.gz: 539d55dc95847ded449b058d70a239106a182d38
5
+ SHA512:
6
+ metadata.gz: 5034a84e85f89f1a5f0e72a7bb0d4bfc0748ebcdc850d331c73fcdd1648ad1cafd3cc75b1c696ac2dad38d8f6fdd946caa29ef7604ef8688d2b34349f66c0596
7
+ data.tar.gz: 1e501b115040938ef46b9422ace090e445f7e568bf4ff0c666a927ebc2c323e4a7a8477768ff9b919a78fe3a234ae1616cc26931af058335a5a2ea13242812ca
@@ -0,0 +1,123 @@
1
+ class Config
2
+ attr_reader :default_currency, :conversions
3
+ def initialize(currency, conversion)
4
+ @default_currency = currency
5
+ @conversions = conversion
6
+ end
7
+ end
8
+
9
+ class Money < Config
10
+ attr_reader :amount, :currency
11
+ @@configure = [ Config.new("EUR", { 'USD' => 1.11, 'Bitcoin' => 0.0047 } ),
12
+ Config.new("USD", { 'EUR' => (1/1.11), 'Bitcoin' => (0.0047/1.11) } ),
13
+ Config.new("Bitcoin", { 'USD' => (1.11/0.0047), 'EUR' => (1/0.0047) } )]
14
+
15
+ def initialize(value, name)
16
+ if value.is_a? Numeric
17
+ @amount = value
18
+ else
19
+ raise "No es posible instanciar el objeto, #{value} es un #{value.class},
20
+ debe ser un numero Ej: 50 , 5.05"
21
+ end
22
+ if name.is_a? String
23
+ @@configure.each do |config|
24
+ next unless config.default_currency == name
25
+ @currency = name
26
+ return
27
+ end
28
+ raise "El parametro #{name} no se encuentra en la configuracion"
29
+ else
30
+ raise "No es posible instanciar el objeto, #{name} es un #{name.class},
31
+ debe ser un string Ej: \"EUR\" , \"USD\", \"Bitcoin\""
32
+ end
33
+ end
34
+
35
+ def inspect
36
+ "#{@amount} #{@currency}"
37
+ end
38
+
39
+ def convert_to(name)
40
+ if name.is_a? String
41
+ @@configure.each do |config|
42
+ next unless config.default_currency == currency
43
+
44
+ @amount = (config.conversions[name] * @amount *100).round/100.0
45
+ @currency = name
46
+
47
+ return self
48
+ end
49
+ raise "El parametro #{name} no se encuentra en la configuracion"
50
+ else
51
+ raise "No es posible realizar la conversion de #{@currency} a #{name} este no se encuentra en la configuracion
52
+ Ej: \"EUR\" , \"USD\", \"Bitcoin\""
53
+ end
54
+ end
55
+
56
+ def +(x)
57
+ unless x.currency == @currency
58
+ @@configure.each do |config|
59
+ if config.default_currency == x.currency
60
+ aux = ( (@amount + config.conversions[@currency] * x.amount) * 100).round/100.0
61
+ return "#{aux} #{@currency}"
62
+ end
63
+ end
64
+ end
65
+ aux = ((@amount + x.amount) * 100).round/100.0
66
+ return "#{aux} #{@currency}"
67
+ end
68
+
69
+ def -(x)
70
+ unless x.currency == @currency
71
+ @@configure.each do |config|
72
+ if config.default_currency == x.currency
73
+ aux = ((@amount - config.conversions[@currency] * x.amount) * 100).round/100.0
74
+ return "#{aux} #{@currency}"
75
+ end
76
+ end
77
+ end
78
+ aux = ((@amount - x.amount) * 100).round/100.0
79
+ return "#{aux} #{@currency}"
80
+ end
81
+
82
+ def /(x)
83
+ return "#{@amount / x} #{@currency}"
84
+ end
85
+
86
+ def *(x)
87
+ return "#{@amount * x} #{@currency}"
88
+ end
89
+
90
+ def ==(x)
91
+ if not x.currency == @currency
92
+ @@configure.each do |config|
93
+ return ( config.default_currency == x.currency and @amount == (x.amount * config.conversions[@currency]) )
94
+ end
95
+ end
96
+ return ( x.amount == @amount )
97
+
98
+ end
99
+
100
+ def >(x)
101
+ if not x.currency == @currency
102
+ @@configure.each do |config|
103
+ return ( config.default_currency == x.currency and @amount > (x.amount * config.conversions[@currency]) )
104
+ end
105
+ end
106
+ return ( @amount > x.amount )
107
+
108
+ end
109
+
110
+ def <(x)
111
+ if not x.currency == @currency
112
+ @@configure.each do |config|
113
+ return ( config.default_currency == x.currency and @amount < (x.amount * config.conversions[@currency]) )
114
+ end
115
+ end
116
+ return ( @amount < x.amount )
117
+
118
+ end
119
+
120
+ def self.configure
121
+ @@configure
122
+ end
123
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: operation_currency
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Diego Navarro
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-08-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: It realize exchange of currencies and simple arithmetic operations between
14
+ different currencies.
15
+ email: diegonavarro42@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/money.rb
21
+ homepage: http://rubygems.org/gems/operation_currency
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.6.12
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: This is a simple change of currency
45
+ test_files: []