investtools-money 0.0.1

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: 56f275fec57301ecdff093b39c5ca310810a7c25
4
+ data.tar.gz: 9b73dc802a5f73fccf2c33a21f3f89a8ab375341
5
+ SHA512:
6
+ metadata.gz: aabfc21e2e69de9fa2019d229f3c94cbb8e04bc9fbfe142a5befaec32a2b4424df76b7cac85fae803485b3027dc67ed90be65aaee660402005983552bdd68f9f
7
+ data.tar.gz: d0b4850238b0156cce21b706bdfd255fb5f0683272de92407bc7b6e15690209d8442092479d7da977ff2f19ecfb2ea0fe596aee23505d2c47fc3a5a2de43654e
data/.gitignore ADDED
@@ -0,0 +1,14 @@
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
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --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
data/Guardfile ADDED
@@ -0,0 +1,77 @@
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 feature)
6
+
7
+ ## Uncomment to clear the screen before every task
8
+ # clearing :on
9
+
10
+ ## Guard internally checks for changes in the Guardfile and exits.
11
+ ## If you want Guard to automatically start up again, run guard in a
12
+ ## shell loop, e.g.:
13
+ ##
14
+ ## $ while bundle exec guard; do echo "Restarting Guard..."; done
15
+ ##
16
+ ## Note: if you are using the `directories` clause above and you are not
17
+ ## watching the project directory ('.'), the you will want to move the Guardfile
18
+ ## to a watched dir and symlink it back, e.g.
19
+ #
20
+ # $ mkdir config
21
+ # $ mv Guardfile config/
22
+ # $ ln -s config/Guardfile .
23
+ #
24
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
25
+
26
+ # Note: The cmd option is now required due to the increasing number of ways
27
+ # rspec may be run, below are examples of the most common uses.
28
+ # * bundler: 'bundle exec rspec'
29
+ # * bundler binstubs: 'bin/rspec'
30
+ # * spring: 'bin/rspec' (This will use spring if running and you have
31
+ # installed the spring binstubs per the docs)
32
+ # * zeus: 'zeus rspec' (requires the server to be started separately)
33
+ # * 'just' rspec: 'rspec'
34
+
35
+ guard :rspec, cmd: "bundle exec rspec" do
36
+ require "guard/rspec/dsl"
37
+ dsl = Guard::RSpec::Dsl.new(self)
38
+
39
+ # Feel free to open issues for suggestions and improvements
40
+
41
+ # RSpec files
42
+ rspec = dsl.rspec
43
+ watch(rspec.spec_helper) { rspec.spec_dir }
44
+ watch(rspec.spec_support) { rspec.spec_dir }
45
+ watch(rspec.spec_files)
46
+
47
+ # Ruby files
48
+ ruby = dsl.ruby
49
+ dsl.watch_spec_files_for(ruby.lib_files)
50
+
51
+ # Rails files
52
+ rails = dsl.rails(view_extensions: %w(erb haml slim))
53
+ dsl.watch_spec_files_for(rails.app_files)
54
+ dsl.watch_spec_files_for(rails.views)
55
+
56
+ watch(rails.controllers) do |m|
57
+ [
58
+ rspec.spec.("routing/#{m[1]}_routing"),
59
+ rspec.spec.("controllers/#{m[1]}_controller"),
60
+ rspec.spec.("acceptance/#{m[1]}")
61
+ ]
62
+ end
63
+
64
+ # Rails config changes
65
+ watch(rails.spec_helper) { rspec.spec_dir }
66
+ watch(rails.routes) { "#{rspec.spec_dir}/routing" }
67
+ watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }
68
+
69
+ # Capybara features specs
70
+ watch(rails.view_dirs) { |m| rspec.spec.("features/#{m[1]}") }
71
+
72
+ # Turnip features and steps
73
+ watch(%r{^spec/acceptance/(.+)\.feature$})
74
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
75
+ Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
76
+ end
77
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 André Aizim Kelmanson
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.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Money
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'money'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install money
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/money/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1 @@
1
+ require 'money'
data/lib/money.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'money/base'
2
+ require 'money/money'
3
+ require 'money/attributes'
4
+ require 'money/currency'
5
+ require 'money/version'
@@ -0,0 +1,25 @@
1
+ require 'money/money'
2
+
3
+ class Money
4
+ module Attributes
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+
9
+ protected
10
+
11
+ def money(field_name)
12
+ define_method(field_name) do
13
+ Money.new(super(), security.currency.code, date)
14
+ end
15
+ define_method("#{field_name}=") do |value|
16
+ if value.kind_of?(Money)
17
+ super(value.amount)
18
+ else
19
+ super(value)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
data/lib/money/base.rb ADDED
@@ -0,0 +1,2 @@
1
+ class Money < Struct.new(:amount, :currency, :date)
2
+ end
@@ -0,0 +1,31 @@
1
+ require 'money/money'
2
+
3
+ class Money
4
+ class Currency < Struct.new(:id)
5
+
6
+ def self.exchange(&block)
7
+ @@exchange_handler = block
8
+ end
9
+
10
+ def self.[](currency)
11
+ return currency if currency.kind_of?(self)
12
+ Currency.new(currency)
13
+ end
14
+
15
+ def to(other_currency, on:)
16
+ other_currency = Currency[other_currency]
17
+ return Money.new(1.0, self, on) if self == other_currency
18
+ Money.new(call_exchange_handler(other_currency, on), other_currency, on)
19
+ end
20
+
21
+ def to_s
22
+ id
23
+ end
24
+
25
+ protected
26
+
27
+ def call_exchange_handler(other_currency, date)
28
+ @@exchange_handler.call(id, other_currency.id, date)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,56 @@
1
+ require 'money/base'
2
+ require 'money/currency'
3
+
4
+ class Money
5
+ def initialize(amount, currency, date=nil)
6
+ super amount, Currency[currency], date
7
+ end
8
+
9
+ def exchange_to(other_currency, on: date)
10
+ currency.to(Currency[other_currency], on: on) * amount
11
+ end
12
+
13
+ def +(other)
14
+ calculate(:+, other)
15
+ end
16
+
17
+ def -(other)
18
+ calculate(:-, other)
19
+ end
20
+
21
+ def /(other)
22
+ calculate(:/, other)
23
+ end
24
+
25
+ def *(other)
26
+ calculate(:*, other)
27
+ end
28
+
29
+
30
+ def coerce(other)
31
+ [Money.new(other, currency, date), amount]
32
+ end
33
+
34
+ def to_s
35
+ if date.nil?
36
+ "#{amount} #{currency}"
37
+ else
38
+ "#{amount} #{currency} on #{date}"
39
+ end
40
+ end
41
+
42
+ def inspect
43
+ "<#Money #{to_s}>"
44
+ end
45
+
46
+ protected
47
+
48
+ def calculate(operation, other)
49
+ other = other.amount if other.kind_of?(Money)
50
+ if other.kind_of?(Numeric)
51
+ Money.new(amount.send(operation, other), currency, date)
52
+ else
53
+ raise TypeError, "#{other.class} can't be coerced into Money"
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,5 @@
1
+ require 'money/base'
2
+
3
+ class Money
4
+ VERSION = "0.0.1"
5
+ end
data/money.gemspec ADDED
@@ -0,0 +1,26 @@
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 = "investtools-money"
8
+ spec.version = Money::VERSION
9
+ spec.authors = ["André Aizim Kelmanson"]
10
+ spec.email = ["akelmanson@gmail.com"]
11
+ spec.summary = %q{A Ruby Library for dealing with money and currency conversion.}
12
+ spec.description = %q{A Ruby Library for dealing with money and currency conversion.}
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 "guard-rspec"
24
+ spec.add_development_dependency "simplecov"
25
+ spec.add_development_dependency "terminal-notifier-guard"
26
+ end
@@ -0,0 +1,40 @@
1
+ require 'money/currency'
2
+
3
+ describe Money::Currency do
4
+ describe '.[]' do
5
+ let(:usd) { Money::Currency.new('USD') }
6
+ it 'creates a now currency object' do
7
+ expect(Money::Currency['USD']).to eq usd
8
+ end
9
+ context 'when Currency is given' do
10
+ it 'returns the given object' do
11
+ expect(Money::Currency[usd]).to be usd
12
+ end
13
+ end
14
+ end
15
+ describe '#to' do
16
+ let(:usd) { Money::Currency['USD'] }
17
+ let(:exchange_handler) { Proc.new { |a, b, date| 5.0 } }
18
+ before do
19
+ Money::Currency.exchange(&exchange_handler)
20
+ end
21
+ it 'calls exchange handler' do
22
+ expect(exchange_handler)
23
+ .to receive(:call)
24
+ .with 'USD', 'BRL', Date.parse('2011-01-01')
25
+ usd.to 'BRL', on: Date.parse('2011-01-01')
26
+ end
27
+
28
+ it 'wraps exchange handler result in Money' do
29
+ expect(usd.to 'BRL', on: Date.parse('2011-01-01'))
30
+ .to eq Money.new(5.0, 'BRL', Date.parse('2011-01-01'))
31
+ end
32
+
33
+ context 'when converting to the same currency' do
34
+ it 'returns 1.0 with the same currency' do
35
+ expect(usd.to 'USD', on: Date.parse('2011-01-01'))
36
+ .to eq Money.new(1.0, 'USD', Date.parse('2011-01-01'))
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,94 @@
1
+ require 'money/money'
2
+
3
+ describe Money do
4
+ describe '#exchange_to' do
5
+ before do
6
+ Money::Currency.exchange do |a, b, date|
7
+ 1.5
8
+ end
9
+ end
10
+ it 'finds the rate and multiplies by the amount' do
11
+ expect(Money.new(1_000.0, 'BRL').exchange_to 'USD').to eq Money.new(1_500.0, 'USD')
12
+ end
13
+ end
14
+ describe '#-' do
15
+ it 'substracts the given value from amount and returns a new object' do
16
+ expect(Money.new(1_000.0, 'BRL') - 100.0).to eq Money.new(900.0, 'BRL')
17
+ end
18
+ context 'when the given value is a Money' do
19
+ it 'substracts the given money amount from amount and returns a new Money' do
20
+ expect(Money.new(1_000.0, 'BRL') - Money.new(100.0, 'BRL')).to eq Money.new(900.0, 'BRL')
21
+ end
22
+ end
23
+ context 'when the given value is not recognized' do
24
+ it 'raises an error' do
25
+ expect { Money.new(1_000.0, 'BRL') - 'TEST' }.to raise_error(TypeError)
26
+ end
27
+ end
28
+ end
29
+ describe '#/' do
30
+ it 'divides the amount by the given value and returns a new object' do
31
+ expect(Money.new(1_000.0, 'BRL') / 100.0).to eq Money.new(10.0, 'BRL')
32
+ end
33
+ context 'when the given value is a Money' do
34
+ it 'divides the amount by the given money amount and returns a new Money' do
35
+ expect(Money.new(1_000.0, 'BRL') / Money.new(100.0, 'BRL')).to eq Money.new(10.0, 'BRL')
36
+ end
37
+ end
38
+ context 'when the given value is not recognized' do
39
+ it 'raises an error' do
40
+ expect { Money.new(1_000.0, 'BRL') / 'TEST' }.to raise_error(TypeError)
41
+ end
42
+ end
43
+ end
44
+ describe '#*' do
45
+ it 'multiplies the amount by the given value and returns a new object' do
46
+ expect(Money.new(1_000.0, 'BRL') * 100.0).to eq Money.new(100_000.0, 'BRL')
47
+ end
48
+ context 'when the given value is a Money' do
49
+ it 'multiplies the amount by the given money amount and returns a new Money' do
50
+ expect(Money.new(1_000.0, 'BRL') * Money.new(100.0, 'BRL')).to eq Money.new(100_000.0, 'BRL')
51
+ end
52
+ end
53
+ context 'when the given value is not recognized' do
54
+ it 'raises an error' do
55
+ expect { Money.new(1_000.0, 'BRL') * 'TEST' }.to raise_error(TypeError)
56
+ end
57
+ end
58
+ end
59
+ describe '#+' do
60
+ it 'adds the amount to the given value and returns a new object' do
61
+ expect(Money.new(1_000.0, 'BRL') + 100.0).to eq Money.new(1_100.0, 'BRL')
62
+ end
63
+ context 'when the given value is a Money' do
64
+ it 'adds the amount to the given money amount and returns a new Money' do
65
+ expect(Money.new(1_000.0, 'BRL') + Money.new(100.0, 'BRL')).to eq Money.new(1_100.0, 'BRL')
66
+ end
67
+ end
68
+ context 'when the given value is not recognized' do
69
+ it 'raises an error' do
70
+ expect { Money.new(1_000.0, 'BRL') + 'TEST' }.to raise_error(TypeError)
71
+ end
72
+ end
73
+ end
74
+ describe '#coerce' do
75
+ it 'wraps first operator in a Money' do
76
+ expect(1_500.0 - Money.new(1_000.0, 'BRL')).to eq Money.new(500.0, 'BRL')
77
+ end
78
+ end
79
+ describe '#to_s' do
80
+ it 'is "(amount) (currency)"' do
81
+ expect(Money.new(100.0, 'BRL').to_s).to eq '100.0 BRL'
82
+ end
83
+ context 'when date is defined' do
84
+ it 'is "(amount) (currency) on (date)"' do
85
+ expect(Money.new(100.0, 'BRL', Date.parse('2011-01-01')).to_s).to eq '100.0 BRL on 2011-01-01'
86
+ end
87
+ end
88
+ end
89
+ describe '#inspect' do
90
+ it 'is "<#Money (to_s)>"' do
91
+ expect(Money.new(100.0, 'BRL').inspect).to eq '<#Money 100.0 BRL>'
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,3 @@
1
+ require 'simplecov'
2
+ SimpleCov.minimum_coverage 100
3
+ SimpleCov.start
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: investtools-money
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - André Aizim Kelmanson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-20 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: guard-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
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
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: terminal-notifier-guard
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
+ description: A Ruby Library for dealing with money and currency conversion.
84
+ email:
85
+ - akelmanson@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - Gemfile
93
+ - Guardfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - lib/investtools/money.rb
98
+ - lib/money.rb
99
+ - lib/money/attributes.rb
100
+ - lib/money/base.rb
101
+ - lib/money/currency.rb
102
+ - lib/money/money.rb
103
+ - lib/money/version.rb
104
+ - money.gemspec
105
+ - spec/lib/money/currency_spec.rb
106
+ - spec/lib/money/money_spec.rb
107
+ - spec/spec_helper.rb
108
+ homepage: ''
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.2.2
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: A Ruby Library for dealing with money and currency conversion.
132
+ test_files:
133
+ - spec/lib/money/currency_spec.rb
134
+ - spec/lib/money/money_spec.rb
135
+ - spec/spec_helper.rb