module Ruuuby::Params

Utility functionalities for checking data types and throw exceptions.

Public Class Methods

check_array(param) click to toggle source

@param [Array|nil|*] param

@raise [ArgumentError]

@return [Boolean] true only, exception raised otherwise

# File lib/ruuuby/extensions.rb, line 43
def self.check_array(param)
  raise ArgumentError.new(self::bad_type(caller_locations.first.label.to_s, Array, param)) if Ruuuby::Params::not_array?(param)
  true
end
check_char(param) click to toggle source

@param [Array|nil|*] param

@raise [ArgumentError]

@return [Boolean] true only, exception raised otherwise

# File lib/ruuuby/extensions.rb, line 53
def self.check_char(param)
  if param == nil || !param.is_a?(String)
    error_message = "a param of type{#{String.to_s}}, instead got{#{param.class.to_s}}!"
  elsif param.chars.length != 1
    error_message = "a String of length 1, instead got{#{param.length.to_s}}"
  else
    return true
  end
  raise ArgumentError.new("#{caller_locations.first.label.to_s} requires #{error_message}")
end
check_string(param) click to toggle source

@param [String|nil|*] param

@raise [ArgumentError]

@return [Boolean] true only, exception raised otherwise

# File lib/ruuuby/extensions.rb, line 33
def self.check_string(param)
  raise ArgumentError.new(self::bad_type(caller_locations.first.label.to_s, String, param)) if Ruuuby::Params::not_string?(param)
  true
end
not_array?(ary) click to toggle source

@param [Array|*] str

@return [Boolean] true, if provided param is not nil and inherits from Array class.

# File lib/ruuuby/extensions.rb, line 24
def self.not_array?(ary)
  ary == nil || !ary.is_a?(Array)
end
not_string?(str) click to toggle source

@param [String|*] str

@return [Boolean] true, if provided param is not nil and inherits from String class.

# File lib/ruuuby/extensions.rb, line 17
def self.not_string?(str)
  str == nil || !str.is_a?(String)
end

Private Class Methods

bad_type(func_name, required_type, param) click to toggle source

@param [String] func | the name of the original function that triggered this exception @param [*] required_type | the required data type for the param @param [*] param | the variable with mis-mitached type

@return [String]

# File lib/ruuuby/extensions.rb, line 71
def self.bad_type(func_name, required_type, param)
  "#{func_name} requires param of type{#{required_type.to_s}}, instead got{#{param.class.to_s}}!"
end