module Patriot::Util::Script

a module to find target files

Public Instance Methods

get_batch_files(path, date, opt = {}) click to toggle source

get target batch files from a given path @param path [String] path to target directory @param date [String] target date in '%Y-%m-%d' @param opt [Hash] @option opt :all [Boolean] force target all files @return [Array<String>] a list of target files

# File lib/patriot/util/script.rb, line 13
def get_batch_files(path, date, opt = {})
  return [path] if File.file?(path) && File.extname(path) == ".pbc" 
  files = []
  opt   = target_option(date, opt)
  files = Dir.glob("#{path}/**/*.pbc").find_all do |file|
    target_file?(file, opt)
  end
  return files
end

Private Instance Methods

target_file?(file, options) click to toggle source
# File lib/patriot/util/script.rb, line 39
def target_file?(file, options)
  case
  when options[:all]               then true
  when file =~ /\/daily\//         then options[:day]
  when file =~ /\/monthly\//       then options[:month]
  when file =~ /\/weekly\/([0-6])/ then options[:week].to_s == $~[1]
  else true
  end
end
target_option(date, opt = {}) click to toggle source
# File lib/patriot/util/script.rb, line 23
def target_option(date, opt = {})
  opt = {:all => false}.merge(opt)
  unless opt[:all]
    d = date.split('-')
    opt[:day] = true unless opt.has_key?(:day)
    unless opt.has_key?(:month)
      opt[:month] = date_add(date,1) =~ /[\d]{4}-[\d]{2}-01/ ? true : false 
    end
    unless opt.has_key?(:week)
      opt[:week]  = Date.new(d[0].to_i, d[1].to_i, d[2].to_i).wday
    end
  end
  return opt
end