module Pathutil::Helpers

Public Instance Methods

allowed() click to toggle source

# File lib/pathutil/helpers.rb, line 7
def allowed
  return @allowed ||= begin
    {
      :yaml => {
        :classes => [],
        :symbols => []
      }
    }
  end
end
load_yaml(data, safe: true, whitelist_classes: allowed[:yaml][:classes], \ whitelist_symbols: allowed[:yaml][:symbols], aliases: :yes) click to toggle source

– Wraps around YAML and SafeYAML to provide alternatives to Rubies. @note We default aliases to yes so we can detect if you explicit true. @return Hash –

# File lib/pathutil/helpers.rb, line 23
def load_yaml(data, safe: true, whitelist_classes: allowed[:yaml][:classes], \
    whitelist_symbols: allowed[:yaml][:symbols], aliases: :yes)

  require "yaml"
  unless safe
    return YAML.load(
      data
    )
  end

  if !YAML.respond_to?(:safe_load)
    setup_safe_yaml whitelist_classes, aliases
    SafeYAML.load(
      data
    )

  else
    YAML.safe_load(
      data,
      whitelist_classes,
      whitelist_symbols,
      aliases
    )
  end
end
make_tmpname(prefix = "", suffix = nil, root = nil) click to toggle source

– Make a temporary name suitable for temporary files and directories. @return String –

# File lib/pathutil/helpers.rb, line 53
def make_tmpname(prefix = "", suffix = nil, root = nil)
  prefix = tmpname_prefix(prefix)
  suffix = tmpname_suffix(suffix)

  root ||= Dir::Tmpname.tmpdir
  File.join(root, Dir::Tmpname.make_tmpname(
    prefix, suffix
  ))
end

Private Instance Methods

setup_safe_yaml(whitelist_classes, aliases) click to toggle source

– Wrap around, cleanup, deprecate and use SafeYAML. rubocop:enable Style/ParallelAssignment –

# File lib/pathutil/helpers.rb, line 97
def setup_safe_yaml(whitelist_classes, aliases)
  warn "WARN: SafeYAML does not support disabling  of aliases." if aliases && aliases != :yes
  warn "WARN: SafeYAML will be removed when Ruby 2.0 goes EOL."
  require "safe_yaml/load"

  SafeYAML.restore_defaults!
  whitelist_classes.map(&SafeYAML.method(
    :whitelist_class!
  ))
end
tmpname_prefix(prefix) click to toggle source

– Cleanup the temp name prefix, joining if necessary. rubocop:disable Style/ParallelAssignment –

# File lib/pathutil/helpers.rb, line 77
def tmpname_prefix(prefix)
  ext, prefix = prefix, "" if !prefix.is_a?(Array) && prefix.start_with?(".")
  ext = prefix.pop if prefix.is_a?(Array) && prefix[-1].start_with?(".")
  prefix = prefix.join("-") if prefix.is_a?(Array)

  unless prefix.empty?
    prefix = prefix.gsub(/\-\Z/, "") \
      + "-"
  end

  return [
    prefix, ext || ""
  ]
end
tmpname_suffix(suffix) click to toggle source

# File lib/pathutil/helpers.rb, line 66
def tmpname_suffix(suffix)
  suffix = suffix.join("-") if suffix.is_a?(Array)
  suffix = suffix.gsub(/\A\-/, "") unless !suffix || suffix.empty?
  suffix
end