This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ sudo gem install gemcutter | |
$ gem tumble | |
$ sudo gem install pwfoo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def newton_raphson(f, f_deriv, start, precision = 5) | |
k_plus_one = start | |
k = 0.0 | |
while ((k - 1) * 10**precision).to_f.floor != ((k_plus_one - 1) * 10**precision).to_f.floor | |
k = k_plus_one | |
k_plus_one = k - f.call(k) / f_deriv.call(k) | |
end | |
k_plus_one | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
loan_amount = 350000 | |
=> 350000 | |
interest_rate = 4.75 | |
=> 4.75 | |
period = 30 * 12 | |
=> 360 | |
lender_fee = 800 | |
=> 800 | |
points = 1.0 | |
=> 1.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def assert_monthly_apr_payment_matches(loan_amount, rate, period, fee, points) | |
mortgage_util = MortgageUtil.new(loan_amount, rate, period, fee, points) | |
monthly_payment_with_fees = mortgage_util.monthly_payment_with_fees | |
monthly_payment_from_apr = MortgageUtil.new(loan_amount, mortgage_util.apr, period, 0, 0).monthly_payment | |
monthly_payment_with_fees.should be_close(monthly_payment_from_apr, 0.01) | |
end | |
it "should calculate original monthly payment from APR" do | |
assert_monthly_apr_payment_matches(300000, 6.5, 360, 1200, 1.25) | |
assert_monthly_apr_payment_matches(300000, 6.5, 360, 0, 0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
context "with valid MortgageUtil" do | |
before(:all) do | |
@mortgage_util = MortgageUtil.new(100000, 6.0, 360, 1200, 1.25) | |
end | |
it "should have proper monthly interest rate" do | |
@mortgage_util.send(:monthly_interst_rate).should == 0.005 | |
end | |
it "should have proper monthly payment" do | |
@mortgage_util.monthly_payment.should be_close(599.55, 0.001) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module DelayedJobExtensions | |
def delay_email_methods(*delay_me) | |
klass = (class << self; self end) | |
delay_me.each do |attr| | |
klass.send(:define_method, "delay_#{attr.to_s}".to_sym) do |*args| | |
if USE_DELAYED_JOB | |
self.delay.send("deliver_#{attr.to_s}".to_sym, *args) | |
else | |
self.send("deliver_#{attr.to_s}".to_sym, *args) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Emailer < ActionMailer::Base | |
helper :application | |
layout 'email' | |
extend DelayedJobExtensions | |
delay_email_methods :invite_customer | |
def invite_customer(cutomer_email) | |
recipients customer_email | |
from admin_email |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Emailer.delay_invite_customer('[email protected]') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module DelayedJobExtensions | |
def delay_email_methods(*delay_me) | |
klass = (class << self; self end) | |
delay_me.each do |attr| | |
klass.send(:define_method, "delay_#{attr.to_s}".to_sym) do |*args| | |
if defined?(DISABLE_DELAYED_JOB) && DISABLE_DELAYED_JOB | |
self.send("deliver_#{attr.to_s}".to_sym, *args) | |
else | |
self.delay.send("deliver_#{attr.to_s}".to_sym, *args) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module DelayedJobExtensions | |
class EmailJob < Struct.new(:deliver_class_name, :message) | |
def perform | |
Object.const_get(deliver_class_name).deliver(message) | |
end | |
end | |
def delay_email_methods(*delay_me) | |
klass = (class << self; self end) | |
delay_me.each do |attr| |