Class: DateNamedFile::Directory

Inherits:
Template
  • Object
show all
Includes:
Enumerable
Defined in:
lib/date_named_file/directory.rb

Overview

The instantiation of a template over a specific directory. This allows us to find out which files that match the template actually exist, extract dates from them, etc.

Constant Summary

Constants inherited from Template

Template::SUBSTITUTION_REGEXP

Constants included from DateishHelpers

DateNamedFile::DateishHelpers::ALL_DIGITS, DateNamedFile::DateishHelpers::VALID_DELIMITERS

Instance Attribute Summary collapse

Attributes inherited from Template

#matcher, #template_string

Instance Method Summary collapse

Methods inherited from Template

#daily_after, #daily_since, #daily_through_yesterday, #filename_for, #in_dir, #match?, #now, #template_matcher, #tomorrow, #yesterday

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, path) ⇒ Directory

Returns a new instance of Directory.

Parameters:

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
# File 'lib/date_named_file/directory.rb', line 20

def initialize(template, path)
  @dir_path = Pathname.new(path).realdirpath
  raise ArgumentError.new("Directory '#{path}' does not exist") unless @dir_path.exist?
  raise ArgumentError.new("'#{path}' isn't a directory") unless @dir_path.directory?
  super((@dir_path + template.template_string).to_s)
  @matching_files = @dir_path.children.sort.select{|x| self.match? x.to_s}.map{|x| DatedFile.from_filename(self,x.to_s)}
end

Instance Attribute Details

#dir_pathPathname

Returns:

  • (Pathname)


16
17
18
# File 'lib/date_named_file/directory.rb', line 16

def dir_path
  @dir_path
end

#matching_filesPathname

Returns:

  • (Pathname)


16
17
18
# File 'lib/date_named_file/directory.rb', line 16

def matching_files
  @matching_files
end

Instance Method Details

#after(date_ish) ⇒ Object



45
46
47
# File 'lib/date_named_file/directory.rb', line 45

def after(date_ish)
  self.select {|f| f > date_ish}
end

#at(date_ish) ⇒ Object Also known as: on



30
31
32
33
34
35
36
# File 'lib/date_named_file/directory.rb', line 30

def at(date_ish)
  if has_file_for_date?(date_ish)
    no_existence_check_at(date_ish)
  else
    MissingFile.new(self, date_ish)
  end
end

#eachObject

Yield matching files for each



66
67
68
69
# File 'lib/date_named_file/directory.rb', line 66

def each
  return enum_for(:each) unless block_given?
  @matching_files.each {|f| yield f}
end

#has_file_for_date?(date_ish) ⇒ Boolean Also known as: has?

Does this directory have a file for the given date?

Parameters:

  • date_ish (<anything date_ish>)

    (see #forgiving_dateify)

Returns:

  • (Boolean)


57
58
59
60
# File 'lib/date_named_file/directory.rb', line 57

def has_file_for_date?(date_ish)
  target = self.no_existence_check_at(date_ish)
  (@dir_path + target).exist?
end

#lastObject



49
50
51
# File 'lib/date_named_file/directory.rb', line 49

def last
  @matching_files.last
end

#no_existence_check_atObject



28
# File 'lib/date_named_file/directory.rb', line 28

alias_method :no_existence_check_at, :at

#since(date_ish) ⇒ Object



41
42
43
# File 'lib/date_named_file/directory.rb', line 41

def since(date_ish)
  self.select {|f| f >= date_ish}
end