Class: Zold::TreeWallets

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

Overview

Collection of local wallets, in a tree of directories

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ TreeWallets

Returns a new instance of TreeWallets.



34
35
36
# File 'lib/zold/tree_wallets.rb', line 34

def initialize(dir)
  @dir = dir
end

Instance Method Details

#acq(id, exclusive: false) {|Wallet.new( File.join(path, (id.to_s.split('', 5).take(4) + [id.to_s]).join('/') + Wallet::EXT) )| ... } ⇒ Object

Yields:



60
61
62
63
64
65
66
67
# 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

#allObject

Returns the list of their IDs (as plain text)



48
49
50
51
52
53
54
55
56
57
58
# 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

#countObject



69
70
71
# File 'lib/zold/tree_wallets.rb', line 69

def count
  `find #{@dir} -name "*.z" | wc -l`.strip.to_i
end

#pathObject



42
43
44
45
# File 'lib/zold/tree_wallets.rb', line 42

def path
  FileUtils.mkdir_p(@dir)
  File.expand_path(@dir)
end

#to_sObject



38
39
40
# File 'lib/zold/tree_wallets.rb', line 38

def to_s
  path
end