Class: HistoryFile::FileDelegator

Inherits:
Object
  • Object
show all
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
]
POTENTIALLY_FILE_CREATING_METHODS =
[
  :new,
  :open
]

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ FileDelegator

Returns a new instance of FileDelegator.

Parameters:

  • prefix (String)

    The prefix for all methods that revolve around filenames

  • fallback_glob (Hash)

    If you want to fall back to an alphabetically smaller file on Errno::ENOENT, you can supply a :fallback_glob here. It will be used with ‘Dir.glob` to find all candidates (so this should match all prefixes)



79
80
81
82
83
# File 'lib/history_file/file_delegator.rb', line 79

def initialize(opts)
  @prefix = opts[:prefix] or raise ArgumentError,":prefix needed"
  @fallback_glob = opts[:fallback_glob]
  @subdir = opts[:use_subdirs]
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



90
91
92
93
94
95
96
97
98
99
# File 'lib/history_file/file_delegator.rb', line 90

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



101
102
103
104
105
106
107
108
109
# File 'lib/history_file/file_delegator.rb', line 101

def prefixed_filename(f)
  dir  = File.dirname(f.to_s)
  file = File.basename(f.to_s)
  if @subdir
    File.join(dir, @prefix, file)
  else
    File.join(dir, "#{@prefix}-#{file}")
  end
end