Class: Amp::Repositories::Stores::FilenameCache::FilenameCacheOpener
Overview
FilenameCacheOpener
This opener handles a cache of filenames that we are currently tracking. This way we don’t need to recursively walk though the folders every single time. To use this class, you pass in the real Opener object (that responds to #open and returns a file pointer). then just treat it like any other opener. It will handle the behind-the-scenes work itself.
Instance Attribute Summary
Attributes inherited from Opener
Instance Method Summary collapse
-
#initialize(opener) ⇒ FilenameCacheOpener
constructor
Initializes a new FNCacheOpener.
-
#load_filename_cache ⇒ Object
Parses the filename cache and loads it into an ivar.
-
#open(path, mode = 'r', &block) ⇒ Object
Opens a file while being sure to write the filename if we haven’t seen it before.
- #path ⇒ Object (also: #root)
Methods inherited from Opener
#join, #open_file, #open_hg, #open_up_file, #read
Constructor Details
#initialize(opener) ⇒ FilenameCacheOpener
Initializes a new FNCacheOpener. Requires a normal object capable of opening files.
219 220 221 222 |
# File 'lib/amp/repository/store.rb', line 219 def initialize(opener) @opener = opener @entries = nil end |
Instance Method Details
#load_filename_cache ⇒ Object
Parses the filename cache and loads it into an ivar.
229 230 231 232 233 234 |
# File 'lib/amp/repository/store.rb', line 229 def load_filename_cache @entries = {} FilenameCache.parse @opener do |f| @entries[f] = true end end |
#open(path, mode = 'r', &block) ⇒ Object
Opens a file while being sure to write the filename if we haven’t seen it before. Just like the normal Opener’s open() method.
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/amp/repository/store.rb', line 243 def open(path, mode='r', &block) if mode !~ /r/ && path =~ /data\// load_filename_cache if @entries.nil? if @entries[path].nil? @opener.open('fncache','ab') {|f| f.puts path } @entries[path] = true end end begin @opener.open(Stores.hybrid_encode(path), mode, &block) rescue Errno::ENOENT raise rescue raise unless mode == 'r' end rescue raise end |
#path ⇒ Object Also known as: root
224 |
# File 'lib/amp/repository/store.rb', line 224 def path; @opener.path; end |