Skip to content

Instantly share code, notes, and snippets.

@svs
Created October 11, 2012 05:43
Show Gist options
  • Save svs/3870436 to your computer and use it in GitHub Desktop.
Save svs/3870436 to your computer and use it in GitHub Desktop.
valid_loan_product_implementation
def is_valid_loan_product(method)
loan_attr = self.send(method)
return [false, "No #{method} specified"] if not loan_attr or loan_attr===""
return [false, "No loan product chosen"] unless self.loan_product
product = self.loan_product
#Checking if the loan adheres to minimum and maximums of the loan product
{:min => :minimum, :max => :maximum}.each{|k, v|
product_attr = product.send("#{k}_#{method}")
if method==:interest_rate
product_attr = product_attr.to_f/100.round(6)
loan_attr = loan_attr.to_f.round(6)
end
if k==:min and loan_attr and product_attr and (product_attr - loan_attr > 0.000001)
return [false, "#{v.to_s.capitalize} #{method.to_s.humanize} limit violated"]
elsif k==:max and loan_attr and product_attr and (loan_attr - product_attr > 0.000001)
return [false, "#{v.to_s.capitalize} #{method.to_s.humanize} limit violated"]
end
}
#check if loan is follows the minimum discrete value for amount and interest
if product.respond_to?("#{method}_multiple")
product_attr = product.send("#{method}_multiple")
loan_attr = loan_attr*100 if method==:interest_rate
remainder = loan_attr.remainder(product_attr)
remainder = remainder/100 if method==:interest_rate
return [false, "#{method.to_s.capitalize} should be in multiples of #{product_attr}"] if not loan_attr or remainder > EPSILON
end
return true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment