Created
May 13, 2013 22:39
-
-
Save gogogarrett/5572114 to your computer and use it in GitHub Desktop.
A very simplistic implementation of a point ledger class that adds and deducts by creating user point transactions.
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 PointLedger | |
| attr_reader :student | |
| def initialize(student) | |
| @student = student | |
| end | |
| def add(amount, item) | |
| create_point_transaction(amount, item) | |
| student.points += amount | |
| student | |
| end | |
| def deduct(amount, item) | |
| create_point_transaction(-amount, item) | |
| student.points -= amount | |
| student | |
| end | |
| private | |
| def create_point_transaction(amount, item) | |
| student.point_transactions.build(amount: amount, item_type: item.class.to_s, item_name: item.name) | |
| end | |
| 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 PurchaseCalc | |
| def possible?(points, item_price) | |
| points >= item_price | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment