class DateNamedFile::Template

A Template is a model of a filename with a (restricted but) valid strftime formatting template embedded within.

Basically, you can use

Examples:

In all cases date/time parts must be in order (year, month, day, hour, minute, second).

NO support for mixing unix epoch with anything else. Why would you do that?

Constants

SUBSTITUTION_REGEXP

Attributes

matcher[R]

@return [Regexp] A regular expression that does its best to correctly match

filenames that follow the template. Also used to try to extract the embedded
date in a filename
template_string[R]

@return [String] the initial template string

Public Class Methods

new(template_string) click to toggle source

@param [String] template Template string with embedded strftime format string @example

tmpl = DateNamedFile::Template.new("mystuff_daily%Y%m%d.tsv")
# File lib/date_named_file/template.rb, line 54
def initialize(template_string)
  @template_string = template_string
  @matcher = template_matcher(template_string)
end

Public Instance Methods

at(date_ish) click to toggle source

Get a DateNamedFile::File for the given date/datetime @param [<anything date_ish>] date_ish (see forgiving_dateify) @return [DateNamedFile::File] DateNamedFile::File for the given date in this template

# File lib/date_named_file/template.rb, line 85
def at(date_ish)
  DatedFile.new(self, date_ish)
end
daily_after(date_ish) click to toggle source

Like daily_since, but don't include the start date @see daily_since

# File lib/date_named_file/template.rb, line 127
def daily_after(date_ish)
  daily_since(date_ish)[1..-1]
end
daily_since(start_date_ish) click to toggle source

Get a list of computed files based on all the dates from the given start through today, _including both ends_. @param [<anything date_ish>] start_date_ish (see forgiving_dateify) @return [DateNamedFile::File] for tomorrow (+24 hours)

# File lib/date_named_file/template.rb, line 110
def daily_since(start_date_ish)
  dt = forgiving_dateify(start_date_ish)
  if dt.to_date > DateTime.now.to_date
    []
  else
    daily_since(dt + 1).unshift(self.at(dt))
  end
end
daily_through_yesterday(date_ish) click to toggle source

Like daily_since, but don't include today @see daily_since

# File lib/date_named_file/template.rb, line 121
def daily_through_yesterday(date_ish)
  daily_since(date_ish)[0..-2]
end
filename_for(date_ish) click to toggle source

Compute the filename from plugging the given date_ish string/integer into the template @param [<anything date_ish>] date_ish (see forgiving_dateify) @return [String] the expanded filename

# File lib/date_named_file/template.rb, line 78
def filename_for(date_ish)
  forgiving_dateify(date_ish).strftime(template_string)
end
in_dir(dir) click to toggle source
# File lib/date_named_file/template.rb, line 70
def in_dir(dir)
  DateNamedFile::Directory.new(self, dir)
end
match?(filename) click to toggle source

Test to see if a filename matches the template @param [String] filename The string to test @return [Boolean]

# File lib/date_named_file/template.rb, line 63
def match?(filename)
  @matcher.match? filename
end
Also aliased as: matches?
matches?(filename)
Alias for: match?
now() click to toggle source

@return [DateNamedFile::File] DateNamedFile::File for today/right now

# File lib/date_named_file/template.rb, line 90
def now
  at DateTime.now
end
Also aliased as: today
template_matcher(template) click to toggle source
# File lib/date_named_file/template.rb, line 131
def template_matcher(template)
  regexp_string = SUBSTITUTION_REGEXP.each_with_object(Regexp.escape(template)) do |subpair, templ|
    pct, rxstring = *subpair
    templ.gsub!(pct, rxstring)
  end
  Regexp.new(regexp_string)
end
today()
Alias for: now
tomorrow() click to toggle source

@return [DateNamedFile::File] DateNamedFile::File for tomorrow (+24 hours)

# File lib/date_named_file/template.rb, line 97
def tomorrow
  at (DateTime.now + 1)
end
yesterday() click to toggle source

@return [DateNamedFile::File] DateNamedFile::File for yesterday (-24 hours)

# File lib/date_named_file/template.rb, line 102
def yesterday
  at (DateTime.now - 1)
end