Module: HistoryFile
- Defined in:
- lib/history_file/history_file.rb,
lib/history_file/file_delegator.rb
Overview
Behaves like a ‘File` class and does some convenience stuff around a FileDelegator instance. It all revolves about defining a time offset. If however, you want to access different versions of a file, use it like this:
> f = HistoryFile[1.day.ago].new("/tmp/foo.txt", "w")
=> #<File:/tmp/2012.11.02-foo.txt>
The returned FileDelegator object supports all methods that File has, but adds a date prefix to those methods that revolve around a single file (reading, writing, etc.)
If a file for a given date is not available, HistoryFile falls back to the freshest file that is older than the given date.
> f = HistoryFile[3.days.ago].new("test.txt", "w")
=> #<File:./2012.11.12-test.txt>
> f.write("I am old")
=> 8
> f.close
=> nil
> HistoryFile[Date.today].read("test.txt")
=> "I am old"
> HistoryFile[10.days.ago].read("test.txt")
Errno::ENOENT: No such file or directory - ./2012.11.05-test.txt
Defined Under Namespace
Classes: FileDelegator
Class Method Summary collapse
Class Method Details
.[](offset) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/history_file/history_file.rb', line 36 def self.[](offset) validate_offset(offset) use_subdirs = @mode == :subdir fallback_glob = "[0-9][0-9][0-9][0-9].[0-9][0-9].[0-9][0-9]" FileDelegator.new(prefix: prefix(offset), fallback_glob: fallback_glob, use_subdirs: use_subdirs) end |
.mode=(mode) ⇒ Object
30 31 32 33 34 |
# File 'lib/history_file/history_file.rb', line 30 def self.mode=(mode) return @mode = :filename if mode.to_sym == :filename return @mode = :subdir if mode.to_sym == :subdir raise ArgumentError, "Mode must be :filename or :subdir" end |