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).
- #count ⇒ Object
-
#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.
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
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 |
#all ⇒ Object
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 |
#count ⇒ Object
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 |
#path ⇒ Object
50 51 52 53 |
# File 'lib/zold/wallets.rb', line 50 def path FileUtils.mkdir_p(@dir) File.(@dir) end |
#to_s ⇒ 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.
44 45 46 47 48 |
# File 'lib/zold/wallets.rb', line 44 def to_s mine = Pathname.new(File.(@dir)) home = Pathname.new(File.(Dir.pwd)) mine.relative_path_from(home).to_s end |