currency_shushugah 2.0.0 → 2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 025a361a07ad5469e9f3b19f285c1882d4d2b510
4
- data.tar.gz: b0a579adf6581e8926192437148bf965d0e400a3
3
+ metadata.gz: 66ba837fb8fa313e4803fd7250c7e86e9355800d
4
+ data.tar.gz: 75382370b6dd872c6dfe041010ab8af15336370c
5
5
  SHA512:
6
- metadata.gz: c3c2f0ecb2deb87d8a6abc4910b6d20bb04c38a45a0d0cd6e9d1b19db6ee22b527b8f01b34574460f4dc7c4d6008cb2c7230e30a13c850338e867b3a00b64be2
7
- data.tar.gz: 76e6665f5ee08b7f089aae84707d4f5079fa7cb73ecd2595d435386df310d500457f4ec31c0b77bd68c751b3d682622a50438b96f16564653b622e59d0948a10
6
+ metadata.gz: d617e7586263f2b16989368140477e460b3780feae896b3a81f9afe642792275bad57937e7b5bb036cc7c92b1d2c400c48bf09e3ad9a978893e78618e62d9973
7
+ data.tar.gz: c93217f6ab36348e1c7aba00c9fdd4ff075da140e672ea49381db77906d16e37fa4fbf737e0d879d97df81082cde2deb87365484248501f3b735e718c9f06fc7
data/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2016 yonatan miller
3
+ Copyright (c) 2017 Yonatan Miller
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,9 +1,3 @@
1
- # CurrencyShushugah
2
-
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/currency_shushugah`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
6
-
7
1
  ## Installation
8
2
 
9
3
  Add this line to your application's Gemfile:
@@ -16,7 +10,7 @@ And then execute:
16
10
 
17
11
  $ bundle
18
12
 
19
- Or install it yourself as:
13
+ Or install it yourself via:
20
14
 
21
15
  $ gem install currency_shushugah
22
16
 
@@ -24,63 +18,62 @@ Or install it yourself as:
24
18
 
25
19
  ```
26
20
  # Configure the currency rates with respect to a base currency (here EUR):
27
-
21
+
28
22
  Money.conversion_rates('EUR', {
29
23
  'USD' => 1.11,
30
24
  'Bitcoin' => 0.0047,
31
25
  'CAD' => 1.39
32
26
  })
33
-
34
- # Instantiate money objects:
35
-
27
+
28
+ # Instantiate Money objects:
29
+
36
30
  fifty_eur = Money.new(50, 'EUR')
37
-
31
+
38
32
  # Get amount and currency:
39
-
33
+
40
34
  fifty_eur.amount # => 50
41
- fifty_eur.currency # => "EUR"
42
- fifty_eur.inspect # => "50.00 EUR"
43
-
44
- # Convert to a different currency (should return a Money
45
- # instance, not a String):
46
-
35
+ fifty_eur.currency # => 'EUR'
36
+ fifty_eur.inspect # => '50.00 EUR'
37
+
38
+ # Convert to a different currency (returns new Money instance)
39
+
47
40
  fifty_eur.convert_to('USD') # => 55.50 USD
48
-
41
+ fifty_eur.convert_to('USD').currency # => 'USD'
42
+
49
43
  # Perform operations in different currencies:
50
-
44
+
51
45
  twenty_dollars = Money.new(20, 'USD')
52
-
46
+
53
47
  # Arithmetics:
54
-
48
+
55
49
  fifty_eur + twenty_dollars # => 68.02 EUR
56
50
  fifty_eur - twenty_dollars # => 31.98 EUR
57
51
  fifty_eur / 2 # => 25 EUR
58
52
  twenty_dollars * 3 # => 60 USD
59
-
60
- # Comparisons (also in different currencies):
61
-
53
+ 3 * twenty_dollars # => 60 USD
54
+
55
+ # Comparisons:
56
+
62
57
  twenty_dollars == Money.new(20, 'USD') # => true
63
58
  twenty_dollars == Money.new(30, 'USD') # => false
64
-
59
+
65
60
  fifty_eur_in_usd = fifty_eur.convert_to('USD')
66
61
  fifty_eur_in_usd == fifty_eur # => true
67
-
62
+
68
63
  twenty_dollars > Money.new(5, 'USD') # => true
69
64
  twenty_dollars < fifty_eur # => true
