monee 0.1.0
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 +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.rubocop.yml +8 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/Guardfile +32 -0
- data/LICENSE.txt +21 -0
- data/README.md +87 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/core_ext/numeric.rb +12 -0
- data/lib/monee.rb +15 -0
- data/lib/monee/arithmetic.rb +46 -0
- data/lib/monee/comparison.rb +18 -0
- data/lib/monee/config.rb +53 -0
- data/lib/monee/configurable.rb +30 -0
- data/lib/monee/conversion.rb +25 -0
- data/lib/monee/currency.rb +54 -0
- data/lib/monee/error.rb +12 -0
- data/lib/monee/money.rb +74 -0
- data/lib/monee/no_config.rb +27 -0
- data/lib/monee/version.rb +4 -0
- data/loc.sh +1 -0
- data/monee.gemspec +42 -0
- metadata +200 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 1efffe7d283046f793a104cf788c0c9b2c17e258
|
|
4
|
+
data.tar.gz: 48b298d8f14e307c9ee45053e38ee24bd02f465c
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 5a060f4893e9755381eaaff7159ceeebd7a308c7c197e9776660616f1cc9a9f871192ea8c37cae2020fc8df5429341a521d4053052fc9b924c2896b060b36071
|
|
7
|
+
data.tar.gz: 0e3cdc9c668340682565e17ffdeabfdc95c392c963ba7b77754a1f7d04078fb9bc5ec992e679faeca9c02c09b9887af0d077989d06edf175dee6153185a9348c
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
AllCops:
|
|
2
|
+
TargetRubyVersion: 2.2
|
|
3
|
+
# RuboCop has a bunch of cops enabled by default. This setting tells RuboCop
|
|
4
|
+
# to ignore them, so only the ones explicitly set in this file are enabled.
|
|
5
|
+
DisabledByDefault: false
|
|
6
|
+
Metrics/LineLength:
|
|
7
|
+
Enabled: false
|
|
8
|
+
Max: 100
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -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 senthilkumar.hce@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/
|
data/Gemfile
ADDED
data/Guardfile
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
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
|
+
guard :rspec, cmd: 'bundle exec rspec' do
|
|
19
|
+
require 'guard/rspec/dsl'
|
|
20
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
|
21
|
+
|
|
22
|
+
# RSpec files
|
|
23
|
+
rspec = dsl.rspec
|
|
24
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
|
25
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
|
26
|
+
watch(rspec.spec_files)
|
|
27
|
+
|
|
28
|
+
# Ruby files
|
|
29
|
+
ruby = dsl.ruby
|
|
30
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
|
31
|
+
|
|
32
|
+
end
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017 beck03076
|
|
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,87 @@
|
|
|
1
|
+
# Monee
|
|
2
|
+
|
|
3
|
+
Monee simply means nothing but Money and it is there as a namespace to avoid contradiction with existing gems.
|
|
4
|
+
|
|
5
|
+
This is a rubygem created with minimal features as required and engineered with very simple techniques showcasing few good practices in building a ruby project.
|
|
6
|
+
|
|
7
|
+
Following TDD practice, designed with few useful patterns, styled with rubocop style guide, documented with yard convention, lines of code analysis, 100% tests coverage and properly error handled rubygem project could be very useful to assess one's knowledge in build rubygems.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
Add this line to your application's Gemfile:
|
|
12
|
+
|
|
13
|
+
```ruby
|
|
14
|
+
gem 'monee'
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
And then execute:
|
|
18
|
+
|
|
19
|
+
$ bundle
|
|
20
|
+
|
|
21
|
+
Or install it yourself as:
|
|
22
|
+
|
|
23
|
+
$ gem install monee
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
# 1. Set the conversion rates
|
|
28
|
+
|
|
29
|
+
$ Monee::Money.conversion_rates('EUR', {
|
|
30
|
+
'USD' => 1.11,
|
|
31
|
+
'Bitcoin' => 0.0047
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
# 2. Start using as below,
|
|
35
|
+
|
|
36
|
+
$ fifty_eur = Monee::Money.new(50, 'EUR')
|
|
37
|
+
$ twenty_dollars = Monee::Money.new(20, 'USD')
|
|
38
|
+
$ fifty_eur + twenty_dollars # => 68.02 EUR
|
|
39
|
+
|
|
40
|
+
## How to run the test suite
|
|
41
|
+
|
|
42
|
+
Use the following commands to run the test suites
|
|
43
|
+
|
|
44
|
+
$ cd monee
|
|
45
|
+
$ rspec
|
|
46
|
+
|
|
47
|
+
## Tests Coverage(100%)
|
|
48
|
+
|
|
49
|
+
Once your run $ rspec then the commandline will generate the tests coverage under coverage directory. This is a static HTML file generated by the simplecov. You can view this file on your browser just like how you would open any other file on your browser. Here is how I opened in with mine.
|
|
50
|
+
|
|
51
|
+
Enter an URL that looks like below on your browser,
|
|
52
|
+
|
|
53
|
+
$ file:///Users/beck/BECK/interview/monee/coverage/index.html#_AllFiles
|
|
54
|
+
|
|
55
|
+
## Steps to check LOC
|
|
56
|
+
|
|
57
|
+
To check the lines of code in this repository, you can use this simple shell script by executing the following command,
|
|
58
|
+
|
|
59
|
+
$ ./loc.sh
|
|
60
|
+
|
|
61
|
+
## Yard Documentation
|
|
62
|
+
|
|
63
|
+
The entire code base is well documented following the yard coding comments conventions. To read the documentation of this rails app you can execute the following command,
|
|
64
|
+
|
|
65
|
+
$ yard server
|
|
66
|
+
|
|
67
|
+
and visit http://localhost:8808, to the yard documentation of this application.
|
|
68
|
+
|
|
69
|
+
## 0 offense(s) in Rubocop
|
|
70
|
+
|
|
71
|
+
This is really something to be proud about!
|
|
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/[USERNAME]/monee. 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
|
+
|
|
83
|
+
|
|
84
|
+
## License
|
|
85
|
+
|
|
86
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
|
87
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "monee"
|
|
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
data/lib/monee.rb
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
require 'monee/version'
|
|
2
|
+
require 'monee/arithmetic'
|
|
3
|
+
require 'monee/comparison'
|
|
4
|
+
require 'monee/config'
|
|
5
|
+
require 'monee/no_config'
|
|
6
|
+
require 'monee/configurable'
|
|
7
|
+
require 'monee/conversion'
|
|
8
|
+
require 'monee/currency'
|
|
9
|
+
require 'monee/error'
|
|
10
|
+
require 'monee/money'
|
|
11
|
+
require_relative '../lib/core_ext/numeric'
|
|
12
|
+
|
|
13
|
+
# main namespace to avoid contradiction with existing gems
|
|
14
|
+
module Monee
|
|
15
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
module Monee
|
|
2
|
+
# modularizing the arithmetic related methods in a separate module for money
|
|
3
|
+
module Arithmetic
|
|
4
|
+
# metaprogramming way of defining methods
|
|
5
|
+
#
|
|
6
|
+
# overloads the operators with 2 operands
|
|
7
|
+
# adds amount/cents if number
|
|
8
|
+
# converts to the currency and calculates if money object
|
|
9
|
+
# @return [Money]
|
|
10
|
+
%i[+ - * /].each do |operator|
|
|
11
|
+
define_method(operator) do |operand|
|
|
12
|
+
result_cents = case operand
|
|
13
|
+
when ::Numeric
|
|
14
|
+
cents.send(operator,
|
|
15
|
+
choose_operand(operator, operand))
|
|
16
|
+
when klass
|
|
17
|
+
cents.send(operator,
|
|
18
|
+
operand.convert_to_cents(currency))
|
|
19
|
+
else
|
|
20
|
+
raise InvalidOperand
|
|
21
|
+
end
|
|
22
|
+
amount = result_cents.to_amount
|
|
23
|
+
klass.new(amount, currency)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# convert to cents if add or subtract
|
|
28
|
+
# dont convert to cents if multiply or divide
|
|
29
|
+
#
|
|
30
|
+
# @params operator [+, -, *, /]
|
|
31
|
+
# @return [Numeric]
|
|
32
|
+
def choose_operand(operator, operand)
|
|
33
|
+
if %i[+ -].include?(operator)
|
|
34
|
+
operand.to_cents
|
|
35
|
+
elsif %i[* /].include?(operator)
|
|
36
|
+
operand
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# this method is to support the order of the operation
|
|
41
|
+
# 2 * Monee::Money.new(50, 'EUR')
|
|
42
|
+
def coerce(other)
|
|
43
|
+
return self, other
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module Monee
|
|
2
|
+
# modularizing the comparison of money objects method in a separate module
|
|
3
|
+
module Comparison
|
|
4
|
+
include Comparable
|
|
5
|
+
|
|
6
|
+
# this is the spaceship operator overriding, this will take care of >, <, ==
|
|
7
|
+
#
|
|
8
|
+
# @params other [Money, Numeric]
|
|
9
|
+
# @return [Boolean]
|
|
10
|
+
def <=>(other)
|
|
11
|
+
if currency == other.currency
|
|
12
|
+
cents <=> other.cents
|
|
13
|
+
else
|
|
14
|
+
cents <=> other.convert_to_cents(currency)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
data/lib/monee/config.rb
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
require 'singleton'
|
|
2
|
+
module Monee
|
|
3
|
+
# Singleton class that can be initiated only once to
|
|
4
|
+
# hold the configuration values
|
|
5
|
+
# @see https://ruby-doc.org/stdlib-2.2.2/libdoc/singleton/rdoc/Singleton.html
|
|
6
|
+
class Config
|
|
7
|
+
include Singleton
|
|
8
|
+
|
|
9
|
+
# this is default rate, against this value other
|
|
10
|
+
# currency rates are calculated
|
|
11
|
+
DEFAULT_RATE = 1
|
|
12
|
+
|
|
13
|
+
attr_accessor :base_currency, :currency_rates
|
|
14
|
+
|
|
15
|
+
# to reset the configuration values
|
|
16
|
+
# @return [void]
|
|
17
|
+
def reset
|
|
18
|
+
self.base_currency = nil
|
|
19
|
+
self.currency_rates = nil
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# fetches the rate of a currency by code of the currency
|
|
23
|
+
#
|
|
24
|
+
# @param code [String] code of the currency
|
|
25
|
+
# @return [Numeric]
|
|
26
|
+
def fetch_rate(code)
|
|
27
|
+
currency_rates[code]
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# checks if the code exists in the config
|
|
31
|
+
#
|
|
32
|
+
# @param code [String] code of the currency
|
|
33
|
+
# @return [Boolean]
|
|
34
|
+
def exists?(code)
|
|
35
|
+
available_currencies.include?(code)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# retrieves all the currencies configured including the base
|
|
39
|
+
#
|
|
40
|
+
# @return [Array]
|
|
41
|
+
def available_currencies
|
|
42
|
+
set_default_rate
|
|
43
|
+
currency_rates.keys
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# sets the default rate to the base currency as 1
|
|
47
|
+
#
|
|
48
|
+
# @return [void]
|
|
49
|
+
def set_default_rate
|
|
50
|
+
currency_rates[base_currency] = DEFAULT_RATE
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
module Monee
|
|
2
|
+
# helper module to fetch the right config instance
|
|
3
|
+
module Configurable
|
|
4
|
+
# yields the Config.instance if a block is called
|
|
5
|
+
#
|
|
6
|
+
# Currency.configure do |conf|
|
|
7
|
+
# conf.variable = value
|
|
8
|
+
# end
|
|
9
|
+
def configure
|
|
10
|
+
yield(actual_config) if block_given?
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# checks if config values are nil and returns no_config or config
|
|
14
|
+
#
|
|
15
|
+
# @return [Config, NoConfig]
|
|
16
|
+
def config
|
|
17
|
+
(null_config? ? NoConfig.instance : actual_config)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# checks if the config values are nil
|
|
21
|
+
def null_config?
|
|
22
|
+
actual_config.base_currency.nil? || actual_config.currency_rates.nil?
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# simple way to access the config singleton instance
|
|
26
|
+
def actual_config
|
|
27
|
+
Config.instance
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
module Monee
|
|
2
|
+
# modularizing all the conversion related logic under one module
|
|
3
|
+
module Conversion
|
|
4
|
+
# converts to a target currency
|
|
5
|
+
#
|
|
6
|
+
# @param to_currency [String]
|
|
7
|
+
# @return [Money]
|
|
8
|
+
def convert_to(to_currency)
|
|
9
|
+
amount = convert_to_cents(to_currency).to_amount
|
|
10
|
+
klass.new(amount, to_currency)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# convert to cents for a target currency
|
|
14
|
+
#
|
|
15
|
+
# @param to_currency [String]
|
|
16
|
+
# @return [Numeric]
|
|
17
|
+
def convert_to_cents(to_currency)
|
|
18
|
+
if to_currency == currency
|
|
19
|
+
cents
|
|
20
|
+
else
|
|
21
|
+
@currency.exchange(to_currency, cents)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
module Monee
|
|
2
|
+
# class to manage currency code and rate, also the exchange
|
|
3
|
+
class Currency
|
|
4
|
+
# class methods to access the config values
|
|
5
|
+
extend Configurable
|
|
6
|
+
|
|
7
|
+
# error message to raise with for uninitialized currencies
|
|
8
|
+
NO_CURRENCY = 'This currency\'s conversion rate is not configured,
|
|
9
|
+
please use Monee::Money.conversion_rates'.freeze
|
|
10
|
+
|
|
11
|
+
# error message to raise for empty string as currency
|
|
12
|
+
EMPTY_CURRENCY = 'Currency cannot to empty!'.freeze
|
|
13
|
+
|
|
14
|
+
attr_reader :code, :rate
|
|
15
|
+
|
|
16
|
+
# creates a currency object with code
|
|
17
|
+
#
|
|
18
|
+
# @param options [Hash] code: 'USD' is one example
|
|
19
|
+
# @return [Currency]
|
|
20
|
+
def initialize(**options)
|
|
21
|
+
options = options.compact
|
|
22
|
+
@code = options[:code]
|
|
23
|
+
@rate = config.fetch_rate(@code) if valid_currency?
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# exchanges the current currency to to_currency with cents and rate
|
|
27
|
+
#
|
|
28
|
+
# @params to_currency [String] target currency to be converted to
|
|
29
|
+
# @params cents [Numeric] number of cents to be converted
|
|
30
|
+
# @raise [UndefinedCurrency] if to_currency is not in Config
|
|
31
|
+
# @return [Numeric] number of cents converted
|
|
32
|
+
def exchange(to_currency, cents)
|
|
33
|
+
raise UndefinedCurrency, NO_CURRENCY unless config.exists?(to_currency)
|
|
34
|
+
(cents / rate) * config.fetch_rate(to_currency)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# @return [Config, NoConfig]
|
|
38
|
+
def config
|
|
39
|
+
self.class.config
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
private
|
|
43
|
+
|
|
44
|
+
# validation for currency object
|
|
45
|
+
#
|
|
46
|
+
# @raise [EmptyCurrency] if currency is empty string
|
|
47
|
+
# @raise [UndefinedCurrency] if to_currency is not in Config
|
|
48
|
+
def valid_currency?
|
|
49
|
+
raise EmptyCurrency, EMPTY_CURRENCY if code.empty?
|
|
50
|
+
raise UndefinedCurrency, NO_CURRENCY unless config.exists?(code)
|
|
51
|
+
true
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
data/lib/monee/error.rb
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# A general monee exception
|
|
2
|
+
class Error < StandardError; end
|
|
3
|
+
# Raised when currency is undefined
|
|
4
|
+
class UndefinedCurrency < Error; end
|
|
5
|
+
# Raised when currency is empty
|
|
6
|
+
class EmptyCurrency < Error; end
|
|
7
|
+
# Raised when currency is invalid
|
|
8
|
+
class InvalidCurrency < Error; end
|
|
9
|
+
# Raised when amount is invalid
|
|
10
|
+
class InvalidAmount < Error; end
|
|
11
|
+
# Raised when operand is invalid
|
|
12
|
+
class InvalidOperand < Error; end
|
data/lib/monee/money.rb
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# This is main module Monee for the entire project's namespace
|
|
2
|
+
module Monee
|
|
3
|
+
# class to manipulate currencies as required
|
|
4
|
+
class Money
|
|
5
|
+
include Arithmetic
|
|
6
|
+
include Conversion
|
|
7
|
+
include Comparison
|
|
8
|
+
|
|
9
|
+
attr_reader :cents, :currency_code
|
|
10
|
+
|
|
11
|
+
class << self
|
|
12
|
+
# Configures the config singleton class with default values
|
|
13
|
+
#
|
|
14
|
+
# @param base_currency [String]
|
|
15
|
+
# @param currency_rates [Hash]
|
|
16
|
+
# @return [void]
|
|
17
|
+
def conversion_rates(base_currency, currency_rates)
|
|
18
|
+
Currency.configure do |config|
|
|
19
|
+
config.base_currency = base_currency
|
|
20
|
+
config.currency_rates = currency_rates
|
|
21
|
+
config.set_default_rate
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
# Initializes a money object
|
|
26
|
+
#
|
|
27
|
+
# @param amount [Numeric]
|
|
28
|
+
# @param currency_code [String]
|
|
29
|
+
# @return [Money]
|
|
30
|
+
def initialize(amount, currency_code)
|
|
31
|
+
@amount = amount
|
|
32
|
+
@cents = amount.to_cents
|
|
33
|
+
@currency_code = currency_code
|
|
34
|
+
validate_args!
|
|
35
|
+
set_currency
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# @return [Money] method to access this class
|
|
39
|
+
def klass
|
|
40
|
+
self.class
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# @return [String] the currency_code of the current object
|
|
44
|
+
def currency
|
|
45
|
+
currency_code
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# @return [Numeric] passed amount of the object
|
|
49
|
+
def amount
|
|
50
|
+
cents.to_amount
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# @return [String] formatted string of money
|
|
54
|
+
def inspect
|
|
55
|
+
"#{format('%.2f', amount)} #{currency}"
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
private
|
|
59
|
+
|
|
60
|
+
# Validation of money arguments
|
|
61
|
+
#
|
|
62
|
+
# @raise [InvalidAmount] if amount is negative or not a number
|
|
63
|
+
# @raise [InvalidCurrency] if currency_code is not alphabets
|
|
64
|
+
def validate_args!
|
|
65
|
+
raise InvalidAmount unless @amount.is_a?(Numeric) && @amount >= 0
|
|
66
|
+
raise InvalidCurrency unless @currency_code =~ /[A-Za-z]/
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Sets the currency object in currency instance variable
|
|
70
|
+
def set_currency
|
|
71
|
+
@currency ||= Currency.new(code: currency_code)
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
require 'singleton'
|
|
2
|
+
module Monee
|
|
3
|
+
# inherits from config and this the null object for config with default values
|
|
4
|
+
#
|
|
5
|
+
# @see https://en.wikipedia.org/wiki/Null_Object_pattern
|
|
6
|
+
class NoConfig < Config
|
|
7
|
+
include Singleton
|
|
8
|
+
|
|
9
|
+
# default base currecy
|
|
10
|
+
DEFAULT_BASE_CURRENCY = 'EUR'.freeze
|
|
11
|
+
# default currency_rates
|
|
12
|
+
DEFAULT_CURRENCY_RATES = {
|
|
13
|
+
'USD' => 1.11,
|
|
14
|
+
'Bitcoin' => 0.0047
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
# @return [String]
|
|
18
|
+
def base_currency
|
|
19
|
+
DEFAULT_BASE_CURRENCY
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# @return [Hash]
|
|
23
|
+
def currency_rates
|
|
24
|
+
DEFAULT_CURRENCY_RATES
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
data/loc.sh
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
find . -name '*.rb' ! -path "./vendor/*" ! -path "./config/*" | xargs wc -l
|
data/monee.gemspec
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'monee/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "monee"
|
|
8
|
+
spec.version = Monee::VERSION
|
|
9
|
+
spec.authors = ["beck03076"]
|
|
10
|
+
spec.email = ["senthilkumar.hce@gmail.com"]
|
|
11
|
+
|
|
12
|
+
spec.summary = %q{ This is a rubygem created with minimal features as required and engineered with very simple techniques showcasing few good practices in building a ruby project. }
|
|
13
|
+
spec.description = %q{ Following TDD practice, designed with few useful patterns, styled with rubocop style guide, documented with yard convention, lines of code analysis, 100% tests coverage and properly error handled rubygem project could be very useful to assess one's knowledge in build rubygems. }
|
|
14
|
+
spec.homepage = "https://github.com/beck03076/monee"
|
|
15
|
+
spec.license = "MIT"
|
|
16
|
+
|
|
17
|
+
# # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
|
18
|
+
# # to allow pushing to a single host or delete this section to allow pushing to any host.
|
|
19
|
+
# if spec.respond_to?(:metadata)
|
|
20
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
|
21
|
+
# else
|
|
22
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
|
23
|
+
# "public gem pushes."
|
|
24
|
+
# end
|
|
25
|
+
|
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
|
27
|
+
f.match(%r{^(test|spec|features)/})
|
|
28
|
+
end
|
|
29
|
+
spec.bindir = "exe"
|
|
30
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
31
|
+
spec.require_paths = ["lib"]
|
|
32
|
+
|
|
33
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
|
34
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
35
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
|
36
|
+
spec.add_development_dependency "guard"
|
|
37
|
+
spec.add_development_dependency "guard-rspec"
|
|
38
|
+
spec.add_development_dependency "byebug"
|
|
39
|
+
spec.add_development_dependency "rubocop"
|
|
40
|
+
spec.add_development_dependency "simplecov"
|
|
41
|
+
spec.add_development_dependency "yard"
|
|
42
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: monee
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- beck03076
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2017-05-07 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.14'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.14'
|
|
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: guard
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - ">="
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '0'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - ">="
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '0'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: guard-rspec
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - ">="
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '0'
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - ">="
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '0'
|
|
83
|
+
- !ruby/object:Gem::Dependency
|
|
84
|
+
name: byebug
|
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - ">="
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: '0'
|
|
90
|
+
type: :development
|
|
91
|
+
prerelease: false
|
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - ">="
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: '0'
|
|
97
|
+
- !ruby/object:Gem::Dependency
|
|
98
|
+
name: rubocop
|
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
|
100
|
+
requirements:
|
|
101
|
+
- - ">="
|
|
102
|
+
- !ruby/object:Gem::Version
|
|
103
|
+
version: '0'
|
|
104
|
+
type: :development
|
|
105
|
+
prerelease: false
|
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
107
|
+
requirements:
|
|
108
|
+
- - ">="
|
|
109
|
+
- !ruby/object:Gem::Version
|
|
110
|
+
version: '0'
|
|
111
|
+
- !ruby/object:Gem::Dependency
|
|
112
|
+
name: simplecov
|
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
|
114
|
+
requirements:
|
|
115
|
+
- - ">="
|
|
116
|
+
- !ruby/object:Gem::Version
|
|
117
|
+
version: '0'
|
|
118
|
+
type: :development
|
|
119
|
+
prerelease: false
|
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
121
|
+
requirements:
|
|
122
|
+
- - ">="
|
|
123
|
+
- !ruby/object:Gem::Version
|
|
124
|
+
version: '0'
|
|
125
|
+
- !ruby/object:Gem::Dependency
|
|
126
|
+
name: yard
|
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
|
128
|
+
requirements:
|
|
129
|
+
- - ">="
|
|
130
|
+
- !ruby/object:Gem::Version
|
|
131
|
+
version: '0'
|
|
132
|
+
type: :development
|
|
133
|
+
prerelease: false
|
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
135
|
+
requirements:
|
|
136
|
+
- - ">="
|
|
137
|
+
- !ruby/object:Gem::Version
|
|
138
|
+
version: '0'
|
|
139
|
+
description: " Following TDD practice, designed with few useful patterns, styled with
|
|
140
|
+
rubocop style guide, documented with yard convention, lines of code analysis, 100%
|
|
141
|
+
tests coverage and properly error handled rubygem project could be very useful to
|
|
142
|
+
assess one's knowledge in build rubygems. "
|
|
143
|
+
email:
|
|
144
|
+
- senthilkumar.hce@gmail.com
|
|
145
|
+
executables: []
|
|
146
|
+
extensions: []
|
|
147
|
+
extra_rdoc_files: []
|
|
148
|
+
files:
|
|
149
|
+
- ".gitignore"
|
|
150
|
+
- ".rspec"
|
|
151
|
+
- ".rubocop.yml"
|
|
152
|
+
- ".travis.yml"
|
|
153
|
+
- CODE_OF_CONDUCT.md
|
|
154
|
+
- Gemfile
|
|
155
|
+
- Guardfile
|
|
156
|
+
- LICENSE.txt
|
|
157
|
+
- README.md
|
|
158
|
+
- Rakefile
|
|
159
|
+
- bin/console
|
|
160
|
+
- bin/setup
|
|
161
|
+
- lib/core_ext/numeric.rb
|
|
162
|
+
- lib/monee.rb
|
|
163
|
+
- lib/monee/arithmetic.rb
|
|
164
|
+
- lib/monee/comparison.rb
|
|
165
|
+
- lib/monee/config.rb
|
|
166
|
+
- lib/monee/configurable.rb
|
|
167
|
+
- lib/monee/conversion.rb
|
|
168
|
+
- lib/monee/currency.rb
|
|
169
|
+
- lib/monee/error.rb
|
|
170
|
+
- lib/monee/money.rb
|
|
171
|
+
- lib/monee/no_config.rb
|
|
172
|
+
- lib/monee/version.rb
|
|
173
|
+
- loc.sh
|
|
174
|
+
- monee.gemspec
|
|
175
|
+
homepage: https://github.com/beck03076/monee
|
|
176
|
+
licenses:
|
|
177
|
+
- MIT
|
|
178
|
+
metadata: {}
|
|
179
|
+
post_install_message:
|
|
180
|
+
rdoc_options: []
|
|
181
|
+
require_paths:
|
|
182
|
+
- lib
|
|
183
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
184
|
+
requirements:
|
|
185
|
+
- - ">="
|
|
186
|
+
- !ruby/object:Gem::Version
|
|
187
|
+
version: '0'
|
|
188
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
189
|
+
requirements:
|
|
190
|
+
- - ">="
|
|
191
|
+
- !ruby/object:Gem::Version
|
|
192
|
+
version: '0'
|
|
193
|
+
requirements: []
|
|
194
|
+
rubyforge_project:
|
|
195
|
+
rubygems_version: 2.6.10
|
|
196
|
+
signing_key:
|
|
197
|
+
specification_version: 4
|
|
198
|
+
summary: This is a rubygem created with minimal features as required and engineered
|
|
199
|
+
with very simple techniques showcasing few good practices in building a ruby project.
|
|
200
|
+
test_files: []
|