Created
February 15, 2018 01:43
-
-
Save anonymous/9cfbe85834e4978a1e10e1c33ca055b3 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
require 'sequel' | |
require 'json' | |
DB = Sequel.connect('sqlite://db/wallets.db') | |
class WalletCentral | |
def self.output(name=nil) | |
if name.nil? | |
DB[:wallets].select_hash_groups(:name, [:currency, :amount]).to_json | |
else | |
DB[:wallets].where(name: name).select_hash_groups(:name, [:currency, :amount]).to_json | |
end | |
end | |
def self.transfer(from, to, currency, amount) | |
from_amount = DB[:wallets].where(name: from, currency: currency).get(:amount) | |
if from_amount >= 0 | |
DB[:wallets].where(name: from, currency: currency).update(amount: Sequel[:amount]-amount) | |
DB[:wallets].where(name: to, currency: currency).update(amount: Sequel[:amount]+amount) | |
else | |
raise "Amount insufficient" | |
end | |
end | |
end | |
puts WalletCentral.output | |
WalletCentral.transfer('jon', 'jaime', 'USD', 100.0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment