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.



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

def initialize(dir)
  @dir = dir
end

Instance Method Details

#allObject

Returns the list of their IDs (as plain text)



47
48
49
50
51
52
53
54
55
# File 'lib/zold/tree_wallets.rb', line 47

def all
  Dir.glob("#{path}/**/*#{Wallet::EXTENSION}").select do |f|
    basename = File.basename(f, Wallet::EXTENSION)
    File.file?(f) &&
      !File.directory?(f) &&
      basename =~ /^[0-9a-fA-F]{16}$/ &&
      Id.new(basename).to_s == basename
  end.map { |w| Id.new(File.basename(w, Wallet::EXTENSION)) }
end

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

Yields:



57
58
59
60
61
# File 'lib/zold/tree_wallets.rb', line 57

def find(id)
  raise 'Id can\'t be nil' if id.nil?
  raise 'Id must be of type Id' unless id.is_a?(Id)
  yield Zold::Wallet.new(File.join(path, (id.to_s.split('', 5).take(4) + [id.to_s]).join('/')))
end

#pathObject



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

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

#to_sObject



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

def to_s
  path
end