Class: Z4BC::Bank

Inherits:
Object
  • Object
show all
Defined in:
lib/z4blockchain.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(i) ⇒ Bank

Returns a new instance of Bank.



222
223
224
225
# File 'lib/z4blockchain.rb', line 222

def initialize i
  @id = i
  @wallet = PStore.new("db/#{i}.pstore")
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



219
220
221
# File 'lib/z4blockchain.rb', line 219

def id
  @id
end

#walletObject

Returns the value of attribute wallet.



220
221
222
# File 'lib/z4blockchain.rb', line 220

def wallet
  @wallet
end

Instance Method Details

#[](k) ⇒ Object



295
296
297
# File 'lib/z4blockchain.rb', line 295

def [] k
  @wallet.transaction { |db| db[k.to_s].to_f }
end

#burn(a) ⇒ Object



246
247
248
# File 'lib/z4blockchain.rb', line 246

def burn a
  MINES.transaction { |db| x = db[@id].to_f; db[@id] = x - a.to_f }
end

#credit(h = {}) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
# File 'lib/z4blockchain.rb', line 266

def credit h={}
  @wallet.transaction { |db|
    x = db[h[:to].to_s].to_f;
    db[h[:to].to_s] = x + h[:amount].to_f;
  }
  if !h.has_key?(:mint);
    decr(h[:amount]);
  else
    mine(h[:amount]);
  end
end

#debit(h = {}) ⇒ Object



278
279
280
281
282
283
284
# File 'lib/z4blockchain.rb', line 278

def debit h={}
  @wallet.transaction { |db|
    x = db[h[:from].to_s].to_f;
    db[h[:from].to_s] = x - h[:amount].to_f;
  }
  incr h[:amount].to_f
end

#decr(a) ⇒ Object



254
255
256
# File 'lib/z4blockchain.rb', line 254

def decr a
  BANKS.transaction { |db| x = db[@id].to_f; db[@id] = x - a.to_f }
end

#holdingsObject



232
233
234
235
236
237
238
239
240
# File 'lib/z4blockchain.rb', line 232

def holdings
  x = 0
  @wallet.transaction {|db|
    db.keys.each { |e|
      x += db[e].to_f
    }
  }
  return x
end

#incr(a) ⇒ Object



250
251
252
# File 'lib/z4blockchain.rb', line 250

def incr a
  BANKS.transaction { |db| x = db[@id].to_f; db[@id] = x + a.to_f }
end

#key(k) ⇒ Object



227
228
229
230
# File 'lib/z4blockchain.rb', line 227

def key k
  #Z4blockchain.digest(%[#{@id}:#{k}])
  %[#{@id}:#{k}]
end

#liquidityObject



258
259
260
# File 'lib/z4blockchain.rb', line 258

def liquidity
  BANKS.transaction { |db| db[@id].to_f }
end

#mine(a) ⇒ Object



242
243
244
# File 'lib/z4blockchain.rb', line 242

def mine a
  MINES.transaction { |db| x = db[@id].to_f; db[@id] = x + a.to_f }
end

#sizeObject



262
263
264
# File 'lib/z4blockchain.rb', line 262

def size
  @wallet.transaction { |db| db.keys.length }
end

#xfer(h = {}) ⇒ Object



286
287
288
289
290
291
292
293
# File 'lib/z4blockchain.rb', line 286

def xfer h={}
  @wallet.transaction { |db|
    x = db[h[:from].to_s].to_f;
    db[h[:from].to_s] = x - h[:amount].to_f; 
    x = db[h[:to].to_s].to_f;
    db[h[:to].to_s] = x + h[:amount].to_f;
  }
end