Class: DateNamedFile::Template

Inherits:
Object
  • Object
show all
Includes:
DateishHelpers
Defined in:
lib/date_named_file/template.rb

Overview

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

Basically, you can use

  • %Y four=digit year

  • %m two-digit month

  • %d two-digit day

  • %H two-digit hour 00-23

  • %M minute

  • %S second

  • %s unix epoch time to the second

  • %Q unix epoch time to the millisecond

  • %1 arbitrary integer (for, e.g., log files. Not part of strftime)

Examples:

  • daily_update_%Y-%m-%d.txt

  • mydaemon_%Y_%m_%d_%H%M.log

  • updates%Y%m%d_dev.ndj.gz

  • mylog%s.log

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?

Direct Known Subclasses

Directory

Constant Summary collapse

SUBSTITUTION_REGEXP =
{}

Constants included from DateishHelpers

DateishHelpers::ALL_DIGITS, DateishHelpers::VALID_DELIMITERS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DateishHelpers

#datetime_from_parts, #digit_string?, #ditch_leading_delimiters, #extract_delimited_datetime, #extract_from_digitstring, #extract_non_year_parts, #extract_rest, #extract_undelimited_datetime, #extract_unix_timestamp, #extract_year, #forgiving_dateify, #looks_like_unix_timestamp?, #perform_simple_transforms, #validate_delimited_datetime!, #validate_parts!

Constructor Details

#initialize(template_string) ⇒ Template

Returns a new instance of Template.

Examples:

tmpl = DateNamedFile::Template.new("mystuff_daily%Y%m%d.tsv")

Parameters:

  • template (String)

    Template string with embedded strftime format string



54
55
56
57
# File 'lib/date_named_file/template.rb', line 54

def initialize(template_string)
  @template_string = template_string
  @matcher = template_matcher(template_string)
end

Instance Attribute Details

#matcherRegexp (readonly)

Returns 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.

Returns:

  • (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



48
49
50
# File 'lib/date_named_file/template.rb', line 48

def matcher
  @matcher
end

#template_stringString (readonly)

Returns the initial template string.

Returns:

  • (String)

    the initial template string



43
44
45
# File 'lib/date_named_file/template.rb', line 43

def template_string
  @template_string
end

Instance Method Details

#at(date_ish) ⇒ DateNamedFile::File

Get a DateNamedFile::File for the given date/datetime

Parameters:

  • date_ish (<anything date_ish>)

    (see #forgiving_dateify)

Returns:

  • (DateNamedFile::File)

    DateNamedFile::File for the given date in this template



85
86
87
# File 'lib/date_named_file/template.rb', line 85

def at(date_ish)
  DatedFile.new(self, date_ish)
end

#daily_after(date_ish) ⇒ Object

Like daily_since, but don’t include the start date

See Also:



127
128
129
# 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) ⇒ DateNamedFile::File

Get a list of computed files based on all the dates from the given start through today, _including both ends_.

Parameters:

  • start_date_ish (<anything date_ish>)

    (see #forgiving_dateify)

Returns:

  • (DateNamedFile::File)

    for tomorrow (+24 hours)



110
111
112
113
114
115
116
117
# 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) ⇒ Object

Like daily_since, but don’t include today

See Also:



121
122
123
# 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) ⇒ String

Compute the filename from plugging the given date_ish string/integer into the template

Parameters:

  • date_ish (<anything date_ish>)

    (see #forgiving_dateify)

Returns:

  • (String)

    the expanded filename



78
79
80
# 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) ⇒ Object



70
71
72
# File 'lib/date_named_file/template.rb', line 70

def in_dir(dir)
  DateNamedFile::Directory.new(self, dir)
end

#match?(filename) ⇒ Boolean Also known as: matches?

Test to see if a filename matches the template

Parameters:

  • filename (String)

    The string to test

Returns:

  • (Boolean)


63
64
65
# File 'lib/date_named_file/template.rb', line 63

def match?(filename)
  @matcher.match? filename
end

#nowDateNamedFile::File Also known as: today

Returns DateNamedFile::File for today/right now.

Returns:

  • (DateNamedFile::File)

    DateNamedFile::File for today/right now



90
91
92
# File 'lib/date_named_file/template.rb', line 90

def now
  at DateTime.now
end

#template_matcher(template) ⇒ Object



131
132
133
134
135
136
137
# 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

#tomorrowDateNamedFile::File

Returns DateNamedFile::File for tomorrow (+24 hours).

Returns:

  • (DateNamedFile::File)

    DateNamedFile::File for tomorrow (+24 hours)



97
98
99
# File 'lib/date_named_file/template.rb', line 97

def tomorrow
  at (DateTime.now + 1)
end

#yesterdayDateNamedFile::File

Returns DateNamedFile::File for yesterday (-24 hours).

Returns:

  • (DateNamedFile::File)

    DateNamedFile::File for yesterday (-24 hours)



102
103
104
# File 'lib/date_named_file/template.rb', line 102

def yesterday
  at (DateTime.now - 1)
end