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:



67
68
69
70
71
72
# File 'lib/zold/wallets.rb', line 67

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)



56
57
58
59
60
61
62
63
64
65
# File 'lib/zold/wallets.rb', line 56

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



74
75
76
77
78
79
# File 'lib/zold/wallets.rb', line 74

def count
  Zold::DirItems.new(@dir)
    .fetch(recursive: false)
    .select { |f| f.end_with?(Wallet::EXT) }
    .count
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