geld 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a477e02346ad9f481e7bbf58304e68419aea5641
4
+ data.tar.gz: 98071b65fc6773fc6f8e29be10bea7619efd96a1
5
+ SHA512:
6
+ metadata.gz: 1a9febd55f109812b20ba2c2a30f8e892cfb9c1094c3fe1b1bb1112ef166b17105dcc35d2e211e6cf4a7ee670a7e9b33a2502400b6358b96584d061263a9506e
7
+ data.tar.gz: 3fa8a5bf98ddbae6d522d00d37405bd7092ee608c6db68438388a772dd8c29ee5de0a06b24690abda4c1eb1d4880225ea1d61340e7baa07712ae0130a44cf862
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+
14
+ *.sublime-*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in geld.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Pedro Benevides
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # Geld
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'geld'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install geld
18
+
19
+ ## Usage
20
+
21
+ Configure your convertions
22
+
23
+ ```ruby
24
+ Geld::Money.convertion_rates("EUR", { "BRL" => 4, "USD" => 1.5 })
25
+ ```
26
+
27
+ Instanciate a new money object
28
+
29
+ ```ruby
30
+ Geld::Money.new(10, "BRL")
31
+ => 10.00 BRL
32
+ ```
33
+
34
+ Convert to another currency
35
+
36
+ ```ruby
37
+ Geld::Money.new(10, "BRL").convert_to("USD")
38
+ => 3.10 USD
39
+ ```
40
+
41
+ Do operations with another currency
42
+
43
+ ```ruby
44
+ Geld::Money.new(10, "EUR") + Geld::Money.new(10, "BRL")
45
+ => 10.25 EUR
46
+ ```
47
+
48
+ Compare with another currency
49
+
50
+ ```ruby
51
+ Geld::Money.new(1, "EUR") == Geld::Money.new(4, "BRL")
52
+ => true
53
+ ```
54
+
55
+ ## Development
56
+
57
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
58
+
59
+ You can also create new example files in the examples folder `ruby examples/usage.rb`
60
+
61
+ ## Contributing
62
+
63
+ Bug reports and pull requests are welcome on GitHub at https://github.com/pedroaugusto/geld.
64
+
65
+ ## License
66
+
67
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "geld"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ require "pry"
11
+ Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/examples/usage.rb ADDED
@@ -0,0 +1,25 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require "geld"
3
+
4
+ rates_eur = { "BRL" => 3.63, "USD" => 1.12 }
5
+
6
+ puts "EUR => #{rates_eur}"
7
+
8
+ Geld::Money.convertion_rates("EUR", rates_eur)
9
+
10
+ m1 = Geld::Money.new(10, "BRL")
11
+ puts "m1 is #{m1.inspect}"
12
+ puts "m1 is #{m1.convert_to("USD").inspect}"
13
+
14
+ m2 = Geld::Money.new(99.10, "EUR")
15
+ puts "m2 is #{m2.inspect}"
16
+ puts "m2 is #{m2.convert_to("BRL").inspect}"
17
+
18
+ m3 = Geld::Money.new(14.67, "USD")
19
+ puts "m3 is #{m3.inspect}"
20
+ puts "m3 is #{m3.convert_to("BRL").inspect}"
21
+
22
+ puts "m1 + m2 is #{(m1 + m2).inspect}"
23
+ puts "m1 - m3 is #{(m1 - m3).inspect}"
24
+ puts "m2 * m3 is #{(m2 * m3).inspect}"
25
+ puts "m3 / m1 is #{(m3 / m1).inspect}"
data/geld.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "geld/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "geld"
8
+ spec.version = Geld::VERSION
9
+ spec.authors = ["Pedro Benevides"]
10
+ spec.email = ["pedro.augusto.sb@gmail.com"]
11
+
12
+ spec.summary = %q{Gem to manage money currency operations.}
13
+ spec.homepage = "https://github.com/pedroaugusto/geld"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency "activesupport", "~> 4.2"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.15"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ spec.add_development_dependency "pry"
29
+ spec.add_development_dependency "pry-byebug"
30
+ end
data/lib/geld.rb ADDED
@@ -0,0 +1,104 @@
1
+ require "active_support/core_ext/module/attribute_accessors"
2
+ require "bigdecimal"
3
+ require "geld/config"
4
+ require "geld/error/currency_error"
5
+ require "geld/error/currency_not_found"
6
+ require "geld/version"
7
+
8
+ module Geld
9
+
10
+ # Singleton Config instance that stores the convertion rates
11
+ mattr_accessor :config
12
+
13
+ # Main class
14
+ class Money
15
+
16
+ attr_accessor :amount, :currency
17
+
18
+ # Uses bigdecimal for safer money operations
19
+ def initialize(amount, currency)
20
+ @amount = BigDecimal.new(amount.to_s)
21
+ @currency = currency
22
+ end
23
+
24
+ # Adds a converion rate in the configuration storage
25
+ def self.convertion_rates(currency, rates)
26
+ Geld.config ||= Config.new
27
+ Geld.config.add_currency(currency, rates)
28
+ end
29
+
30
+ # Apply the specific rate or raises error if not found
31
+ def convert_to(new_currency)
32
+ return dup if new_currency == self.currency
33
+
34
+ new_amount = Geld.config &&
35
+ Geld.config.get_rate(self.currency, new_currency) &&
36
+ Geld.config.get_rate(self.currency, new_currency).mult(self.amount, 2)
37
+
38
+ if new_amount.nil?
39
+ raise Error::CurrencyNotFound.new("The desired currency is not set.")
40
+ else
41
+ Money.new(new_amount, new_currency)
42
+ end
43
+ end
44
+
45
+ # Sums two moneys
46
+ def +(money)
47
+ operate(money, :add)
48
+ end
49
+
50
+ # Subtracts two moneys
51
+ def -(money)
52
+ operate(money, :sub)
53
+ end
54
+
55
+ # Multiplies two moneys
56
+ def *(money)
57
+ operate(money, :mult)
58
+ end
59
+
60
+ # Divides two moneys
61
+ def /(money)
62
+ operate(money, :div)
63
+ end
64
+
65
+ # Shows money formated
66
+ def inspect
67
+ "#{'%.2f' % amount} #{currency}"
68
+ end
69
+
70
+ # Tests equality
71
+ def ==(money)
72
+ compare(money, :==)
73
+ end
74
+
75
+ # Tests equality
76
+ def !=(money)
77
+ compare(money, :!=)
78
+ end
79
+
80
+ # Tests greater than
81
+ def >(money)
82
+ compare(money, :>)
83
+ end
84
+
85
+ # Tests less than
86
+ def <(money)
87
+ compare(money, :<)
88
+ end
89
+
90
+ private
91
+
92
+ # executes an arithmetic operation after convert to the same currency
93
+ def operate(money, operation)
94
+ new_amount = self.amount.send(operation, money.convert_to(self.currency).amount, 2)
95
+ Money.new(new_amount, self.currency)
96
+ end
97
+
98
+ # executes an comparisson after convert
99
+ def compare(money, operator)
100
+ self.amount.send(operator, money.convert_to(self.currency).amount)
101
+ end
102
+
103
+ end
104
+ end
@@ -0,0 +1,59 @@
1
+ module Geld
2
+ class Config
3
+
4
+ # Set a new currency configuration.
5
+ # If the rates table is set more than once, the new one overrides the older
6
+ def add_currency(currency, rates)
7
+ validate_currency(currency)
8
+ validate_rates(rates)
9
+
10
+ @currencies ||= {}
11
+
12
+ # Converts rates to BigDecimal
13
+ bd_rates = rates.inject({}){|hash, (k,v)| hash[k] = BigDecimal.new(v.to_s); hash }
14
+ @currencies[currency] = bd_rates
15
+
16
+ bd_rates.each do |a_currency, a_rate|
17
+
18
+ # reverse operation will be the first element
19
+ current_rate = BigDecimal.new("1").div(a_rate, 2) # 1/rate 2 digits
20
+ @currencies[a_currency] = { currency => current_rate }
21
+
22
+ # now the others
23
+ (bd_rates.keys - [a_currency]).each do |other_currency|
24
+ @currencies[a_currency][other_currency] = @currencies[currency][other_currency].mult(current_rate, 2)
25
+ end
26
+
27
+ end
28
+ # returns the new calculations
29
+ @currencies
30
+ end
31
+
32
+ # returns the specific rate
33
+ def get_rate(from, to)
34
+ @currencies &&
35
+ @currencies[from] &&
36
+ @currencies[from][to]
37
+ end
38
+
39
+ private
40
+
41
+ # Validates currency entry
42
+ def validate_currency(currency)
43
+ unless currency.is_a?(String) && !currency.empty?
44
+ raise Error::CurrencyError.new("Invalid currency. Should be a non empty string")
45
+ end
46
+ end
47
+
48
+ # Validates rates entry
49
+ def validate_rates(rates)
50
+ unless rates.is_a?(Hash) &&
51
+ !rates.empty? &&
52
+ !rates.keys.any?{|key| !key.is_a?(String) } &&
53
+ !rates.values.any?{|value| !value.is_a?(Numeric) }
54
+ raise Error::CurrencyError.new("Invalid rates format. Should be a not empty hash of { String => Numeric }")
55
+ end
56
+ end
57
+
58
+ end
59
+ end
@@ -0,0 +1,5 @@
1
+ module Geld
2
+ module Error
3
+ class CurrencyError < ::RuntimeError; end
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Geld
2
+ module Error
3
+ class CurrencyNotFound < ::RuntimeError; end
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module Geld
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: geld
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Pedro Benevides
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-06-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.15'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.15'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description:
98
+ email:
99
+ - pedro.augusto.sb@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - bin/console
112
+ - bin/setup
113
+ - examples/usage.rb
114
+ - geld.gemspec
115
+ - lib/geld.rb
116
+ - lib/geld/config.rb
117
+ - lib/geld/error/currency_error.rb
118
+ - lib/geld/error/currency_not_found.rb
119
+ - lib/geld/version.rb
120
+ homepage: https://github.com/pedroaugusto/geld
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.6.11
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Gem to manage money currency operations.
144
+ test_files: []