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.



31
32
33
# File 'lib/zold/wallets.rb', line 31

def initialize(dir)
  @dir = dir
end

Instance Method Details

#allObject

Returns the list of their IDs (as plain text)



50
51
52
53
54
55
56
57
58
59
# File 'lib/zold/wallets.rb', line 50

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

#find(id) ⇒ Object



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

def find(id)
  raise 'Id can\'t be nil' if id.nil?
  raise 'Id must be of type Id' unless id.is_a?(Id)
  Zold::Wallet.new(File.join(path, id.to_s))
end

#pathObject



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

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

#to_s(curr_dir = Dir.pwd) ⇒ Object

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.



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

def to_s(curr_dir = Dir.pwd)
  curr_path = Pathname.new(path)
  dir_path = Pathname.new(curr_dir)
  curr_path.relative_path_from(dir_path).to_s
end