money-exercise 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 94a8116cb8c6592c070d68d273f3c3609c7e251d
4
+ data.tar.gz: 51c0319a6f484667979653f4660a801f52fe2fc9
5
+ SHA512:
6
+ metadata.gz: 9ad61146e01168f242dccc775c1499a2ed78eed9b190d3280e0da9ac3bd445a12c82c697ccc4d1a55a4cb2331389e2063cfaaa4c94b7501cc03f62e2866e7d9f
7
+ data.tar.gz: 774804a84048a815269ea10a9c52a887224b54f3dac12fea023a1750e2bba4f2cf8a3509590341f9f69ca70258b8f47e77a7c04fa620d57d10d55a7dc7dbc3c1
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in money.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2018 Geronimo Diaz
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,30 @@
1
+ # Money
2
+
3
+ Money exercise gem
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'money'
10
+ ```
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install money
19
+
20
+ ## Usage
21
+
22
+ Just an exercise
23
+
24
+ ## Contributing
25
+
26
+ 1. Fork it ( https://github.com/[my-github-username]/money/fork )
27
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
28
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
29
+ 4. Push to the branch (`git push origin my-new-feature`)
30
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,19 @@
1
+ require "money/version"
2
+ require "money/config"
3
+ require "money/base"
4
+
5
+ module Money
6
+ class << self
7
+ attr_accessor :configuration
8
+
9
+ def configure
10
+ self.configuration ||= Config.new
11
+ yield(configuration) if block_given?
12
+ configuration
13
+ end
14
+
15
+ def new(amount, currency = configuration.default_currency)
16
+ Base.new(amount, currency)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,67 @@
1
+ module Money
2
+ class Base
3
+ include Comparable
4
+
5
+ attr_reader :amount, :currency
6
+
7
+ def initialize(amount, currency)
8
+ @amount, @currency = amount, currency
9
+ end
10
+
11
+ def inspect
12
+ "#{@amount} #{@currency}"
13
+ end
14
+
15
+ def convert_to(to_curr)
16
+ self.class.new(convert(to_curr), to_curr)
17
+ end
18
+
19
+ def <=>(other)
20
+ @amount <=> other.convert_to(@currency).amount
21
+ end
22
+
23
+ def +(other)
24
+ arith(:+, other)
25
+ end
26
+
27
+ def -(other)
28
+ arith(:-, other)
29
+ end
30
+
31
+ def *(other)
32
+ arith(:*, other)
33
+ end
34
+
35
+ def /(other)
36
+ arith(:/, other)
37
+ end
38
+
39
+ private
40
+
41
+ def arith(op, obj)
42
+ if obj.is_a?(Money::Base)
43
+ arith op, obj.convert_to(@currency).amount
44
+ else
45
+ @amount = @amount.send(op, obj)
46
+ end
47
+
48
+ self
49
+ end
50
+
51
+ def convert_to_default_currency
52
+ @amount * conversions[@currency]
53
+ end
54
+
55
+ def convert(to_curr)
56
+ convert_to_default_currency / conversions[to_curr]
57
+ end
58
+
59
+ def default_currency
60
+ Money.configuration.default_currency
61
+ end
62
+
63
+ def conversions
64
+ Money.configuration.conversions
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,26 @@
1
+ module Money
2
+ class Config
3
+ attr_accessor :default_currency, :conversions
4
+
5
+ def initialize
6
+ @default_currency = 'USD'
7
+ @conversions = { 'USD' => 1 }
8
+ end
9
+
10
+ def default_currency=(currency)
11
+ @default_currency = currency
12
+ add_default_conversion
13
+ end
14
+
15
+ def conversions=(hash)
16
+ @conversions = hash
17
+ add_default_conversion
18
+ end
19
+
20
+ private
21
+
22
+ def add_default_conversion
23
+ @conversions[@default_currency] = 1
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module Money
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'money/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "money-exercise"
8
+ spec.version = Money::VERSION
9
+ spec.authors = ["Geronimo Diaz"]
10
+ spec.email = ["geronimod@gmail.com"]
11
+ spec.summary = %q{Currency conversion}
12
+ spec.description = %q{Convert different currencies plus aritmethical manipulation}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,99 @@
1
+ require 'spec_helper'
2
+
3
+ describe Money do
4
+ let(:money) { Money.new(10, 'EUR') }
5
+
6
+ before do
7
+ Money.configure do |config|
8
+ config.default_currency = 'ARS'
9
+ config.conversions = {
10
+ 'EUR' => 22.35,
11
+ 'USD' => 18.8,
12
+ 'Ether' => 0.001
13
+ }
14
+ end
15
+ end
16
+
17
+ it 'should show the amount' do
18
+ expect(money.amount).to eq(10)
19
+ end
20
+
21
+ it 'should show the currency' do
22
+ expect(money.currency).to eq('EUR')
23
+ end
24
+
25
+ it 'should support inspect' do
26
+ expect(money.inspect).to eq('10 EUR')
27
+ end
28
+
29
+ context 'configuration' do
30
+ it "should configure default_currency" do
31
+ expect(Money.configuration.default_currency).to eq('ARS')
32
+ end
33
+
34
+ it "should configure conversions" do
35
+ expect(Money.configuration.conversions).to be_a(Hash)
36
+ end
37
+
38
+ it 'should assign default currency' do
39
+ expect(Money.new(10).currency).to eq('ARS')
40
+ end
41
+ end
42
+
43
+ context 'conversion' do
44
+ let(:usd) { money.convert_to('USD') }
45
+
46
+ it 'should return the correct conversion value' do
47
+ ars = money.amount * Money.configuration.conversions['EUR'] #to ARS
48
+ expect(usd.amount).to eq(ars / Money.configuration.conversions['USD'])
49
+ end
50
+
51
+ it 'should convert to the same currency' do
52
+ eth = Money.new(10, 'Ether')
53
+ expect(eth.convert_to('Ether').amount).to eq(10)
54
+ end
55
+ end
56
+
57
+ context 'comparison' do
58
+ let(:ars) { Money.new(18.8, 'ARS') }
59
+ let(:usd) { Money.new(1, 'USD') }
60
+ let(:eur) { Money.new(2, 'EUR') }
61
+
62
+ it 'should be equal' do
63
+ expect(ars == usd).to be_truthy
64
+ end
65
+
66
+ it 'should be greater' do
67
+ expect(ars < eur).to be_truthy
68
+ end
69
+
70
+ it 'should be less' do
71
+ expect(eur > usd).to be_truthy
72
+ end
73
+ end
74
+
75
+ context 'arithmetic' do
76
+ let(:fifty_eur) { Money.new(50, 'EUR') }
77
+ let(:twenty_dollars) { Money.new(20, 'USD') }
78
+
79
+ it 'should support arithmetic operators between moneys' do
80
+ expect(fifty_eur + twenty_dollars).to eq(Money.new(66.82326621923937, 'EUR'))
81
+ end
82
+
83
+ it 'supports +' do
84
+ expect(fifty_eur + 5).to eq(Money.new(55, 'EUR'))
85
+ end
86
+
87
+ it 'supports -' do
88
+ expect(fifty_eur - 5).to eq(Money.new(45, 'EUR'))
89
+ end
90
+
91
+ it 'supports /' do
92
+ expect(fifty_eur / 5).to eq(Money.new(10, 'EUR'))
93
+ end
94
+
95
+ it 'supports *' do
96
+ expect(fifty_eur * 3).to eq(Money.new(150, 'EUR'))
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,101 @@
1
+ require 'money'
2
+ # This file was generated by the `rspec --init` command. Conventionally, all
3
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
4
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
5
+ # this file to always be loaded, without a need to explicitly require it in any
6
+ # files.
7
+ #
8
+ # Given that it is always loaded, you are encouraged to keep this file as
9
+ # light-weight as possible. Requiring heavyweight dependencies from this file
10
+ # will add to the boot time of your test suite on EVERY test run, even for an
11
+ # individual file that may not need all of that loaded. Instead, consider making
12
+ # a separate helper file that requires the additional dependencies and performs
13
+ # the additional setup, and require it from the spec files that actually need
14
+ # it.
15
+ #
16
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
17
+ RSpec.configure do |config|
18
+ # rspec-expectations config goes here. You can use an alternate
19
+ # assertion/expectation library such as wrong or the stdlib/minitest
20
+ # assertions if you prefer.
21
+ config.expect_with :rspec do |expectations|
22
+ # This option will default to `true` in RSpec 4. It makes the `description`
23
+ # and `failure_message` of custom matchers include text for helper methods
24
+ # defined using `chain`, e.g.:
25
+ # be_bigger_than(2).and_smaller_than(4).description
26
+ # # => "be bigger than 2 and smaller than 4"
27
+ # ...rather than:
28
+ # # => "be bigger than 2"
29
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
30
+ end
31
+
32
+ # rspec-mocks config goes here. You can use an alternate test double
33
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
34
+ config.mock_with :rspec do |mocks|
35
+ # Prevents you from mocking or stubbing a method that does not exist on
36
+ # a real object. This is generally recommended, and will default to
37
+ # `true` in RSpec 4.
38
+ mocks.verify_partial_doubles = true
39
+ end
40
+
41
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
42
+ # have no way to turn it off -- the option exists only for backwards
43
+ # compatibility in RSpec 3). It causes shared context metadata to be
44
+ # inherited by the metadata hash of host groups and examples, rather than
45
+ # triggering implicit auto-inclusion in groups with matching metadata.
46
+ config.shared_context_metadata_behavior = :apply_to_host_groups
47
+
48
+ # The settings below are suggested to provide a good initial experience
49
+ # with RSpec, but feel free to customize to your heart's content.
50
+ =begin
51
+ # This allows you to limit a spec run to individual examples or groups
52
+ # you care about by tagging them with `:focus` metadata. When nothing
53
+ # is tagged with `:focus`, all examples get run. RSpec also provides
54
+ # aliases for `it`, `describe`, and `context` that include `:focus`
55
+ # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
56
+ config.filter_run_when_matching :focus
57
+
58
+ # Allows RSpec to persist some state between runs in order to support
59
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
60
+ # you configure your source control system to ignore this file.
61
+ config.example_status_persistence_file_path = "spec/examples.txt"
62
+
63
+ # Limits the available syntax to the non-monkey patched syntax that is
64
+ # recommended. For more details, see:
65
+ # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
66
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
67
+ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
68
+ config.disable_monkey_patching!
69
+
70
+ # This setting enables warnings. It's recommended, but in some cases may
71
+ # be too noisy due to issues in dependencies.
72
+ config.warnings = true
73
+
74
+ # Many RSpec users commonly either run the entire suite or an individual
75
+ # file, and it's useful to allow more verbose output when running an
76
+ # individual spec file.
77
+ if config.files_to_run.one?
78
+ # Use the documentation formatter for detailed output,
79
+ # unless a formatter has already been configured
80
+ # (e.g. via a command-line flag).
81
+ config.default_formatter = "doc"
82
+ end
83
+
84
+ # Print the 10 slowest examples and example groups at the
85
+ # end of the spec run, to help surface which specs are running
86
+ # particularly slow.
87
+ config.profile_examples = 10
88
+
89
+ # Run specs in random order to surface order dependencies. If you find an
90
+ # order dependency and want to debug it, you can fix the order by providing
91
+ # the seed, which is printed after each run.
92
+ # --seed 1234
93
+ config.order = :random
94
+
95
+ # Seed global randomization in this process using the `--seed` CLI option.
96
+ # Setting this allows you to use `--seed` to deterministically reproduce
97
+ # test failures related to randomization by passing the same `--seed` value
98
+ # as the one that triggered the failure.
99
+ Kernel.srand config.seed
100
+ =end
101
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: money-exercise
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Geronimo Diaz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-01-11 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Convert different currencies plus aritmethical manipulation
56
+ email:
57
+ - geronimod@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/money.rb
69
+ - lib/money/base.rb
70
+ - lib/money/config.rb
71
+ - lib/money/version.rb
72
+ - money.gemspec
73
+ - spec/lib/money_spec.rb
74
+ - spec/spec_helper.rb
75
+ homepage: ''
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.5.2
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Currency conversion
99
+ test_files:
100
+ - spec/lib/money_spec.rb
101
+ - spec/spec_helper.rb