class Sia::Configurable::Validator

Validate the options @private

Constants

SAFE_PATH_REGEX

Public Class Methods

new(opt) click to toggle source
# File lib/sia/configurable.rb, line 123
def initialize(opt)
  @opt = opt
  @converted = []
end

Public Instance Methods

convert(*keys) { |opt| ... } click to toggle source
# File lib/sia/configurable.rb, line 144
def convert(*keys, &block)
  @converted += keys
  (keys & @opt.keys).each do |k|
    @opt[k] = yield @opt[k]
  rescue NoMethodError => nme
    raise Sia::Error::ConfigurationError,
      "#{k.inspect} was #{nme.args} and could not be converted using " +
      "`#{nme.name.inspect}`"
  end
  self
end
done() click to toggle source

Make sure Sia converts each option exactly one time. Any time a new option is added, it needs to be converted.

# File lib/sia/configurable.rb, line 165
def done
  return if DEFAULTS.keys.sort == @converted.sort

  bads = DEFAULTS.transform_values { 0 }.merge(
    @converted.group_by(&:itself).transform_values(&:count)
  ).select { |k, v| v != 1 }

  raise "Options were not converted exactly once! #{bads}"
end
portable_file(string) click to toggle source
# File lib/sia/configurable.rb, line 156
def portable_file(string)
  return if string =~ SAFE_PATH_REGEX
  raise Sia::Error::ConfigurationError,
    "Filenames must match the regex #{SAFE_PATH_REGEX.inspect}, " +
    "but was #{string.inspect}"
end
safe_filename(*keys) click to toggle source
# File lib/sia/configurable.rb, line 140
def safe_filename(*keys)
  (keys & @opt.keys).each { |k| portable_file(@opt[k].to_s) }
end
safe_path(*keys) click to toggle source
# File lib/sia/configurable.rb, line 128
def safe_path(*keys)
  (keys & @opt.keys).each do |k|
    if @opt[k].to_s.empty?
      raise Sia::Error::ConfigurationError, "#{k.inspect} must not be empty"
    end

    slash = File::SEPARATOR
    path = @opt[k].to_s.chomp(slash).reverse.chomp(slash).reverse
    path.split(slash).all? { |s| portable_file(s) }
  end
end