Module: Ruuuby::Params

Defined in:
lib/ruuuby/extensions.rb

Overview

Utility functionalities for checking data types and throw exceptions.

Class Method Summary collapse

Class Method Details

.check_array(param) ⇒ Boolean

Returns true only, exception raised otherwise.

Parameters:

Returns:

  • (Boolean)

    true only, exception raised otherwise

Raises:

  • (ArgumentError)


43
44
45
46
# 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) ⇒ Boolean

Returns true only, exception raised otherwise.

Parameters:

Returns:

  • (Boolean)

    true only, exception raised otherwise

Raises:

  • (ArgumentError)


53
54
55
56
57
58
59
60
61
62
# 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) ⇒ Boolean

Returns true only, exception raised otherwise.

Parameters:

Returns:

  • (Boolean)

    true only, exception raised otherwise

Raises:

  • (ArgumentError)


33
34
35
36
# 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) ⇒ Boolean

Returns true, if provided param is not nil and inherits from Array class.

Parameters:

Returns:

  • (Boolean)

    true, if provided param is not nil and inherits from Array class.



24
25
26
# File 'lib/ruuuby/extensions.rb', line 24

def self.not_array?(ary)
  ary == nil || !ary.is_a?(Array)
end

.not_string?(str) ⇒ Boolean

Returns true, if provided param is not nil and inherits from String class.

Parameters:

Returns:

  • (Boolean)

    true, if provided param is not nil and inherits from String class.



17
18
19
# File 'lib/ruuuby/extensions.rb', line 17

def self.not_string?(str)
  str == nil || !str.is_a?(String)
end