class Expire::BackupFromPathService

Take a path and return an instance of Expire::Backup

Attributes

by[R]
pathname[R]

Public Class Methods

call(path:, by: :path) click to toggle source
# File lib/expire/backup_from_path_service.rb, line 6
def self.call(path:, by: :path)
  new(path: path, by: by).call
end
new(path:, by: :path) click to toggle source
# File lib/expire/backup_from_path_service.rb, line 10
def initialize(path:, by: :path)
  @by       = by
  @pathname = Pathname.new(path)

  raise ArgumentError, "by: must be :ctime, :mtime or :path, not #{by}" unless %i[ctime mtime path].include?(by)
end

Public Instance Methods

call() click to toggle source
# File lib/expire/backup_from_path_service.rb, line 19
def call
  Backup.new(datetime: datetime, pathname: pathname)
end

Private Instance Methods

datetime() click to toggle source
# File lib/expire/backup_from_path_service.rb, line 25
def datetime
  digits = extract_digits

  year   = digits[0..3].to_i
  month  = digits[4..5].to_i
  day    = digits[6..7].to_i
  hour   = digits[8..9].to_i
  minute = digits[10..11].to_i

  datetime_for(year, month, day, hour, minute)
end
datetime_for(year, month, day, hour, minute) click to toggle source
# File lib/expire/backup_from_path_service.rb, line 37
def datetime_for(year, month, day, hour, minute)
  DateTime.new(year, month, day, hour, minute)
rescue Date::Error
  raise InvalidPathError, "can't construct date and time from #{pathname}" 
end
extract_digits() click to toggle source
# File lib/expire/backup_from_path_service.rb, line 43
def extract_digits
  basename = pathname.basename.to_s

  digits = basename.gsub(/[^0-9]/, '')

  digits_length = digits.length

  return digits if digits_length == 12
  return digits if digits_length == 14

  raise InvalidPathError, "can't extract date and time from #{basename}"
end