elegant_money 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2bb9d1bca3a2ac64c4e6031d1eb1776d97e42585
4
+ data.tar.gz: 87f9dd0bc6bc19be0b21d6edda872aba0fea1935
5
+ SHA512:
6
+ metadata.gz: 9f5272cc301d57d39c367539ad03a45738a7aa52fc435c69ae97751e02cf045664ea916cd27deed6d6fb4ce7d2406c6ecae6f33adb5d1e3928402c7a4f3a7877
7
+ data.tar.gz: 13282a7fbbca037f37e409de1017303cfcbdaa6e65edefac5446f4672daad9a51073e07aacaa5167b27946c87ccfef7d97f66eddab2624ad60fc58ad22e50b60
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at ncls.ayala@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Nico Ayala (nicoayala)
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,58 @@
1
+ # ElegantMoney
2
+
3
+ Elegant and simple implementation to handle money in different currencies.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'elegant_money'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install elegant_money
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ # Configure:
25
+ ElegantMoney.configure do |config|
26
+ config.default_currency = "EUR"
27
+ config.conversions = {
28
+ "USD" => 1.22,
29
+ # ...
30
+ }
31
+ end
32
+
33
+ # Instantiate and manipulate:
34
+ twenty_eur = ElegantMoney.new(20, "EUR")
35
+ twenty_eur * 2 # => 40.00 EUR
36
+ twenty_eur / 2 # => 10.00 EUR
37
+ twenty_eur + 10 # => 30.00 EUR
38
+ twenty_eur + ElegantMoney.new(30, "EUR") # => 50.00 EUR
39
+
40
+ # Convert:
41
+ twenty_eur.convert_to("USD") # => 22.20 USD
42
+ ```
43
+
44
+ ## Development
45
+
46
+ 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.
47
+
48
+ ## Contributing
49
+
50
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nicoayala/elegant_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.
51
+
52
+ ## License
53
+
54
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
55
+
56
+ ## Code of Conduct
57
+
58
+ Everyone interacting in the ElegantMoney project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/nicoayala/elegant_money/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,22 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "elegant_money"
3
+ spec.version = "0.1.0"
4
+ spec.authors = ["Nico"]
5
+ spec.email = ["ncls.ayala@gmail.com"]
6
+
7
+ spec.summary = %q{Elegant and simple implementation to handle money in different currencies}
8
+ spec.description = spec.summary
9
+ spec.homepage = "https://github.com/nicoayala/elegant_money"
10
+ spec.license = "MIT"
11
+
12
+ spec.files = `git ls-files lib spec *.md`.split("\n")
13
+ spec.files += %w[LICENSE.txt elegant_money.gemspec]
14
+ spec.require_paths = ["lib"]
15
+
16
+ spec.required_ruby_version = ">= 2.1.2"
17
+
18
+ spec.add_development_dependency "bundler", "~> 1.16"
19
+ spec.add_development_dependency "rake", "~> 10.0"
20
+ spec.add_development_dependency "rspec", "~> 3.0"
21
+ spec.add_development_dependency "pry", "~> 0.11"
22
+ end
@@ -0,0 +1,99 @@
1
+ class ElegantMoney
2
+ include Comparable
3
+
4
+ attr_reader :amount, :currency
5
+
6
+ def self.configure
7
+ yield configuration
8
+ end
9
+
10
+ def self.configuration
11
+ @configuration ||= Configuration.new
12
+ end
13
+
14
+ def initialize(amount, currency = 'EUR')
15
+ @amount = BigDecimal.new(amount.to_s)
16
+ @currency = currency
17
+ end
18
+
19
+ def inspect
20
+ "%.2f %s" % [amount, currency]
21
+ end
22
+ alias :to_s :inspect
23
+
24
+ def <=>(other)
25
+ other_amount = amount_with_normalized_currency(other)
26
+ amount.round(2) <=> other_amount.round(2)
27
+ end
28
+
29
+ def +(other)
30
+ operate_with_normalized_currency(other) do |other_amount|
31
+ amount + other_amount
32
+ end
33
+ end
34
+
35
+ def -(other)
36
+ operate_with_normalized_currency(other) do |other_amount|
37
+ amount - other_amount
38
+ end
39
+ end
40
+
41
+ def *(other)
42
+ operate_with_normalized_currency(other) do |other_amount|
43
+ amount * other_amount
44
+ end
45
+ end
46
+
47
+ def /(other)
48
+ operate_with_normalized_currency(other) do |other_amount|
49
+ amount / other_amount
50
+ end
51
+ end
52
+
53
+ def convert_to(new_currency)
54
+ return build(amount, currency) if currency == new_currency
55
+
56
+ conversion = conversion_for(new_currency)
57
+ new_amount = if currency == default_currency
58
+ amount * conversion
59
+ else
60
+ default_amount = amount / conversion_for(currency)
61
+ default_amount * conversion
62
+ end
63
+
64
+ build(new_amount, new_currency)
65
+ end
66
+
67
+ def infinite?
68
+ amount.infinite?
69
+ end
70
+
71
+ private
72
+
73
+ def operate_with_normalized_currency(other)
74
+ result = yield amount_with_normalized_currency(other)
75
+ build(result, currency)
76
+ end
77
+
78
+ def amount_with_normalized_currency(other)
79
+ return other.convert_to(currency).amount if other.is_a?(ElegantMoney)
80
+ return BigDecimal.new(other.to_s) if other.is_a?(Float)
81
+ other
82
+ end
83
+
84
+ def build(amount, currency)
85
+ ElegantMoney.new(amount, currency)
86
+ end
87
+
88
+ def conversion_for(key)
89
+ return 1 if key == default_currency
90
+ ElegantMoney.configuration.conversions.fetch(key)
91
+ end
92
+
93
+ def default_currency
94
+ ElegantMoney.configuration.default_currency
95
+ end
96
+ end
97
+
98
+ require "bigdecimal"
99
+ require "elegant_money/configuration"
@@ -0,0 +1,4 @@
1
+ # TODO: Should be immutable
2
+ class ElegantMoney::Configuration
3
+ attr_accessor :default_currency, :conversions
4
+ end
@@ -0,0 +1,112 @@
1
+ ElegantMoney.configure do |config|
2
+ config.default_currency = "EUR"
3
+ config.conversions = {
4
+ "USD" => 1.11,
5
+ "BTC" => 0.0047
6
+ }
7
+ end
8
+
9
+ RSpec.describe ElegantMoney do
10
+ describe "#inspect" do
11
+ it "returns a string representation" do
12
+ expect(eur(9.98).inspect).to eql("9.98 EUR")
13
+ end
14
+
15
+ it "returns a string representation when amount is not a number" do
16
+ expect(eur("asd").inspect).to eql("0.00 EUR")
17
+ end
18
+ end
19
+
20
+ describe "#+" do
21
+ it "returns the sum of two amounts" do
22
+ ten = eur(10)
23
+ expect(ten + ten).to eq eur(20)
24
+ end
25
+
26
+ it "returns the sum of two amounts when they have different currencies" do
27
+ expect(eur(50) + usd(20)).to eq eur(68.02)
28
+ end
29
+ end
30
+
31
+ describe "#-" do
32
+ it "returns the difference between two amounts" do
33
+ ten = eur(10)
34
+ expect(ten - ten).to eq eur(0)
35
+ end
36
+
37
+ it "returns the sum of two amounts when they have different currencies" do
38
+ expect(eur(50) - usd(20)).to eq eur(31.98)
39
+ end
40
+ end
41
+
42
+ describe "#*" do
43
+ it "returns the result of multiplying of two amounts" do
44
+ ten = eur(10)
45
+ expect(ten * ten).to eq eur(100)
46
+ end
47
+
48
+ it "returns the result of multiplying an amount and a primitive value" do
49
+ expect(eur(10) * 2).to eq eur(20)
50
+ expect(eur(10) * 2.0).to eq eur(20)
51
+ end
52
+ end
53
+
54
+ describe "#/" do
55
+ it "returns the result of the division of two amounts" do
56
+ expect(eur(10) / eur(2)).to eq eur(5)
57
+ end
58
+
59
+ it "returns infinity when the divider is zero" do
60
+ expect(eur(10) / eur(0)).to be_infinite
61
+ expect(eur(10) / 0).to be_infinite
62
+ end
63
+
64
+ it "returns the result of the division between an amount and a primitive value" do
65
+ expect(eur(10) / 2).to eq eur(5)
66
+ expect(eur(10) / 2.0).to eq eur(5)
67
+ end
68
+ end
69
+
70
+ describe "#convert_to" do
71
+ it "returns the amount in the new currency" do
72
+ expect(eur(10).convert_to("USD")).to eq usd(11.10)
73
+ end
74
+
75
+ it "returns the amount of the new currency when it is different than the default one" do
76
+ money = usd(10).convert_to("BTC")
77
+ expect(money).to eq btc(0.04)
78
+ expect(money.convert_to("USD")).to eq usd(10)
79
+ end
80
+
81
+ it "returns the same amount when converting to the same currency" do
82
+ expect(eur(10).convert_to("EUR")).to eq eur(10)
83
+ end
84
+ end
85
+
86
+ describe "comparision" do
87
+ it "returns true when amounts are equal" do
88
+ expect(eur(10) == eur(10)).to eq true
89
+ end
90
+
91
+ it "returns true when amounts are equal and currencies are different" do
92
+ expect(eur(10) == usd(11.10)).to eq true
93
+ end
94
+
95
+ it "returns true when amount is greater than the second one" do
96
+ expect(eur(10) > usd(10)).to eq true
97
+ expect(usd(10) < eur(10)).to eq true
98
+ end
99
+ end
100
+
101
+ def eur(amount)
102
+ described_class.new(amount, "EUR")
103
+ end
104
+
105
+ def usd(amount)
106
+ described_class.new(amount, "USD")
107
+ end
108
+
109
+ def btc(amount)
110
+ described_class.new(amount, "BTC")
111
+ end
112
+ end
@@ -0,0 +1,18 @@
1
+ require "bundler/setup"
2
+ require "elegant_money"
3
+ require "pry"
4
+
5
+ RSpec.configure do |config|
6
+ # Enable flags like --only-failures and --next-failure
7
+ config.example_status_persistence_file_path = ".rspec_status"
8
+
9
+ # Disable RSpec exposing methods globally on `Module` and `main`
10
+ config.disable_monkey_patching!
11
+
12
+ config.order = :random
13
+ Kernel.srand config.seed
14
+
15
+ config.expect_with :rspec do |c|
16
+ c.syntax = :expect
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: elegant_money
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nico
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-01-13 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
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.11'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.11'
69
+ description: Elegant and simple implementation to handle money in different currencies
70
+ email:
71
+ - ncls.ayala@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - CODE_OF_CONDUCT.md
77
+ - LICENSE.txt
78
+ - README.md
79
+ - elegant_money.gemspec
80
+ - lib/elegant_money.rb
81
+ - lib/elegant_money/configuration.rb
82
+ - spec/elegant_money_spec.rb
83
+ - spec/spec_helper.rb
84
+ homepage: https://github.com/nicoayala/elegant_money
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 2.1.2
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.2.2
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: Elegant and simple implementation to handle money in different currencies
108
+ test_files: []