class DateNamedFile::DatedFile
Attributes
datetime[R]
Public Class Methods
from_filename(template, filename)
click to toggle source
# File lib/date_named_file/dated_file.rb, line 20 def self.from_filename(template, filename) raise Error.new("String #{filename} does not match template '#{template.template_string}'") unless template.match? filename newobject = self.new(template) newobject.datetime = newobject.extract_datetime_from_filename(filename) newobject end
new(template, date_ish=DateTime.now)
click to toggle source
@param [DateNamedFile::Template] template @param [<anything date_ish>] date_ish (see forgiving_dateify)
# File lib/date_named_file/dated_file.rb, line 15 def initialize(template, date_ish=DateTime.now) @template = template self.datetime = date_ish end
Public Instance Methods
<=>(other)
click to toggle source
# File lib/date_named_file/dated_file.rb, line 43 def <=>(other) if self.match? other.to_s self.datetime <=> extract_datetime_from_filename(other) else d2 = Dateish.forgiving_dateify(other) z = self.datetime <=> d2 end end
datetime=(date_ish)
click to toggle source
# File lib/date_named_file/dated_file.rb, line 33 def datetime=(date_ish) @datetime = Dateish.forgiving_dateify(date_ish) @path = Pathname.new(@template.filename_for(@datetime).to_s) __setobj__ @path end
extract_datetime_from_filename(str)
click to toggle source
# File lib/date_named_file/dated_file.rb, line 52 def extract_datetime_from_filename(str) if m = @template.matcher.match(str) Dateish.forgiving_dateify(m[1..-1].join('')) else DateTime.new(0) end end
match?(other)
click to toggle source
# File lib/date_named_file/dated_file.rb, line 39 def match?(other) @template.match? other.to_s end
open()
click to toggle source
# File lib/date_named_file/dated_file.rb, line 60 def open raise "File #{@path.to_s} doesn't exist" unless @path.exist? begin Zlib::GzipReader.open(@path) rescue Zlib::GzipFile::Error File.open(@path) end end
pretty_print(q)
click to toggle source
Override pretty-print so it shows up correctly in pry
# File lib/date_named_file/dated_file.rb, line 70 def pretty_print(q) q.text "<#{self.class}:#{@path}>" end
to_datetime()
click to toggle source
Defining to_datetime
allows Dateish.forgiving_datetime to deal with it directly
# File lib/date_named_file/dated_file.rb, line 29 def to_datetime self.datetime end