fund 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f8d64c0d8f9edda4df34a6e3b1b715c0ea3599f0
4
+ data.tar.gz: a4a6016fa529d0a8acfca826449e0d2ecc466a41
5
+ SHA512:
6
+ metadata.gz: 73ef9fc21371041b50802f502cf8e2f32026595ef9ebab902cba815a034d4c088312e21aab1927e0335cf292f6ee0426b8a49e103c7ee2e3a157a9b42de3a130
7
+ data.tar.gz: 15e847cafbc24bca153c22648f3069ad1386ab5e019c5266cb57d66e9895a2a7e3cc232df73c9921e1144e264b1758b02a5d31e7e5c49c3dea3106c61bf260c7
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+ .idea/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.0
5
+ before_install: gem install bundler -v 1.14.6
@@ -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 nazarhussain@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
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fund.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # Fund
2
+
3
+ [![Build Status](https://travis-ci.org/nazarhussain/fund.svg?branch=master)](https://travis-ci.org/nazarhussain/fund)
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/fund`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ TODO: Delete this and the text above, and describe your gem
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'fund'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install fund
24
+
25
+ ## Usage
26
+
27
+ Create the objects like
28
+ ```ruby
29
+ Fund.new(100)
30
+ Fund.new(100, :usd)
31
+ Fund.new(25.45, :eur)
32
+ ```
33
+ For more details see the test [specs](/spec)
34
+
35
+ ## Development
36
+
37
+ 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.
38
+
39
+ 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).
40
+
41
+ ## Contributing
42
+
43
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nazarhussain/fund. 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.
44
+
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 "fund"
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/exe/fund ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "fund"
data/fund.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fund/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fund"
8
+ spec.version = Fund::VERSION
9
+ spec.authors = ["Nazar Hussain"]
10
+ spec.email = ["nazarhussain@gmail.com"]
11
+
12
+ spec.summary = %q{A gem utilizing ruby power to handle money and currency conversion issues.}
13
+ spec.description = %q{If you ever need to build a ruby based app where you need currency conversion. This gem will help you in every aspect.}
14
+ spec.homepage = "https://github.com/nazarhussain"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against " \
22
+ "public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ f.match(%r{^(test|spec|features)/})
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.14"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "rspec", "~> 3.0"
35
+ end
@@ -0,0 +1,26 @@
1
+ class Fund
2
+ module Arithmetic
3
+
4
+ def +(other)
5
+ Fund.new(@value + other.convert_to(@currency).value, @currency)
6
+ end
7
+
8
+ def -(other)
9
+ Fund.new(@value - other.convert_to(@currency).value, @currency)
10
+ end
11
+
12
+ def *(other)
13
+ raise ArgumentError.new('Fund can\'t be multiplied by funds. It must be Intger or Float.') if other.class == Fund
14
+
15
+ @value *= other
16
+ self
17
+ end
18
+
19
+ def /(other)
20
+ raise ArgumentError.new('Fund can\'t be multiplied by funds. It must be Intger or Float.') if other.class == Fund
21
+
22
+ @value /= other
23
+ self
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,13 @@
1
+ class Fund
2
+ module Comparision
3
+
4
+ # Comparision of of two funds
5
+ #
6
+ # @param other [Fund] Compare two funds
7
+ def <=>(other)
8
+ raise ArgumentError.new('You can only compare two funds.') if other.class != Fund
9
+
10
+ @value <=> other.convert_to(@currency).value
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,54 @@
1
+ class Fund
2
+ # * Module to setup the configuration
3
+ module Configuration
4
+ attr_accessor :default_currency, :supported_currencies, :currency_symbols
5
+ attr_accessor :currency_rates, :round_limit
6
+
7
+ class << self
8
+ # List of configurable keys for {Fund::Client}
9
+ # @return [Array] of option keys
10
+ def keys
11
+ # Reject all setters to get keys of only attributes
12
+ # Also reject other utility methods for the class
13
+ @keys ||= Fund::Configuration.public_instance_methods.reject { |method| method =~ /^.*=$/ } -
14
+ [:setup, :load_default, :config, :add]
15
+ end
16
+ end
17
+
18
+ # Set configuration options using a block
19
+ #
20
+ # Fund.setup do |config|
21
+ # config.supported_currencies = [:usd, :eur]
22
+ # end
23
+ # Fund.setup do |config|
24
+ # config.add :bit, 'Bit', 113.40
25
+ # end
26
+ def setup
27
+ yield self
28
+ end
29
+
30
+ # Utility method to add a new language in configuration
31
+ #
32
+ # @param currency [Symbol] A symbol representing the currency e.g. :usd, :eur
33
+ # @param symbol [String] A string representation of the currency e.g. $
34
+ # @param rate [Float] A floating number showing the currency rate with respect to default currency. See [Configuration::Default] for more reference
35
+ #
36
+ def add(currency, symbol, rate)
37
+ @supported_currencies << currency
38
+ @currency_symbols[currency] = symbol
39
+ @currency_rates[currency] = rate
40
+ end
41
+
42
+ def load_default
43
+ Configuration.keys.each do |key|
44
+ instance_variable_set(:"@#{key}", Fund::Default.options[key])
45
+ end
46
+ self
47
+ end
48
+
49
+ def config
50
+ self
51
+ end
52
+ end
53
+ end
54
+
@@ -0,0 +1,38 @@
1
+ class Fund
2
+ module Conversion
3
+
4
+ # Convert one fund to other currency
5
+ #
6
+ # @param other_currency [Symbol, String] Symbol or string representing other currency
7
+ #
8
+ # @return [Fund]
9
+ #
10
+ def convert_to(other_currency)
11
+ other_currency = other_currency.to_sym
12
+
13
+ Fund.new(@value * conversion_rate_for(other_currency), other_currency)
14
+ end
15
+
16
+ # Get conversion rate for a currency that used to be multiplied in conversion
17
+ #
18
+ # @param other_currency [Symbol] symbol representing other currency
19
+ #
20
+ # @return [Float]
21
+ #
22
+ def conversion_rate_for(other_currency)
23
+ # If both currencies are same then conversion rate is 1
24
+ if @currency == other_currency
25
+ 1
26
+
27
+ # If rate required for base currency then divide currency rates with 1
28
+ elsif other_currency == Fund.config.default_currency
29
+ 1.0 / Fund.config.currency_rates[@currency].to_f
30
+
31
+ # If rate required for currency other than base then first convert base currency rate
32
+ # and then multiply with target currency rate
33
+ else
34
+ Fund.config.currency_rates[other_currency] * (1.0 / Fund.config.currency_rates[@currency].to_f)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,41 @@
1
+ class Fund
2
+ # * Module to load the default configuration values
3
+ module Default
4
+ class << self
5
+
6
+ # Configuration options
7
+ # @return [Hash]
8
+ def options
9
+ Hash[Fund::Configuration.keys.map{ |key| [key, send(key)] }]
10
+ end
11
+
12
+ def default_currency
13
+ :usd
14
+ end
15
+
16
+ # For adding more currencies please check the list at
17
+ # https://en.wikipedia.org/wiki/Currency_symbol
18
+ def supported_currencies
19
+ [:usd, :eur, :pkr]
20
+ end
21
+
22
+ def currency_symbols
23
+ { usd: '$',
24
+ eur: '€',
25
+ pkr: 'Rs'
26
+ }
27
+ end
28
+
29
+ def currency_rates
30
+ { usd: 1,
31
+ eur: 0.91,
32
+ pkr: 110
33
+ }
34
+ end
35
+
36
+ def round_limit
37
+ 2
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ class Fund
2
+ VERSION = "0.1.0"
3
+ end
data/lib/fund.rb ADDED
@@ -0,0 +1,54 @@
1
+ require 'fund/version'
2
+ require 'fund/configuration'
3
+ require 'fund/default'
4
+ require 'fund/conversion'
5
+ require 'fund/arithmetic'
6
+ require 'fund/comparision'
7
+
8
+
9
+ # * Main Fund Module
10
+ class Fund
11
+ extend Configuration
12
+ include Fund::Conversion
13
+ include Fund::Arithmetic
14
+ include Comparable
15
+ include Fund::Comparision
16
+
17
+ attr_reader :value, :currency
18
+
19
+ # Default initializer for the fund object
20
+ #
21
+ # @param value [BigDecimal] A value representing the fund
22
+ # @param currency [Symbol, String] A value representing the fund currency
23
+ #
24
+ # @return [Fund]
25
+ #
26
+ # @example
27
+ # Fund.new(200)
28
+ # Fund.new(140.50, 'eur')
29
+ # Fund.new(1785.4, :pkr)
30
+ #
31
+ # @raise
32
+ # [ArgumentError] if provided currency is not valid
33
+ #
34
+ def initialize(value, currency = Fund.config.default_currency)
35
+ @value = prettify(value)
36
+ @currency = currency.to_sym
37
+
38
+ unless Fund.config.supported_currencies.include? @currency
39
+ raise ArgumentError.new("Provided currency #{@currency} is not enabled in Fund.config.supported_currencies")
40
+ end
41
+ end
42
+
43
+ def to_s
44
+ "#{@value} #{Fund.config.currency_symbols[@currency]}"
45
+ end
46
+
47
+ private
48
+
49
+ def prettify(value)
50
+ value == value.to_i ? value.to_i : value.round(Fund.config.round_limit)
51
+ end
52
+ end
53
+
54
+ Fund.load_default
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fund
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nazar Hussain
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-05-08 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
+ description: If you ever need to build a ruby based app where you need currency conversion.
56
+ This gem will help you in every aspect.
57
+ email:
58
+ - nazarhussain@gmail.com
59
+ executables:
60
+ - fund
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - ".travis.yml"
67
+ - CODE_OF_CONDUCT.md
68
+ - Gemfile
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - exe/fund
74
+ - fund.gemspec
75
+ - lib/fund.rb
76
+ - lib/fund/arithmetic.rb
77
+ - lib/fund/comparision.rb
78
+ - lib/fund/configuration.rb
79
+ - lib/fund/conversion.rb
80
+ - lib/fund/default.rb
81
+ - lib/fund/version.rb
82
+ homepage: https://github.com/nazarhussain
83
+ licenses: []
84
+ metadata:
85
+ allowed_push_host: https://rubygems.org
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 2.5.1
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: A gem utilizing ruby power to handle money and currency conversion issues.
106
+ test_files: []