Class: Zold::Wallets
- Inherits:
-
Object
- Object
- Zold::Wallets
- Defined in:
- lib/zold/wallets.rb
Overview
Collection of local wallets
Instance Method Summary collapse
- #acq(id, exclusive: false) {|Wallet.new(File.join(path, id.to_s + Wallet::EXT))| ... } ⇒ Object
-
#all ⇒ Object
Returns the list of their IDs (as plain text).
-
#initialize(dir) ⇒ Wallets
constructor
A new instance of Wallets.
- #path ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(dir) ⇒ Wallets
Returns a new instance of Wallets.
34 35 36 |
# File 'lib/zold/wallets.rb', line 34 def initialize(dir) @dir = dir end |
Instance Method Details
#acq(id, exclusive: false) {|Wallet.new(File.join(path, id.to_s + Wallet::EXT))| ... } ⇒ Object
64 65 66 67 68 69 |
# File 'lib/zold/wallets.rb', line 64 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 |
#all ⇒ Object
Returns the list of their IDs (as plain text)
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/zold/wallets.rb', line 53 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 |
#path ⇒ Object
47 48 49 50 |
# File 'lib/zold/wallets.rb', line 47 def path FileUtils.mkdir_p(@dir) File.(@dir) end |
#to_s ⇒ Object
41 42 43 44 45 |
# File 'lib/zold/wallets.rb', line 41 def to_s mine = Pathname.new(File.(@dir)) home = Pathname.new(File.(Dir.pwd)) mine.relative_path_from(home).to_s end |