Class: Zold::Wallets

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

Overview

Collection of local wallets

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ Wallets

Returns a new instance of Wallets.



34
35
36
# File 'lib/zold/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 + Wallet::EXT))| ... } ⇒ Object

Yields:



64
65
66
67
68
69
# File 'lib/zold/wallets.rb', line 64

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, #{id.class.name} instead" unless id.is_a?(Id)
  yield Wallet.new(File.join(path, id.to_s + Wallet::EXT))
end

#allObject

Returns the list of their IDs (as plain text)



53
54
55
56
57
58
59
60
61
62
# File 'lib/zold/wallets.rb', line 53

def all
  DirItems.new(path).fetch(recursive: false).select do |f|
    file = File.join(@dir, f)
    basename = File.basename(f, Wallet::EXT)
    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

#pathObject



47
48
49
50
# File 'lib/zold/wallets.rb', line 47

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

#to_sObject



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

def to_s
  mine = Pathname.new(File.expand_path(@dir))
  home = Pathname.new(File.expand_path(Dir.pwd))
  mine.relative_path_from(home).to_s
end