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.



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

def initialize(dir)
  @dir = dir
end

Instance Method Details

#allObject

Returns the list of their IDs (as plain text)



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

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) {|Zold::Wallet.new(File.join(path, id.to_s))| ... } ⇒ Object

Yields:



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

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

#pathObject



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

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.



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

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