module Sgfa::Error

Errors generated by the Sgfa system

Public Class Methods

limits(str, min, max, inv, desc) click to toggle source

Most limit checks

@param str [String] String to check @param min [Integer] Minimum length @param max [Integer] max Maximum length @param inv [Regexp, String] Invalid characters @param desc [String] desc Description of string @raise [Error::Limits] if limits are violated

# File lib/sgfa/error.rb, line 32
def self.limits(str, min, max, inv, desc)

  # string
  if !str || !str.is_a?(String)
    raise Error::Limits, desc + ' is not a string.'
  end

  # size
  size = str.size
  if size > max
    raise Error::Limits, '%s length %d is too large, limit %d' %
      [desc, size, max]
  end
  if size < min
    raise Error::Limits, '%s length %d is too small, limit %d' %
      [desc, size, min]
  end

  # invalid characters
  idx = str.index(inv)
  if idx
    raise Error::Limits,
      '%s contains invalid character \'%s\'' % [desc, str[idx]]
  end
end