Skip to content

Instantly share code, notes, and snippets.

Created February 15, 2018 01:43
Show Gist options
  • Save anonymous/9cfbe85834e4978a1e10e1c33ca055b3 to your computer and use it in GitHub Desktop.
Save anonymous/9cfbe85834e4978a1e10e1c33ca055b3 to your computer and use it in GitHub Desktop.
#!/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