Skip to content

Instantly share code, notes, and snippets.

@samdvr
Last active May 31, 2017 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samdvr/0e44d73fe3b05f0547504ed9cb411494 to your computer and use it in GitHub Desktop.
Save samdvr/0e44d73fe3b05f0547504ed9cb411494 to your computer and use it in GitHub Desktop.
Pure example
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