mortgagerb 0.1.5

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
+ SHA256:
3
+ metadata.gz: 15d0932e45e8a052fd914fe4deec87393e381d67e8b3cfdd7303b3d96072375c
4
+ data.tar.gz: be02a4e36939c96e569540b0a67a9b07e744482fd3493ae330ab6c2dd2b9ee50
5
+ SHA512:
6
+ metadata.gz: bf8847fda2ddf9ddb30b1373dff35ba4845f47902079e321e9993a6b64b92eca62a63832683a51ef6fd83a1a794a08b4536b305ecfacf23de4d6ef35afac5c1a
7
+ data.tar.gz: fba108618cbaf0b44d918460ba2c9194586942b6744bef324234d29cac72c6276ffd9e20ba40bd8b7c74acca817131e3744951b0beab6e0da8296b8651fa0489
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-03-10
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Jason Kim
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,45 @@
1
+ # Mortgagerb
2
+
3
+ Calculates monthly payment for various types of mortgage. To learn more about how the calculations are done, see [amortizing loan](https://en.wikipedia.org/wiki/Amortizing_loan).
4
+
5
+ Currently supported loan types
6
+
7
+ - 30 year fixed
8
+ - Others coming soon
9
+
10
+ ## Installation
11
+
12
+ Install the gem and add to the application's Gemfile by executing:
13
+
14
+ $ bundle add mortgagerb
15
+
16
+ If bundler is not being used to manage dependencies, install the gem by executing:
17
+
18
+ $ gem install mortgagerb
19
+
20
+ ## Usage
21
+
22
+ ```ruby
23
+ require 'mortgagerb'
24
+
25
+ rate = 5.5
26
+ principal = 1000000
27
+ type = :thirty_year_fixed
28
+
29
+ scenario = Mortgagerb::Scenario.new(principal, rate, type)
30
+ amortization_payment = scenario.calculate # 5677.89
31
+ ```
32
+
33
+ ## Development
34
+
35
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
36
+
37
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
38
+
39
+ ## Contributing
40
+
41
+ Bug reports and pull requests are welcome on GitHub at https://github.com/serv/mortgagerb.
42
+
43
+ ## License
44
+
45
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[test rubocop]
@@ -0,0 +1,34 @@
1
+ module Mortgagerb
2
+ class Calculator
3
+ NUMBER_OF_MONTHS = 12
4
+
5
+ # r
6
+ def self.periodic_interest_rate(annual_rate)
7
+ annual_rate / 100 / NUMBER_OF_MONTHS
8
+ end
9
+
10
+ # n
11
+ def self.total_number_of_payments(years)
12
+ years * NUMBER_OF_MONTHS
13
+ end
14
+
15
+ # A
16
+ def self.periodic_amortization_payment(principal, rate, n)
17
+ rate_multiplier = rate_plus_one_power_to_n(rate, n)
18
+ (principal * (rate * rate_multiplier) / (rate_multiplier - 1)).round(2)
19
+ end
20
+
21
+ # P
22
+ def self.principal_from_amortization_payment(amortization_payment, rate, n)
23
+ rate_multiplier = rate_plus_one_power_to_n(rate, n)
24
+ (amortization_payment * (1 - (1 / rate_multiplier)) / rate).round(2)
25
+ end
26
+
27
+ private
28
+
29
+ # (1 + r) ^ n
30
+ def self.rate_plus_one_power_to_n(rate, n)
31
+ (1 + rate)**n
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,50 @@
1
+ module Mortgagerb
2
+ class Scenario
3
+ attr_accessor :amortization_payment, :principal
4
+
5
+ def initialize(amount, rate, type)
6
+ @amount = amount
7
+ @rate = rate
8
+ @type = type
9
+ @amortization_payment = nil
10
+ @principal = nil
11
+ end
12
+
13
+ def calculate
14
+ if %i[thirty_year_fixed three_year_arm five_year_arm seven_year_arm ten_year_arm].include?(@type)
15
+ r = Calculator.periodic_interest_rate(@rate)
16
+ n = Calculator.total_number_of_payments(30)
17
+ @amortization_payment = Calculator.periodic_amortization_payment(@amount, r, n)
18
+ elsif %i[thirty_year_fixed_get_pricipal three_year_arm_get_principal five_year_arm_get_principal
19
+ seven_year_arm_get_principal ten_year_arm_get_principal].include?(@type)
20
+ r = Calculator.periodic_interest_rate(@rate)
21
+ n = Calculator.total_number_of_payments(30)
22
+ @principal = Calculator.principal_from_amortization_payment(@amount, r, n)
23
+ elsif @type === :twenty_year_fixed
24
+ r = Calculator.periodic_interest_rate(@rate)
25
+ n = Calculator.total_number_of_payments(20)
26
+ @amortization_payment = Calculator.periodic_amortization_payment(@amount, r, n)
27
+ elsif @type === :twenty_year_fixed_get_principal
28
+ r = Calculator.periodic_interest_rate(@rate)
29
+ n = Calculator.total_number_of_payments(20)
30
+ @principal = Calculator.principal_from_amortization_payment(@amount, r, n)
31
+ elsif @type === :fifteen_year_fixed
32
+ r = Calculator.periodic_interest_rate(@rate)
33
+ n = Calculator.total_number_of_payments(15)
34
+ @amortization_payment = Calculator.periodic_amortization_payment(@amount, r, n)
35
+ elsif @type === :fifteen_year_fixed_get_principal
36
+ r = Calculator.periodic_interest_rate(@rate)
37
+ n = Calculator.total_number_of_payments(15)
38
+ @principal = Calculator.principal_from_amortization_payment(@amount, r, n)
39
+ elsif @type === :ten_year_fixed
40
+ r = Calculator.periodic_interest_rate(@rate)
41
+ n = Calculator.total_number_of_payments(10)
42
+ @amortization_payment = Calculator.periodic_amortization_payment(@amount, r, n)
43
+ elsif @type === :ten_year_fixed_get_principal
44
+ r = Calculator.periodic_interest_rate(@rate)
45
+ n = Calculator.total_number_of_payments(10)
46
+ @principal = Calculator.principal_from_amortization_payment(@amount, r, n)
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mortgagerb
4
+ VERSION = "0.1.5"
5
+ end
data/lib/mortgagerb.rb ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "mortgagerb/version"
4
+ require_relative "mortgagerb/calculator"
5
+ require_relative "mortgagerb/scenario"
6
+
7
+ module Mortgagerb
8
+ class Error < StandardError; end
9
+ # Your code goes here...
10
+ end
@@ -0,0 +1,4 @@
1
+ module Mortgagerb
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mortgagerb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.5
5
+ platform: ruby
6
+ authors:
7
+ - Jason Kim
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-11-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Calculate periodic payment given principal, rate and mortgage type
14
+ email:
15
+ - iamjsonkim@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rubocop.yml"
21
+ - CHANGELOG.md
22
+ - LICENSE.txt
23
+ - README.md
24
+ - Rakefile
25
+ - lib/mortgagerb.rb
26
+ - lib/mortgagerb/calculator.rb
27
+ - lib/mortgagerb/scenario.rb
28
+ - lib/mortgagerb/version.rb
29
+ - sig/mortgagerb.rbs
30
+ homepage: https://github.com/serv/mortgagerb
31
+ licenses:
32
+ - MIT
33
+ metadata:
34
+ homepage_uri: https://github.com/serv/mortgagerb
35
+ source_code_uri: https://github.com/serv/mortgagerb
36
+ changelog_uri: https://github.com/serv/mortgagerb
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: 3.3.0
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubygems_version: 3.5.16
53
+ signing_key:
54
+ specification_version: 4
55
+ summary: Mortgage calculator
56
+ test_files: []