convers_money 0.1.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b2620d2419b9b8c1581afe23181d4d720b3fa3a5
4
- data.tar.gz: 3083ef4714fb584877d2af64d22067a23f9f3003
3
+ metadata.gz: 69c34b7aae24a455b862d3f22e03836a79a3fa45
4
+ data.tar.gz: 18b2f788e8c2ee46a50b7f0490455bbd86257f9e
5
5
  SHA512:
6
- metadata.gz: 06e22832ec2cf426bc5e6a0a4fa57257a9ba9fb167f13381209db89cd29ba02afa9da36a060cabba2ef5c2348d982650ef8c6be4bef7571f190ad5140ba19f48
7
- data.tar.gz: 02cfe6cb4c2bccf9b55c8a872363286f1a411e1690fefb8acd507cb4f6c29711738fdc810812672ce49485191e0f8262aeccdacc714a2b3b86b38019303bfe96
6
+ metadata.gz: ebed51488bfad055db104033f03c7f14cbada3acaef865300a324e97549da679c44369586087927b8caa6e3a9ab4b13adb21fd9fbc6703f6a7d89bce62356586
7
+ data.tar.gz: 790f608ac5bf1a2eb577a4fd2e7990232c92471ea143eb9822689de4ede05b7cf5f45130a778d919e9de0681f77e78cf2e38844f8d9f32738a3926f6bb9f9182
data/README.md CHANGED
@@ -28,7 +28,7 @@ Or install it yourself as:
28
28
 
29
29
  ### Configure the currency rates with respect to a base currency (here EUR):
30
30
  ```ruby
31
- Money.conversion_rates('EUR', {
31
+ ConversMoney.conversion_rates('EUR', {
32
32
  'USD' => 1.11,
33
33
  'Bitcoin' => 0.0047
34
34
  })
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ['antonelo@gmail.com']
11
11
 
12
12
  spec.summary = 'Money conversor'
13
- spec.description = "ConversMoney it's a Money Conversor given by fixed currency and change rates of your choice."
13
+ spec.description = "ConversMoney it's a Money Conversor given by fixed currency and change rates of your choice. \n This version '1.0.0' it's the first full working version"
14
14
  spec.homepage = 'https://github.com/nelantone/convers_money'
15
15
  spec.license = 'MIT'
16
16
 
@@ -1,3 +1,3 @@
1
1
  class ConversMoney
2
- VERSION = '0.1.3'
2
+ VERSION = '1.0.0'
3
3
  end
data/lib/convers_money.rb CHANGED
@@ -1,8 +1,10 @@
1
1
  require 'convers_money/version'
2
+ require 'pry'
2
3
 
3
4
  # ConversMoney it's a Money Conversor given by fixed currency and change rates of your choice.
4
5
  class ConversMoney
5
- attr_accessor :amount, :currency, :second_amount
6
+ include Comparable
7
+ attr_accessor :amount, :currency
6
8
 
7
9
  def initialize(amount, currency)
8
10
  @amount = amount
@@ -12,8 +14,16 @@ class ConversMoney
12
14
 
13
15
  def self.conversion_rates(currency, conversion_rates)
14
16
  @conversion_rates ||= {}
15
- conversion_rates = Hash[conversion_rates.map { |k, v| [k.upcase, v] }]
17
+ conversion_rates = Hash[conversion_rates.map { |fixed_currency, rates| [fixed_currency.upcase, rates] }]
16
18
  @conversion_rates.merge!(currency.upcase => conversion_rates)
19
+ unless @conversion_rates.nil? || conversion_rates.nil?
20
+ @conversion_rates.each do |fixed_currency, rates|
21
+ rates.map do |currency_rate, value|
22
+ inverse_conversion = { fixed_currency => format('%.5f', (1 / value)).to_f.round(5) }
23
+ @conversion_rates = @conversion_rates.merge( currency_rate => inverse_conversion ) unless currency.eql? currency_rate
24
+ end
25
+ end
26
+ end
17
27
  end
18
28
 
19
29
  def self.validate!
@@ -38,7 +48,7 @@ class ConversMoney
38
48
  else
39
49
  converted_amount = ConversMoney.convert!(@amount, @currency, currency)
40
50
  amount_str = format('%.2f', converted_amount)
41
- ConversMoney.new(amount_str.to_f, currency)
51
+ ConversMoney.new(amount_str.to_f.round(2), currency)
42
52
  end
43
53
  end
44
54
 
@@ -46,36 +56,49 @@ class ConversMoney
46
56
  "#{format('%.2f', @amount)} #{@currency}"
47
57
  end
48
58
 
59
+ def convert_float_to_instance(other, currency)
60
+ if other.is_a? Float
61
+ other = ConversMoney.new( other, currency)
62
+ end
63
+ end
64
+
49
65
  def +(other)
66
+ if other.is_a? Float
67
+ other = ConversMoney.new( other, currency)
68
+ end
50
69
  total_amount = amount + other.convert_to(currency).amount
51
- ConversMoney.new(format('%.2f', total_amount), currency)
70
+ ConversMoney.new(total_amount.to_f.round(2), currency)
52
71
  end
53
72
 
54
73
  def -(other)
74
+ if other.is_a? Float
75
+ other = ConversMoney.new( other, currency)
76
+ end
55
77
  total_amount = amount - other.convert_to(currency).amount
56
- ConversMoney.new(format('%.2f', total_amount), currency)
78
+ ConversMoney.new(total_amount.to_f.round(2), currency)
57
79
  end
58
80
 
59
81
  def *(other)
82
+ if other.is_a? Float
83
+ other = ConversMoney.new( other, currency)
84
+ end
60
85
  total_amount = amount * other.convert_to(currency).amount
61
- ConversMoney.new(format('%.2f', total_amount), currency)
86
+ ConversMoney.new(total_amount.to_f.round(2), currency)
62
87
  end
63
88
 
64
89
  def /(other)
90
+ if other.is_a? Float
91
+ other = ConversMoney.new( other, currency)
92
+ end
65
93
  total_amount = amount / other.convert_to(currency).amount
66
- ConversMoney.new(format('%.2f', total_amount), currency)
67
- end
68
-
69
- def ==(other)
70
- amount == other.convert_to(currency).amount
94
+ ConversMoney.new(total_amount.to_f.round(2), currency)
71
95
  end
72
96
 
73
- def >(other)
74
- amount > other.convert_to(currency).amount
75
- end
76
-
77
- def <(other)
78
- amount < other.convert_to(currency).amount
97
+ def <=>(other)
98
+ if other.is_a? Float
99
+ other = ConversMoney.new( other.round(2), currency)
100
+ end
101
+ amount.round(2) <=> other.convert_to(currency).amount.round(2)
79
102
  end
80
103
 
81
104
  def self.checking_fixed_currency(other, currency)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: convers_money
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Toño Serna
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-02-04 00:00:00.000000000 Z
11
+ date: 2017-02-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -98,8 +98,8 @@ dependencies:
98
98
  - - ">="
99
99
  - !ruby/object:Gem::Version
100
100
  version: 4.6.0
101
- description: ConversMoney it's a Money Conversor given by fixed currency and change
102
- rates of your choice.
101
+ description: "ConversMoney it's a Money Conversor given by fixed currency and change
102
+ rates of your choice. \n This version '1.0.0' it's the first full working version"
103
103
  email:
104
104
  - antonelo@gmail.com
105
105
  executables: []