class Zold::TreeWallets

Collection of local wallets, in a tree of directories

Public Class Methods

new(dir) click to toggle source
# File lib/zold/tree_wallets.rb, line 34
def initialize(dir)
  @dir = dir
end

Public Instance Methods

acq(id, exclusive: false) { |wallet( join(path, (id.split('', 5).take(4) + [id]).join('/') + EXT)| ... } click to toggle source
# File lib/zold/tree_wallets.rb, line 60
def acq(id, exclusive: false)
  raise 'The flag can\'t be nil' if exclusive.nil?
  raise 'Id can\'t be nil' if id.nil?
  raise 'Id must be of type Id' unless id.is_a?(Id)
  yield Wallet.new(
    File.join(path, (id.to_s.split('', 5).take(4) + [id.to_s]).join('/') + Wallet::EXT)
  )
end
all() click to toggle source

Returns the list of their IDs (as plain text)

# File lib/zold/tree_wallets.rb, line 48
def all
  DirItems.new(path).fetch.select do |f|
    next unless f.end_with?(Wallet::EXT)
    basename = File.basename(f, Wallet::EXT)
    file = File.join(path, f)
    File.file?(file) &&
      !File.directory?(file) &&
      basename =~ /^[0-9a-fA-F]{16}$/ &&
      Id.new(basename).to_s == basename
  end.map { |w| Id.new(File.basename(w, Wallet::EXT)) }
end
count() click to toggle source
# File lib/zold/tree_wallets.rb, line 69
def count
  `find #{@dir} -name "*.z" | wc -l`.strip.to_i
end
path() click to toggle source
# File lib/zold/tree_wallets.rb, line 42
def path
  FileUtils.mkdir_p(@dir)
  File.expand_path(@dir)
end
to_s() click to toggle source
# File lib/zold/tree_wallets.rb, line 38
def to_s
  path
end