Class: Bank::Models::Account
- Inherits:
-
Object
- Object
- Bank::Models::Account
- Includes:
- Mongoid::Document, Mongoid::Timestamps
- Defined in:
- lib/bank/models/account.rb
Class Method Summary collapse
- .deposit(id, amount) ⇒ Object
- .open(params) ⇒ Object
- .transfer(from, to, amount) ⇒ Object
- .withdraw(id, amount) ⇒ Object
Class Method Details
.deposit(id, amount) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/bank/models/account.rb', line 19 def self.deposit(id, amount) puts "Depositing #{amount} on account #{id}" if amount <= 0 puts 'Deposit failed! Amount must be greater than 0.00' return false end account = self.find_by(id: id) account.balance = (account.balance += amount).round(2) account.save! end |
.open(params) ⇒ Object
14 15 16 17 |
# File 'lib/bank/models/account.rb', line 14 def self.open(params) puts "Creating a account with #{params}" self.create!(params) end |
.transfer(from, to, amount) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/bank/models/account.rb', line 45 def self.transfer(from, to, amount) puts "Transfering #{amount} from account #{from} to account #{to}" if amount <= 0 puts 'Transfer failed! Amount must be greater than 0.00' return false end self.deposit(to, amount) self.withdraw(from, amount) end |
.withdraw(id, amount) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/bank/models/account.rb', line 32 def self.withdraw(id, amount) puts "Withdrawing #{amount} on account #{id}" if amount <= 0 puts 'Withdraw failed! Amount must be greater than 0.00' return false end account = self.find_by(id: id) account.balance = (account.balance -= amount).round(2) account.save! end |