Class: Zold::Wallets
- Inherits:
-
Object
- Object
- Zold::Wallets
- Defined in:
- lib/zold/wallets.rb
Overview
Collection of local wallets
Instance Method Summary collapse
-
#all ⇒ Object
Returns the list of their IDs (as plain text).
- #find(id) {|Zold::Wallet.new(File.join(path, id.to_s))| ... } ⇒ 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.
33 34 35 |
# File 'lib/zold/wallets.rb', line 33 def initialize(dir) @dir = dir end |
Instance Method Details
#all ⇒ Object
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
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 |
#path ⇒ Object
46 47 48 49 |
# File 'lib/zold/wallets.rb', line 46 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.
40 41 42 43 44 |
# File 'lib/zold/wallets.rb', line 40 def to_s mine = Pathname.new(File.(@dir)) home = Pathname.new(File.(Dir.pwd)) mine.relative_path_from(home).to_s end |