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.



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

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:



72
73
74
75
76
77
# File 'lib/zold/wallets.rb', line 72

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)



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

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



79
80
81
82
83
84
# File 'lib/zold/wallets.rb', line 79

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

#exists?(id) ⇒ Boolean

This wallet exists?

Returns:

  • (Boolean)


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

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

#pathObject



50
51
52
53
# File 'lib/zold/wallets.rb', line 50

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

#to_sObject

TODO:

#70:30min Let’s make it smarter. Instead of returning the full path let’s substract the prefix from it if it’s equal to the current directory in Dir.pwd.



44
45
46
47
48
# File 'lib/zold/wallets.rb', line 44

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