module PowerStencil::Utils::FileHelper

Public Instance Methods

find_recursively_in_path(dir_or_file, from_dir) click to toggle source
# File lib/power_stencil/utils/file_helper.rb, line 6
def find_recursively_in_path(dir_or_file, from_dir)
  raise PowerStencil::Error, "Invalid directory '#{from_dir}'" unless Dir.exist?(from_dir) and File.readable?(from_dir)

  candidate = File.join from_dir, dir_or_file

  if Dir.exists? candidate or File.exist? candidate
    if File.readable? candidate and File.writable? candidate
      candidate
    else
      PowerStencil.logger.warn "Humm, found something: '#{candidate}', but with wrong access rights."
      nil
    end
  else
    next_dir = File.expand_path '..', from_dir
    if next_dir == from_dir
      PowerStencil.logger.info "Didn't find any trace of '#{dir_or_file}'... :("
      nil
    else
      find_recursively_in_path dir_or_file, next_dir
    end
  end

end
timestamp(timedate) click to toggle source
# File lib/power_stencil/utils/file_helper.rb, line 47
def timestamp(timedate)
  timedate.strftime '%Y%m%d-%H%M-%S%L'
end
timestamped_uniq_dir(seed, start_time) click to toggle source
# File lib/power_stencil/utils/file_helper.rb, line 41
def timestamped_uniq_dir(seed, start_time)
  uniq_dir = timestamp start_time
  uniq_dir += "-#{seed}" unless seed.nil?
  uniq_dir
end
yaml_file_to_hash(file_name) click to toggle source
# File lib/power_stencil/utils/file_helper.rb, line 30
def yaml_file_to_hash(file_name)
  raw_content = File.read file_name
  res = YAML.load raw_content
  if res
    Hash[res.map { |k, v| [k, v] }]
  else
    raise PowerStencil::Error, "Invalid file content for '#{file_name}'" unless raw_content.empty?
    []
  end
end