convers_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: 3d2d29a6b13b0fd0309580eb9c921b8b7db21de0
4
+ data.tar.gz: 315982a2c9ba74772ec9e2384bea161067680a50
5
+ SHA512:
6
+ metadata.gz: 54ce1a87108910387f70194b5db778887c40f69754841ca5d4beaf8ea0ef61cabf3ce4c482199bf885b0adc199ae0c84767bb49043f7b03cc2ff884599cf87f6
7
+ data.tar.gz: 6bc6cc2d7af62f555e9472abd122a0ef4027c021a146de05e43af05b92b0a1f5df6eb849f756b336dcd557cfc1e22aff3532d24c373dfb3df29197f5f6471ba4
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --require spec_helper
3
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.7
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in convers_money.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,70 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec features) \
6
+ # .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
7
+
8
+ ## Note: if you are using the `directories` clause above and you are not
9
+ ## watching the project directory ('.'), then you will want to move
10
+ ## the Guardfile to a watched dir and symlink it back, e.g.
11
+ #
12
+ # $ mkdir config
13
+ # $ mv Guardfile config/
14
+ # $ ln -s config/Guardfile .
15
+ #
16
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
17
+
18
+ # Note: The cmd option is now required due to the increasing number of ways
19
+ # rspec may be run, below are examples of the most common uses.
20
+ # * bundler: 'bundle exec rspec'
21
+ # * bundler binstubs: 'bin/rspec'
22
+ # * spring: 'bin/rspec' (This will use spring if running and you have
23
+ # installed the spring binstubs per the docs)
24
+ # * zeus: 'zeus rspec' (requires the server to be started separately)
25
+ # * 'just' rspec: 'rspec'
26
+
27
+ guard :rspec, cmd: "bundle exec rspec" do
28
+ require "guard/rspec/dsl"
29
+ dsl = Guard::RSpec::Dsl.new(self)
30
+
31
+ # Feel free to open issues for suggestions and improvements
32
+
33
+ # RSpec files
34
+ rspec = dsl.rspec
35
+ watch(rspec.spec_helper) { rspec.spec_dir }
36
+ watch(rspec.spec_support) { rspec.spec_dir }
37
+ watch(rspec.spec_files)
38
+
39
+ # Ruby files
40
+ ruby = dsl.ruby
41
+ dsl.watch_spec_files_for(ruby.lib_files)
42
+
43
+ # Rails files
44
+ rails = dsl.rails(view_extensions: %w(erb haml slim))
45
+ dsl.watch_spec_files_for(rails.app_files)
46
+ dsl.watch_spec_files_for(rails.views)
47
+
48
+ watch(rails.controllers) do |m|
49
+ [
50
+ rspec.spec.call("routing/#{m[1]}_routing"),
51
+ rspec.spec.call("controllers/#{m[1]}_controller"),
52
+ rspec.spec.call("acceptance/#{m[1]}")
53
+ ]
54
+ end
55
+
56
+ # Rails config changes
57
+ watch(rails.spec_helper) { rspec.spec_dir }
58
+ watch(rails.routes) { "#{rspec.spec_dir}/routing" }
59
+ watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }
60
+
61
+ # Capybara features specs
62
+ watch(rails.view_dirs) { |m| rspec.spec.call("features/#{m[1]}") }
63
+ watch(rails.layouts) { |m| rspec.spec.call("features/#{m[1]}") }
64
+
65
+ # Turnip features and steps
66
+ watch(%r{^spec/acceptance/(.+)\.feature$})
67
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
68
+ Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
69
+ end
70
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Toño Serna
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,86 @@
1
+ # ConversMoney
2
+ ![ConversMoney](currency_convers_nobg.png)
3
+
4
+ ## Welcome to ConversMoney gem
5
+
6
+ With this gem you can convert different currencies just giving the conversion rates with the fixed amount (main based 1 currecy value 'EUR' in the example below) ant the exchange rate.
7
+
8
+ For more options please check the examples.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'convers_money'
16
+ ```
17
+
18
+ And then execute:
19
+ ```
20
+ $ bundle
21
+ ```
22
+ Or install it yourself as:
23
+ ```
24
+ $ gem install convers_money
25
+ ```
26
+ ## Usage
27
+
28
+
29
+ ### Configure the currency rates with respect to a base currency (here EUR):
30
+ ```ruby
31
+ Money.conversion_rates('EUR', {
32
+ 'USD' => 1.11,
33
+ 'Bitcoin' => 0.0047
34
+ })
35
+ ```
36
+ ### Instantiate money objects:
37
+ ```ruby
38
+ fifty_eur = Money.new(50, 'EUR')
39
+ ```
40
+ ### Get amount and currency:
41
+ ```ruby
42
+ fifty_eur.amount # => 50
43
+ fifty_eur.currency # => "EUR"
44
+ fifty_eur.inspect # => "50.00 EUR"
45
+ ```
46
+ ### Convert to a different currency (should return a Money
47
+ instance, not a String):
48
+ ```ruby
49
+ fifty_eur.convert_to('USD') # => 55.50 USD
50
+ ```
51
+ ### Perform operations in different currencies:
52
+ ```ruby
53
+ twenty_dollars = Money.new(20, 'USD')
54
+ ```
55
+ ### Arithmetics:
56
+ ```ruby
57
+ fifty_eur + twenty_dollars # => 68.02 EUR
58
+ fifty_eur - twenty_dollars # => 31.98 EUR
59
+ fifty_eur / 2 # => 25 EUR
60
+ twenty_dollars * 3 # => 60 USD
61
+ ```
62
+ ### Comparisons (also in different currencies):
63
+ ```ruby
64
+ twenty_dollars == Money.new(20, 'USD') # => true
65
+ twenty_dollars == Money.new(30, 'USD') # => false
66
+
67
+ fifty_eur_in_usd = fifty_eur.convert_to('USD')
68
+ fifty_eur_in_usd == fifty_eur # => true
69
+
70
+ twenty_dollars > Money.new(5, 'USD') # => true
71
+ twenty_dollars < fifty_eur # => true
72
+ ```
73
+ ### Development
74
+
75
+ 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.
76
+
77
+ 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).
78
+
79
+ ### Contributing
80
+
81
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nelantone/convers_money.
82
+
83
+
84
+ ### License
85
+
86
+ 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 "convers_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
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
Binary file
@@ -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 'convers_money/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'convers_money'
8
+ spec.version = ConversMoney::VERSION
9
+ spec.authors = ['Toño Serna']
10
+ spec.email = ['antonelo@gmail.com']
11
+
12
+ spec.summary = 'Money conversor'
13
+ spec.description = "ConversMoney it's a Money Conversor given by fixed currency and change rates of your choice."
14
+ spec.homepage = 'https://github.com/nelantone/convers_money'
15
+ spec.license = 'MIT'
16
+
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.13'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'rspec', '~> 3.5', '>= 3.5'
28
+ spec.add_development_dependency 'guard', '~> 2.14', '>= 2.14.1'
29
+ spec.add_development_dependency 'guard-rspec', '~> 0', '~> 0'
30
+ end
Binary file
@@ -0,0 +1,3 @@
1
+ class ConversMoney
2
+ VERSION = '0.1.1'
3
+ end
@@ -0,0 +1,95 @@
1
+ require 'convers_money/version'
2
+
3
+ # ConversMoney it's a Money Conversor given by fixed currency and change rates of your choice.
4
+ class ConversMoney
5
+ attr_accessor :amount, :currency, :second_amount
6
+
7
+ def initialize(amount, currency)
8
+ @amount = amount
9
+ @currency = currency.upcase
10
+ ConversMoney.validate!
11
+ end
12
+
13
+ def self.conversion_rates(currency, conversion_rates)
14
+ @conversion_rates ||= {}
15
+ conversion_rates = Hash[conversion_rates.map { |k, v| [k.upcase, v] }]
16
+ @conversion_rates.merge!(currency.upcase => conversion_rates)
17
+ end
18
+
19
+ def self.validate!
20
+ raise 'not configured' unless @conversion_rates&.any?
21
+ end
22
+
23
+ def self.get_rate!(from_currency, to_currency)
24
+ rates = @conversion_rates[from_currency]
25
+ raise "no convention rates from #{from_currency}" unless rates
26
+ rate = rates[to_currency]
27
+ raise "no conversion rates to #{to_currency}" unless rate
28
+ rate
29
+ end
30
+
31
+ def self.convert!(amount, from_currency, to_currency)
32
+ get_rate!(from_currency, to_currency) * amount
33
+ end
34
+
35
+ def convert_to(currency)
36
+ if self.currency.eql?(currency)
37
+ self
38
+ else
39
+ converted_amount = ConversMoney.convert!(@amount, @currency, currency)
40
+ amount_str = format('%.2f', converted_amount)
41
+ ConversMoney.new(amount_str.to_f, currency)
42
+ end
43
+ end
44
+
45
+ def inspect
46
+ "#{format('%.2f', @amount)} #{@currency}"
47
+ end
48
+
49
+ def +(other)
50
+ total_amount = amount + other.convert_to(currency).amount
51
+ ConversMoney.new(format('%.2f', total_amount), currency)
52
+ end
53
+
54
+ def -(other)
55
+ total_amount = amount - other.convert_to(currency).amount
56
+ ConversMoney.new(format('%.2f', total_amount), currency)
57
+ end
58
+
59
+ def *(other)
60
+ total_amount = amount * other.convert_to(currency).amount
61
+ ConversMoney.new(format('%.2f', total_amount), currency)
62
+ end
63
+
64
+ def /(other)
65
+ total_amount = amount / other.convert_to(currency).amount
66
+ ConversMoney.new(format('%.2f', total_amount), currency)
67
+ end
68
+
69
+ def ==(other)
70
+ amount == other.convert_to(currency).amount
71
+ end
72
+
73
+ def >(other)
74
+ amount > other.convert_to(currency).amount
75
+ end
76
+
77
+ def <(other)
78
+ amount < other.convert_to(currency).amount
79
+ end
80
+
81
+ def self.checking_fixed_currency(other, currency)
82
+ if other.currency.eql?(currency)
83
+ other
84
+ else
85
+ conversion_rates_access
86
+ other.convert_to(currency)
87
+ end
88
+ end
89
+
90
+ def self.conversion_rates_access
91
+ @conversion_rates
92
+ end
93
+
94
+ private_class_method :conversion_rates_access
95
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: convers_money
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Toño Serna
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-02-01 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.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
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.5'
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '3.5'
51
+ type: :development
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - "~>"
56
+ - !ruby/object:Gem::Version
57
+ version: '3.5'
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '3.5'
61
+ - !ruby/object:Gem::Dependency
62
+ name: guard
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '2.14'
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 2.14.1
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - "~>"
76
+ - !ruby/object:Gem::Version
77
+ version: '2.14'
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: 2.14.1
81
+ - !ruby/object:Gem::Dependency
82
+ name: guard-rspec
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - "~>"
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ description: ConversMoney it's a Money Conversor given by fixed currency and change
96
+ rates of your choice.
97
+ email:
98
+ - antonelo@gmail.com
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - ".gitignore"
104
+ - ".rspec"
105
+ - ".travis.yml"
106
+ - Gemfile
107
+ - Guardfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - bin/console
112
+ - bin/setup
113
+ - convers_money-0.1.0.gem
114
+ - convers_money.gemspec
115
+ - currency_convers_nobg.png
116
+ - lib/convers_money.rb
117
+ - lib/convers_money/version.rb
118
+ homepage: https://github.com/nelantone/convers_money
119
+ licenses:
120
+ - MIT
121
+ metadata: {}
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 2.5.1
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: Money conversor
142
+ test_files: []