-
-
Save samdvr/0e44d73fe3b05f0547504ed9cb411494 to your computer and use it in GitHub Desktop.
Pure example
This file contains 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 Cafe | |
class NotSufficientMoney < StandardError | |
def self.charge(user_id,amount) | |
user = User.find(user_id) | |
raise NotSufficientMoney if user.balance < amount | |
tax = amount * 0.7 | |
Debit.create!(user, amount + tax) | |
end | |
end | |
#More Pure version | |
class Cafe | |
class NotSufficientMoney < StandardError | |
def self.charge(user_id,amount) | |
user = User.find(user_id) | |
raise NotSufficientMoney if user_does_not_have_enough_money?(user.balance, amount) | |
Debit.create!(user, amount + calculate_tax_for(amount)) | |
end | |
def user_does_not_have_enough_money?(balance, amount) | |
balance < amount | |
end | |
def calculate_tax_for(amount) | |
amount * 0.7 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment