mgd_money 0.0.2 → 1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c4c10d10675064445a34fb24ab6efeb6e30146b9
4
- data.tar.gz: aa2d1e3d3cd53bd2c690b06656e2c8cb44c837fe
3
+ metadata.gz: 51b83b7f9ae3a67c02a8c54bb3ff6f30293311e6
4
+ data.tar.gz: f15781d2be55afaecd1470ad2d5d17f7e450b341
5
5
  SHA512:
6
- metadata.gz: 18b054083240cbf6538d0d52f4ddf4ad6192646807ef6b15fbccd55334c5cace2bd64df1c2d0678c070e04722a444f6a1b5bf13e743d7ce64916f234363cd1a6
7
- data.tar.gz: d961cc6071bf268f2ba391b28d6d9169fc440ea898e4004ad5059fac6f08e413524fc4a4e5a7f2b3e9d1fd6d59d9fabbf13963ea39dc32b3a52ba05369c97fb6
6
+ metadata.gz: f524c79a917aaaab75f88b16d34391d8927370919e793e912bb184b71484785e96d9208fa527ea716bac86d13dfaedfdc6dfd2d50f6ae48fcd87755a3f224016
7
+ data.tar.gz: f2d99a38c6af886d2fb8907f1313aa5c9d48e987678e945cee464d41cd6f79183dddc2980f47463818bcf1e242d4298fdf05cdc79a6c5d2883d6a3c980bd8eb4
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.4
5
+ before_install: gem install bundler -v 1.15.4
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in mgd_money.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Matt Davis
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.
@@ -0,0 +1,59 @@
1
+ # MgdMoney
2
+ The MGDMoney gem provides a simple interface for converting between currencies
3
+ and performing operations in different currencies.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'mgd_money'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install mgd_money
20
+
21
+ ## Usage
22
+
23
+ Define the conversion rates between currencies:
24
+
25
+ MGDMoney.conversion_rates("USD", { "EUR" => 0.75, "BTC" => 0.0001 })
26
+
27
+ You must specify at least one conversion rate or the gem will not know how to perform
28
+ any operations. The gem only supports conversion operations on those specified currencies.
29
+
30
+ After setting the rates, you can define new MGDMoney objects, which consist of an
31
+ `amount` and a `currency`:
32
+
33
+ twenty_dollars = MGDMoney.new(20, "USD")
34
+ ten_eur = MGDMoney.new(10, "EUR")
35
+
36
+ Convert between currencies:
37
+
38
+ twenty_dollars.convert_to("EUR")
39
+ => 15.00 EUR
40
+
41
+
42
+ Perform arithmetic operations (+, -, *, /) on different currencies
43
+
44
+ twenty_dollars + ten_eur
45
+ => 27.50 USD
46
+
47
+ ## Development
48
+
49
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
50
+
51
+ 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).
52
+
53
+ ## Contributing
54
+
55
+ Bug reports and pull requests are welcome on GitHub at https://github.com/davismattg/mgd_money.
56
+
57
+ ## License
58
+
59
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mgd_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__)
@@ -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
@@ -1,3 +1,5 @@
1
+ require "mgd_money/version"
2
+
1
3
  class MGDMoney
2
4
  include Comparable # for implementing custom comparing methods for MGDMoney objects
3
5
 
@@ -11,13 +13,17 @@ class MGDMoney
11
13
  attr_reader :amount, :currency # each MGDMoney object will have these attributes
12
14
 
13
15
  # instantiate new MGDMoney objects.
14
- # @param [Numeric] amount, the amount of the given currency
15
- # @param [String] currency, the user-defined currency string
16
+ # @param [Numeric] amount the amount of the given currency
17
+ # @param [String] currency the user-defined currency string
16
18
  # @return [MGDMoney] the resulting MGDMoney object
17
- # @example fifty_eur = MGDMoney.new(50, 'EUR') #=> 50 EUR
19
+ # @example fifty_eur = MGDMoney.new(50, 'EUR') => 50.00 EUR
18
20
  def initialize(amount, currency)
19
21
  unless amount.is_a?(Numeric) # the amount entered wasn't empty, but we still have to make sure it's a number
20
- raise UnknownObjectError, 'Amount must be a number'
22
+ raise InvalidDeclarationError, 'Amount must be a number'
23
+ end
24
+
25
+ unless currency.is_a?(String) # the currency entered wasn't a string
26
+ raise InvalidDeclarationError, 'Currency must be a string'
21
27
  end
22
28
 
23
29
  if currency.empty?
@@ -29,15 +35,33 @@ class MGDMoney
29
35
  end
30
36
 
31
37
  # configure the currency rates on the singleton class with respect to a base currency
32
- # @param [String] base_currency, the currency used to determine conversion rates
33
- # @param [Hash] conversion_factors, the conversion rates for supported currencies
38
+ # @param [String] base_currency the currency used to determine conversion rates
39
+ # @param [Hash] conversion_factors the conversion rates for supported currencies
34
40
  # @return [nil]
35
- # @example
36
- # MGDMoney.conversion_rates("EUR", {
37
- # "USD" => 1.11,
38
- # "Bitcoin" => 0.0047
39
- # })
41
+ # @example MGDMoney.conversion_rates("EUR", {
42
+ # "USD" => 1.11,
43
+ # "Bitcoin" => 0.0047
44
+ # })
40
45
  def self.conversion_rates(base_currency, conversion_factors)
