Class: HistoryFile::FileDelegator
- Inherits:
-
Object
- Object
- HistoryFile::FileDelegator
- Defined in:
- lib/history_file/file_delegator.rb
Overview
This class delegates all method calls to the File class. The generic methods that don’t revolve around one specific file, File.join for example, are just passed on. These methods are defined in EXCLUDED_METHODS.
The methods that revolve around doing something with one specific file, however, will be called with an altered Filename. Consider this example:
> fd = HistoryFile::FileDelegator.new("some_prefix")
> fd.open("/path/to/my_file.txt", "w") do |io|
> io.puts "hello there"
> end
> puts fd.read("/path/to/my_file.txt")
=> hello there
This will pass on your block to File.open, but with a prefixed the filename. So what’s really called is:
File.open("/path/to/some_prefix-my_file.txt")
For methods that get a bunch of filenames, but only filenames, as arguments, all of the filenames are patched to include the date prefix. These methods are defined in BULK_METHODS
You shouldn’t need to instanciate this class directly, HistoryFile wraps this for you.
Constant Summary collapse
- EXCLUDED_METHODS =
[ :absolute_path, :basename, :catname, :chmod, :chown, :compare, :copy, :directory?, :dirname, :expand_path, :extname, :fnmatch, :fnmatch?, :identical?, :install, :join, :lchown, :link, :makedirs, :move, :path, :realdirpath, :realpath, :rename, :split, :umask, :utime ]
- BULK_METHODS =
[ :delete, :unlink, :safe_unlink ]
Instance Method Summary collapse
-
#delegate(*args, &block) ⇒ Object
Either - passes on the call directly to File (why am I not removing this feature?) or - adds the date prefix to the first argument or - adds the date prefix to all arguments.
-
#initialize(prefix, fallback_glob = nil) ⇒ FileDelegator
constructor
A new instance of FileDelegator.
- #prefixed_filename(f) ⇒ Object
Constructor Details
#initialize(prefix, fallback_glob = nil) ⇒ FileDelegator
Returns a new instance of FileDelegator.
74 75 76 77 |
# File 'lib/history_file/file_delegator.rb', line 74 def initialize(prefix, fallback_glob = nil) @prefix = prefix @fallback_glob = fallback_glob end |
Instance Method Details
#delegate(*args, &block) ⇒ Object
Either
-
passes on the call directly to File (why am I not removing this feature?) or
-
adds the date prefix to the first argument or
-
adds the date prefix to all arguments
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/history_file/file_delegator.rb', line 84 def delegate(*args, &block) method = args.slice!(0,1).first if EXCLUDED_METHODS.include?(method) File.send(method, *args, &block) elsif BULK_METHODS.include?(method) delegate_with_patched_filenames(method, *args, &block) else delegate_with_patched_filename(method, *args, &block) end end |
#prefixed_filename(f) ⇒ Object
95 96 97 98 99 |
# File 'lib/history_file/file_delegator.rb', line 95 def prefixed_filename(f) dir = File.dirname(f.to_s) file = File.basename(f.to_s) File.join(dir, "#{@prefix}-#{file}") end |