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.



20
21
22
# File 'lib/zold/wallets.rb', line 20

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:



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

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)



44
45
46
47
48
49
50
51
52
53
# File 'lib/zold/wallets.rb', line 44

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

#countObject



62
63
64
65
66
# File 'lib/zold/wallets.rb', line 62

def count
  Zold::DirItems.new(@dir)
    .fetch(recursive: false)
    .count { |f| f.end_with?(Wallet::EXT) }
end

#exists?(id) ⇒ Boolean

This wallet exists?

Returns:

  • (Boolean)


39
40
41
# File 'lib/zold/wallets.rb', line 39

def exists?(id)
  File.exist?(File.join(path, id.to_s + Wallet::EXT))
end

#pathObject



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

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

#to_sObject



27
28
29
30
31
# File 'lib/zold/wallets.rb', line 27

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