70
65
  ```
71
66
 
72
67
  ## Development
73
68
 
74
- 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.
69
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
75
70
 
76
71
  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).
77
72
 
78
73
  ## Contributing
79
74
 
80
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/currency_shushugah. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
81
-
75
+ Bug reports, feedback and pull requests are welcome on GitHub at https://github.com/shushugah/currency_shushugah. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
82
76
 
83
77
  ## License
84
78
 
85
79
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
86
-
data/bin/console CHANGED
@@ -1,14 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "currency_shushugah"
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"
3
+ require 'bundler/setup'
4
+ require 'currency_shushugah'
5
+ require 'irb'
14
6
  IRB.start
data/bin/setup CHANGED
@@ -4,5 +4,3 @@ IFS=$'\n\t'
4
4
  set -vx
5
5
 
6
6
  bundle install
7
-
8
- # Do any other automated setup that you need to do here
@@ -0,0 +1,4 @@
1
+ module CurrencyShushugah
2
+ class UnsupportedCurrency < StandardError; end
3
+ class DivisionByZero < StandardError; end
4
+ end
@@ -1,3 +1,3 @@
1
- class CurrencyShushugah
2
- VERSION = '2.0.0'.freeze
1
+ module CurrencyShushugah
2
+ VERSION = '2.0.1'.freeze
3
3
  end
@@ -1,74 +1,77 @@
1
1
  require 'currency_shushugah/version'
2
-
3
- # Understands currency and quantity
4
- class Money
5
- include Comparable
6
-
7
- attr_reader :quantity, :currency
8
- protected :currency, :quantity
9
-
10
- def self.conversion_rates(base_currency, hash_rates)
11
- @rates = hash_rates.merge(base_currency => 1)
12
- end
13
-
14
- def initialize(quantity, currency)
15
- raise StandardError.new('Not supported currency') unless rates[currency]
16
- @quantity = quantity
17
- @currency = currency
18
- end
19
-
20
- def <=>(other)
21
- return unless other.class == self.class
22
- quantity <=> other.convert_to(currency).quantity
23
- end
24
-
25
- def inspect
26
- "#{'%0.2f' % quantity} #{currency}"
27
- end
28
-
29
- def convert_to(other_currency)
30
- raise StandardError.new('InvalidCurrency') unless rates[other_currency]
31
- return self if currency == other_currency
32
- new_quantity = @quantity * rates[other_currency] / rates[currency]
33
- new_money(new_quantity, other_currency)
34
- end
35
-
36
- def +(other)
37
- new_money(quantity + other.convert_to(currency).quantity)
38
- end
39
-
40
- def -(other)
41
- self + -other
42
- end
43
-
44
- def -@
45
- new_money(-quantity)
46
- end
47
-
48
- def coerce(other)
49
- return self, other
50
- end
51
-
52
- def *(scalar)
53
- new_money(quantity * scalar)
54
- end
55
-
56
- def /(scalar)
57
- raise StandardError.new 'InvalidDivision' if scalar.zero?
58
- self * (1.0 / scalar)
59
- end
60
-
61
- def new_money(quantity, currency = @currency)
62
- Money.send :new, quantity.round(2), currency
63
- end
64
-
65
- def self.rates
66
- Money.instance_variable_get(:@rates)
67
- end
68
-
69
- private
70
-
71
- def rates
72
- self.class.rates
2
+ require 'currency_shushugah/errors'
3
+
4
+ module CurrencyShushugah
5
+ # Understands currency and quantity
6
+ class Money
7
+ include Comparable
8
+
9
+ attr_reader :quantity, :currency
10
+ protected :currency, :quantity
11
+
12
+ def self.conversion_rates(base_currency, hash_rates)
13
+ @rates = hash_rates.merge(base_currency => 1)
14
+ end
15
+
16
+ def initialize(quantity, currency)
17
+ raise UnsupportedCurrency unless rates[currency]
18
+ @quantity = quantity
19
+ @currency = currency
20
+ end
21
+
22
+ def <=>(other)
23
+ return unless other.class == self.class
24
+ quantity <=> other.convert_to(currency).quantity
25
+ end
26
+
27
+ def inspect
28
+ "#{'%0.2f' % quantity} #{currency}"
29
+ end
30
+
31
+ def convert_to(other_currency)
32
+ raise UnsupportedCurrency unless rates[other_currency]
33
+ return self if currency == other_currency
34
+ new_quantity = @quantity * rates[other_currency] / rates[currency]
35
+ new_money(new_quantity, other_currency)
36
+ end
37
+
38
+ def +(other)
39
+ new_money(quantity + other.convert_to(currency).quantity)
40
+ end
41
+
42
+ def -(other)
43
+ self + -other
44
+ end
45
+
46
+ def -@
47
+ new_money(-quantity)
48
+ end
49
+
50
+ def coerce(other)
51
+ [self, other]
52
+ end
53
+
54
+ def *(other)
55
+ new_money(quantity * other)
56
+ end
57
+
58
+ def /(other)
59
+ raise DivisionByZero if other.zero?
60
+ self * (1.0 / other)
61
+ end
62
+
63
+ def new_money(quantity, currency = @currency)
64
+ Money.send :new, quantity.round(2), currency
65
+ end
66
+
67
+ def self.rates
68
+ Money.instance_variable_get(:@rates)
69
+ end
70
+
71
+ private
72
+
73
+ def rates
74
+ self.class.rates
75
+ end
73
76
  end
74
77
  end
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: currency_shushugah
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yonatan Miller
8
8
  autorequire:
9
- bindir: exe
9
+ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2017-07-29 00:00:00.000000000 Z
12
12
  dependencies:
@@ -59,22 +59,13 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
- - ".gitignore"
63
- - ".rspec"
64
- - ".travis.yml"
65
- - CODE_OF_CONDUCT.md
66
- - Gemfile
67
62
  - LICENSE.txt
68
63
  - README.md
69
- - Rakefile
70
64
  - bin/console
71
65
  - bin/setup
72
- - currency_shushugah.gemspec
73
66
  - lib/currency_shushugah.rb
67
+ - lib/currency_shushugah/errors.rb
74
68
  - lib/currency_shushugah/version.rb
75
- - tasks/Rakefile
76
- - tasks/debug.rake
77
- - tasks/rspec.rake
78
69
  homepage: https://github.com/shushugah/currency_shushugah
79
70
  licenses:
80
71
  - MIT
data/.gitignore DELETED
@@ -1,9 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /Gemfile.lock
4
- /_yardoc/
5
- /coverage/
6
- /doc/
7
- /pkg/
8
- /spec/reports/
9
- /tmp/
data/.rspec DELETED
@@ -1,2 +0,0 @@
1
- --format documentation
2
- --color
data/.travis.yml DELETED
@@ -1,5 +0,0 @@
1
- sudo: false
2
- language: ruby
3
- rvm:
4
- - 2.3.1
5
- before_install: gem install bundler -v 1.12.5
data/CODE_OF_CONDUCT.md DELETED
@@ -1,49 +0,0 @@
1
- # Contributor Code of Conduct
2
-
3
- As contributors and maintainers of this project, and in the interest of
4
- fostering an open and welcoming community, we pledge to respect all people who
5
- contribute through reporting issues, posting feature requests, updating
6
- documentation, submitting pull requests or patches, and other activities.
7
-
8
- We are committed to making participation in this project a harassment-free
9
- experience for everyone, regardless of level of experience, gender, gender
10
- identity and expression, sexual orientation, disability, personal appearance,
11
- body size, race, ethnicity, age, religion, or nationality.
12
-
13
- Examples of unacceptable behavior by participants include:
14
-
15
- * The use of sexualized language or imagery
16
- * Personal attacks
17
- * Trolling or insulting/derogatory comments
18
- * Public or private harassment
19
- * Publishing other's private information, such as physical or electronic
20
- addresses, without explicit permission
21
- * Other unethical or unprofessional conduct
22
-
23
- Project maintainers have the right and responsibility to remove, edit, or
24
- reject comments, commits, code, wiki edits, issues, and other contributions
25
- that are not aligned to this Code of Conduct, or to ban temporarily or
26
- permanently any contributor for other behaviors that they deem inappropriate,
27
- threatening, offensive, or harmful.
28
-
29
- By adopting this Code of Conduct, project maintainers commit themselves to
30
- fairly and consistently applying these principles to every aspect of managing
31
- this project. Project maintainers who do not follow or enforce the Code of
32
- Conduct may be permanently removed from the project team.
33
-
34
- This code of conduct applies both within project spaces and in public spaces
35
- when an individual is representing the project or its community.
36
-
37
- Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
- reported by contacting a project maintainer at brombacher1@gmail.com. All
39
- complaints will be reviewed and investigated and will result in a response that
40
- is deemed necessary and appropriate to the circumstances. Maintainers are
41
- obligated to maintain confidentiality with regard to the reporter of an
42
- incident.
43
-
44
- This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
- version 1.3.0, available at
46
- [http://contributor-covenant.org/version/1/3/0/][version]
47
-
48
- [homepage]: http://contributor-covenant.org
49
- [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile DELETED
@@ -1,4 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in currency_shushugah.gemspec
4
- gemspec
data/Rakefile DELETED
@@ -1,5 +0,0 @@
1
- require 'bundler/gem_tasks'
2
-
3
- Dir.glob('tasks/**/*.rake').each(&method(:import))
4
-
5
- task default: :spec
@@ -1,23 +0,0 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'currency_shushugah/version'
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = "currency_shushugah"
8
- spec.version = CurrencyShushugah::VERSION
9
- spec.authors = ["Yonatan Miller"]
10
- spec.email = ["brombacher1@gmail.com"]
11
-
12
- spec.summary = "Converts currencies and accepts conversion rates"
13
- spec.homepage = "https://github.com/shushugah/currency_shushugah"
14
- spec.license = "MIT"
15
- spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
- spec.bindir = "exe"
17
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
- spec.require_paths = ["lib"]
19
-
20
- spec.add_development_dependency "bundler", "~> 1.15"
21
- spec.add_development_dependency "rake", "~> 10.0"
22
- spec.add_development_dependency "rspec", "~> 3.0"
23
- end
data/tasks/Rakefile DELETED
@@ -1,3 +0,0 @@
1
- require 'rspec/core/rake_task'
2
-
3
- RSpec::Core::RakeTask.new(:spec)
data/tasks/debug.rake DELETED
@@ -1,4 +0,0 @@
1
- desc 'Open an irb session preloaded with this library'
2
- task :console do
3
- sh 'irb -rubygems -I lib -r currency_shushugah.rb'
4
- end
data/tasks/rspec.rake DELETED
@@ -1,3 +0,0 @@
1
- require 'rspec/core/rake_task'
2
-
3
- RSpec::Core::RakeTask.new(:spec)