ruby_money 0.0.2

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
+ SHA256:
3
+ metadata.gz: 78cc88a83224ea863f04a2c985dee965b1e81a1fbff7f9cc2dfdb27e58231012
4
+ data.tar.gz: 918ba1846aacb7e6a53b04c30fce3b7da8e2c5f9739e21cb0aaaebf59ab4b4be
5
+ SHA512:
6
+ metadata.gz: 60f11c93a7b1976c249fac16a2844c2fb7401d7974ab19139dbf2ace5845dc3696b12ef8c46dc30ce59fa60ab74c55e15aa11093289e9f4bcf02973fcd4f5c8c
7
+ data.tar.gz: 31db2bcd8b58f0daf948a0d33aab45d6f2ff531575f99b448f8c5c20ffc4eb3e43afc2c3656a0f4447b1b8b7021bf5730bd17ec629d8481297036efaa4834e4c
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.1.2
5
+ before_install: gem install bundler -v 1.16.0
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 money.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ruby_money (0.0.2)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (10.5.0)
11
+ rspec (3.7.0)
12
+ rspec-core (~> 3.7.0)
13
+ rspec-expectations (~> 3.7.0)
14
+ rspec-mocks (~> 3.7.0)
15
+ rspec-core (3.7.1)
16
+ rspec-support (~> 3.7.0)
17
+ rspec-expectations (3.7.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.7.0)
20
+ rspec-mocks (3.7.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.7.0)
23
+ rspec-support (3.7.1)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 1.16)
30
+ rake (~> 10.0)
31
+ rspec (~> 3.0)
32
+ ruby_money!
33
+
34
+ BUNDLED WITH
35
+ 1.16.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Nicolas Tonnelier
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,82 @@
1
+ # Money
2
+
3
+ Simple library for easy operations involving money: Currency conversion, comparisons, adition, substraction, multiplications, divisions.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'money', :git =>'git://github.com/natonnelier/money.git'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install money
20
+
21
+ ## Usage
22
+
23
+ ### Configure the currency rates with respect to a base currency (here EUR):
24
+
25
+ Money::Money.conversion_rates('EUR', {
26
+ 'USD' => 1.11,
27
+ 'Bitcoin' => 0.0047
28
+ })
29
+
30
+ ### Instantiate money objects:
31
+
32
+ fifty_eur = Money::Money.new(50, 'EUR')
33
+
34
+ ### Get amount and currency:
35
+
36
+ fifty_eur.amount # => 50
37
+ fifty_eur.currency # => "EUR"
38
+ fifty_eur.inspect # => "50.00 EUR"
39
+
40
+ ### Convert to a different currency (should return a Money instance, not a String:
41
+
42
+ fifty_eur.convert_to('USD') # => 55.50 USD
43
+
44
+ ### Perform operations in different currencies:
45
+
46
+ twenty_dollars = Money::Money.new(20, 'USD')
47
+
48
+ ### Arithmetics:
49
+
50
+ fifty_eur + twenty_dollars # => 68.02 EUR
51
+ fifty_eur - twenty_dollars # => 31.98 EUR
52
+ fifty_eur / 2 # => 25 EUR
53
+ twenty_dollars * 3 # => 60 USD
54
+
55
+ ### Comparisons (also in different currencies):
56
+
57
+ twenty_dollars == Money::Money.new(20, 'USD') # => true
58
+ twenty_dollars == Money::Money.new(30, 'USD') # => false
59
+
60
+ fifty_eur_in_usd = fifty_eur.convert_to('USD')
61
+ fifty_eur_in_usd == fifty_eur # => true
62
+
63
+ twenty_dollars > Money::Money.new(5, 'USD') # => true
64
+ twenty_dollars < fifty_eur # => true
65
+
66
+ ## Development
67
+
68
+ 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
+
70
+ 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).
71
+
72
+ ## Contributing
73
+
74
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/money. 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.
75
+
76
+ ## License
77
+
78
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
79
+
80
+ ## Code of Conduct
81
+
82
+ Everyone interacting in the Money project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/money/blob/master/CODE_OF_CONDUCT.md).
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 "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/lib/ruby_money.rb ADDED
@@ -0,0 +1,2 @@
1
+ require "ruby_money/version"
2
+ require "ruby_money/money"
@@ -0,0 +1,84 @@
1
+ module Money
2
+ class Money
3
+ attr_accessor :amount, :currency
4
+
5
+ class << self
6
+ attr_accessor :default_currency, :rates
7
+
8
+ def conversion_rates(default_currency, rates)
9
+ @default_currency = default_currency
10
+ @rates = rates.merge({ default_currency => 1 })
11
+ end
12
+ end
13
+
14
+ def initialize(amount, currency)
15
+ @amount = Float(amount)
16
+ @currency = currency
17
+
18
+ validate_currency!(currency)
19
+ end
20
+
21
+ def inspect
22
+ "#{sprintf('%.2f', @amount)} #{@currency}"
23
+ end
24
+
25
+ def convert_to currency
26
+ validate_currency!(currency)
27
+ amount = (amount_in_default_currency * rates[currency]).round(4)
28
+ self.class.new(amount, currency)
29
+ end
30
+
31
+ def + (other)
32
+ other_amount = other_to_currency(other).amount
33
+ self.class.new(@amount + other_amount, @currency)
34
+ end
35
+
36
+ def - (other)
37
+ other_amount = other_to_currency(other).amount
38
+ self.class.new(@amount - other_amount, @currency)
39
+ end
40
+
41
+ def / (num)
42
+ self.class.new(@amount / num, @currency)
43
+ end
44
+
45
+ def * (num)
46
+ self.class.new(@amount * num, @currency)
47
+ end
48
+
49
+ def == (other)
50
+ other_to_currency(other).amount == @amount
51
+ end
52
+
53
+ def < (other)
54
+ @amount < other_to_currency(other).amount
55
+ end
56
+
57
+ def > (other)
58
+ @amount > other_to_currency(other).amount
59
+ end
60
+
61
+ private
62
+ def other_to_currency other
63
+ other.currency == @currency ? other : other.convert_to(@currency)
64
+ end
65
+
66
+ def amount_in_default_currency
67
+ return @amount if @currency == default_currency
68
+ @amount / rates[@currency]
69
+ end
70
+
71
+ def rates
72
+ @rates ||= self.class.rates
73
+ end
74
+
75
+ def default_currency
76
+ @default_currency ||= self.class.default_currency
77
+ end
78
+
79
+ def validate_currency! currency
80
+ raise ArgumentError, "#{currency} is no a valid currency" if rates[currency].nil?
81
+ end
82
+
83
+ end
84
+ end
@@ -0,0 +1,3 @@
1
+ module Money
2
+ VERSION = "0.0.2"
3
+ end
Binary file
@@ -0,0 +1,27 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "ruby_money/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ruby_money"
8
+ spec.version = Money::VERSION
9
+ spec.authors = ["Nicolas Tonnelier"]
10
+ spec.email = ["na.tonnelier@gmail.com"]
11
+
12
+ spec.summary = %q{Simple handling of money and currencies.}
13
+ spec.description = %q{Make operations and exchange between existent currencies using Money objects.}
14
+ spec.homepage = "http://rubygems.org/gems/natonnelier/money"
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.16"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_money
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Nicolas Tonnelier
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-05-17 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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
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.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Make operations and exchange between existent currencies using Money
56
+ objects.
57
+ email:
58
+ - na.tonnelier@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - Gemfile.lock
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - lib/ruby_money.rb
74
+ - lib/ruby_money/money.rb
75
+ - lib/ruby_money/version.rb
76
+ - ruby_money-0.0.1.gem
77
+ - ruby_money.gemspec
78
+ homepage: http://rubygems.org/gems/natonnelier/money
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.7.4
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Simple handling of money and currencies.
102
+ test_files: []