dw_money 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5538e7f491c6083a0cf7769ded2c78daacb45928
4
+ data.tar.gz: 03a66a0dfb5447fce1f7c5af19aa7cab5f3eb139
5
+ SHA512:
6
+ metadata.gz: 7365a54ef971b3eb6a869d5d52b9d4b82e789371b2c89f5a8460fc868cb477a52ad22002fce80946ec54554325c9310902323522d76b1d8be8e4ab0adbf0db95
7
+ data.tar.gz: dc75461b4313963fe32b70c84eb00355312ed4cc09d6d1e9f7250f7fc522ee28495482f400b645f9a338bfcbaf934f8be87ee2022c0507bda51c982d7ca44620
data/.gitignore ADDED
@@ -0,0 +1,12 @@
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
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,2 @@
1
+ Metrics/LineLength:
2
+ Max: 120
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.4
5
+ before_install: gem install bundler -v 1.14.3
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dw_money.gemspec
4
+ gemspec
5
+
6
+ group :development, :test do
7
+ gem 'rubocop'
8
+ gem 'pry'
9
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Pedro Vitti
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,81 @@
1
+ # DwMoney
2
+
3
+ DwMoney is a flexible solution for conversion and arithmetics with different currencies.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'dw_money'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install dw_money
20
+
21
+ ## Usage
22
+ #### Configure
23
+
24
+ Configuring the currency rates with respect to a base currency (here EUR):
25
+
26
+ ```ruby
27
+ DwMoney::Money.conversion_rates('EUR', {
28
+ 'USD' => 1.11,
29
+ 'Bitcoin' => 0.0047
30
+ })
31
+ ```
32
+
33
+ #### Convert to a different currency
34
+
35
+ ```ruby
36
+ fifty_eur = DwMoney::Money.new(50, 'EUR')
37
+ fifty_eur.amount # => 50
38
+ fifty_eur.currency # => "EUR"
39
+ fifty_eur.inspect # => "50.00 EUR"
40
+
41
+ fifty_eur.convert_to('USD') # => 55.50 USD
42
+ ```
43
+
44
+ #### Arithmetics
45
+
46
+ ```ruby
47
+ twenty_dollars = DwMoney::Money.new(20, 'USD')
48
+
49
+ fifty_eur + twenty_dollars # => 68.02 EUR
50
+ fifty_eur - twenty_dollars # => 31.98 EUR
51
+ fifty_eur / 2 # => 25 EUR
52
+ twenty_dollars * 3 # => 60 USD
53
+ ```
54
+
55
+ #### Comparisons (also in different currencies):
56
+
57
+ ```ruby
58
+ twenty_dollars == DwMoney::Money.new(20, 'USD') # => true
59
+ twenty_dollars == DwMoney::Money.new(30, 'USD') # => false
60
+
61
+ fifty_eur_in_usd = fifty_eur.convert_to('USD')
62
+ fifty_eur_in_usd == fifty_eur # => true
63
+
64
+ twenty_dollars > DwMoney::Money.new(5, 'USD') # => true
65
+ twenty_dollars < fifty_eur # => true
66
+ ```
67
+
68
+ ## Development
69
+
70
+ 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.
71
+
72
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
73
+
74
+ ## Contributing
75
+
76
+ Bug reports and pull requests are welcome on GitHub at https://github.com/pedrovitti/dw_money.
77
+
78
+
79
+ ## License
80
+
81
+ 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 'dw_money'
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/dw_money.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dw_money/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'dw_money'
8
+ spec.version = DwMoney::VERSION
9
+ spec.authors = ['Pedro Vitti']
10
+ spec.email = ['pedrovitti@gmail.com']
11
+
12
+ spec.summary = 'Currency conversion and arithmetics with different currencies.'
13
+ spec.homepage = 'http://github.com/pedrovitti/dw_money'
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_development_dependency 'bundler', '~> 1.14'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'rspec', '~> 3.4'
26
+ end
@@ -0,0 +1,18 @@
1
+ module DwMoney
2
+ class Money
3
+ module Arithmetic
4
+ def <=>(other)
5
+ amount.round(2) <=> other.convert_to(currency).amount.round(2)
6
+ end
7
+
8
+ [:+, :-, :*, :/].each do |math_operator|
9
+ define_method(math_operator) do |other|
10
+ raise TypeError unless other.is_a?(Money) || other.is_a?(Numeric)
11
+
12
+ other = other.convert_to(currency).amount if other.is_a?(Money)
13
+ Money.new(amount.public_send(math_operator, other), currency)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,46 @@
1
+ require 'dw_money/money/arithmetic'
2
+
3
+ module DwMoney
4
+ class ConversionRateNotFound < StandardError; end
5
+
6
+ class Money
7
+ include Comparable
8
+ include Money::Arithmetic
9
+
10
+ attr_reader :amount, :currency
11
+
12
+ @conversion_rates = {}
13
+
14
+ def self.conversion_rates(base_currency, rates)
15
+ @conversion_rates[base_currency] = rates
16
+ end
17
+
18
+ def initialize(amount, currency)
19
+ @amount = amount
20
+ @currency = currency
21
+ end
22
+
23
+ def inspect
24
+ "#{format('%.2f', amount)} #{currency}"
25
+ end
26
+
27
+ def convert_to(new_currency)
28
+ return self if new_currency == currency
29
+
30
+ raise ConversionRateNotFound, "#{new_currency} => #{currency}" unless rates && rates[new_currency]
31
+
32
+ new_amount = amount * rates[new_currency]
33
+ Money.new(new_amount, new_currency)
34
+ end
35
+
36
+ private
37
+
38
+ def rates
39
+ self.class.conversion_rates_configuration[currency]
40
+ end
41
+
42
+ def self.conversion_rates_configuration
43
+ @conversion_rates
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ module DwMoney
2
+ VERSION = '0.1.1'.freeze
3
+ end
data/lib/dw_money.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'dw_money/version'
2
+ require 'dw_money/money'
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dw_money
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Pedro Vitti
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-06-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.4'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.4'
55
+ description:
56
+ email:
57
+ - pedrovitti@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".rubocop.yml"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - dw_money.gemspec
73
+ - lib/dw_money.rb
74
+ - lib/dw_money/money.rb
75
+ - lib/dw_money/money/arithmetic.rb
76
+ - lib/dw_money/version.rb
77
+ homepage: http://github.com/pedrovitti/dw_money
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.4.5.1
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Currency conversion and arithmetics with different currencies.
101
+ test_files: []