Sha256: c8b13d08ddcc83ca288cd523dcf671528e10061b9a6974989868aee83ff397cb
Contents?: true
Size: 1.58 KB
Versions: 1
Compression:
Stored size: 1.58 KB
Contents
class Account class WithdrawCommand < CommandModel::Model parameter :amount, :typecast => :integer, :presence => true, :numericality => { :greater_than => 0, :less_than_or_equal_to => 500 } end class DepositCommand < CommandModel::Model parameter :amount, :typecast => :integer, :presence => true, :numericality => { :greater_than => 0 } end class TransferCommand < CommandModel::Model parameter :from, :to, :presence => true parameter :amount, :typecast => :integer, :presence => true, :numericality => { :greater_than => 0 } validate do |model| errors.add :base, "From and to accounts cannot be the same" if model.from == model.to end end attr_reader :name, :balance def initialize(name, balance) @name = name @balance = balance end def withdraw(options) WithdrawCommand.execute(options) do |command| if balance >= command.amount @balance -= command.amount else command.errors.add :amount, "is more than account balance" end end end def deposit(options) DepositCommand.execute(options) do |command| @balance += command.amount end end def self.transfer(options) TransferCommand.execute(options) do |command| if command.from.balance >= command.amount command.from.withdraw :amount => command.amount command.to.deposit :amount => command.amount else command.errors.add :amount, "is more than account balance" end end end def to_param name end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
command_model-1.0.0 | examples/bank/app/models/account.rb |