46
+ unless base_currency.is_a?(String) # check the base currency is a string
47
+ raise InvalidConversionDeclarationError, 'Currency must be a string'
48
+ end
49
+
50
+ unless conversion_factors.is_a?(Hash) # check the rates were specified as a hash
51
+ raise InvalidConversionDeclarationError, 'Conversion rates not specified'
52
+ end
53
+
54
+ # check that the users supplied the currency as a string, and the conversion rate as a number
55
+ conversion_factors.each do |key, value|
56
+ unless key.is_a?(String)
57
+ raise InvalidConversionDeclarationError, 'Currency must be a string'
58
+ end
59
+
60
+ unless value.is_a?(Numeric)
61
+ raise InvalidConversionDeclarationError, 'Conversion factor must be a number'
62
+ end
63
+ end
64
+
41
65
  self.base_currency = base_currency
42
66
  self.conversion_factors = conversion_factors
43
67
  end
@@ -45,7 +69,7 @@ class MGDMoney
45
69
  # convert to a different currency, returning a new MGDMoney object.
46
70
  # requires both source and destination currency to be defined by
47
71
  # MGDMoney.conversion_rates (otherwise rate not known)
48
- # @param [String] currency, the desired currency after conversion
72
+ # @param [String] currency the desired currency after conversion
49
73
  # @return [MGDMoney] the new MGDMoney object representing the converted currency
50
74
  # @example fifty_eur.convert_to('USD') # => 55.50 USD
51
75
  def convert_to(currency)
@@ -99,7 +123,7 @@ class MGDMoney
99
123
 
100
124
  # convert the given number to its float representation.
101
125
  # this makes all the arithmetic possible
102
- # @param [Numeric] amount, the amount to convert
126
+ # @param [Numeric] amount the amount to convert
103
127
  # @return [BigDecimal]
104
128
  # @example convert_to_float(twenty_dollars.amount)
105
129
  def convert_to_float(amount)
@@ -111,7 +135,7 @@ class MGDMoney
111
135
  end
112
136
 
113
137
  # perform arithmetic operations in two different currencies
114
- # @param [MGDMoney] other_object, the MGDMoney object we're doing the operation with
138
+ # @param [MGDMoney] other_object the MGDMoney object we're doing the operation with
115
139
  # @return [MGDMoney]
116
140
  # @example fifty_eur + twenty_dollars = 68.02 EUR
117
141
  # @example fifty_eur / 2 = 25 EUR
@@ -151,7 +175,7 @@ class MGDMoney
151
175
 
152
176
  # compare different currencies (using Comparable)
153
177
  # in this case, we only care about comparing the amounts of each MGDMoney object
154
- # @param [MGDMoney] other_object, the object to compare to
178
+ # @param [MGDMoney] other_object the object to compare to
155
179
  # @return [FixNum]
156
180
  # @example twenty_dollars == MGDMoney.new(20, 'USD') # => true
157
181
  # @example twenty_dollars == MGDMoney.new(30, 'USD') # => false
@@ -160,7 +184,7 @@ class MGDMoney
160
184
  def <=>(other_object)
161
185
  if other_object.is_a?(MGDMoney)
162
186
  other_object = other_object.convert_to(currency)
163
- amount <=> other_object.amount
187
+ convert_to_float(amount) <=> convert_to_float(other_object.amount) # convert to float so we're only comparing up to 2 decimal places
164
188
  else
165
189
  raise UnknownObjectError, 'Unknown destination object type (must be type MGDMoney)'
166
190
  end
@@ -175,4 +199,10 @@ class MGDMoney
175
199
 
176
200
  class UnsupportedOperationError < StandardError
177
201
  end
178
- end
202
+
203
+ class InvalidDeclarationError < StandardError
204
+ end
205
+
206
+ class InvalidConversionDeclarationError < StandardError
207
+ end
208
+ end
@@ -0,0 +1,3 @@
1
+ module MgdMoney
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "mgd_money/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mgd_money"
8
+ spec.version = MgdMoney::VERSION
9
+ spec.authors = ["Matt Davis"]
10
+ spec.email = ["davismattg@gmail.com"]
11
+
12
+ spec.summary = "MGDMoney"
13
+ spec.description = "A simple gem to enable conversion between different currencies"
14
+ spec.homepage = "http://www.mattgdavis.com"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.15"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "minitest", "~> 5.0"
27
+ end
metadata CHANGED
@@ -1,23 +1,75 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mgd_money
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Davis
8
8
  autorequire:
9
- bindir: bin
9
+ bindir: exe
10
10
  cert_chain: []
11
- date: 2018-04-06 00:00:00.000000000 Z
12
- dependencies: []
13
- description: A simple gem to enable conversion and arithmetic operations between different
14
- currencies
15
- email: davismattg@gmail.com
11
+ date: 2018-04-18 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.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: A simple gem to enable conversion between different currencies
56
+ email:
57
+ - davismattg@gmail.com
16
58
  executables: []
17
59
  extensions: []
18
60
  extra_rdoc_files: []
19
61
  files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
20
70
  - lib/mgd_money.rb
71
+ - lib/mgd_money/version.rb
72
+ - mgd_money.gemspec
21
73
  homepage: http://www.mattgdavis.com
22
74
  licenses:
23
75
  - MIT
@@ -28,17 +80,17 @@ require_paths:
28
80
  - lib
29
81
  required_ruby_version: !ruby/object:Gem::Requirement
30
82
  requirements:
31
- - - '>='
83
+ - - ">="
32
84
  - !ruby/object:Gem::Version
33
85
  version: '0'
34
86
  required_rubygems_version: !ruby/object:Gem::Requirement
35
87
  requirements:
36
- - - '>='
88
+ - - ">="
37
89
  - !ruby/object:Gem::Version
38
90
  version: '0'
39
91
  requirements: []
40
92
  rubyforge_project:
41
- rubygems_version: 2.0.14.1
93
+ rubygems_version: 2.6.11
42
94
  signing_key:
43
95
  specification_version: 4
44
96
  summary: MGDMoney