Module: ArgChecks

Defined in:
lib/arg_checks.rb

Overview

Include this module in your classes that need sanity checking of method arguments. If any of these checks is not satisfied, it will raise an ArgumentError with a detailed message about what went wrong.

Instance Method Summary collapse

Instance Method Details

#arg_hash_keys_exact(exact_keys, *args) ⇒ Object

Require that all arguments return a set of keys containing exactly the specified elements.



54
55
56
57
58
59
60
61
62
# File 'lib/arg_checks.rb', line 54

def arg_hash_keys_exact(exact_keys, *args)
    args.each do |h|
        missing = exact_keys - h.keys
        extra = h.keys - exact_keys
        raise ArgumentError, "Hash keys don't match required set (#{exact_keys.join(', ')}). " +
            "Missing required keys (#{missing.join(', ')}); extra keys not allowed (#{extra.join(', ')}).\n" +
            "Got:\n#{h.inspect}" if (missing.length > 0) || (extra.length >0)
    end
end

#arg_hash_keys_limit(required_keys, optional_keys, *args) ⇒ Object

Require that all arguments return a set of keys containing a set of required keys, zero or more of the optional keys, and nothing else.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/arg_checks.rb', line 65

def arg_hash_keys_limit(required_keys, optional_keys, *args)
    args.each do |h|
      missing = required_keys - h.keys
      extra = h.keys - required_keys - optional_keys
  
      unless missing.empty? && extra.empty?
          msg = ""
          msg << "Hash is missing required keys (#{missing.join(', ')}).\n" unless missing.empty?
          msg << "Hash has extra keys which aren't allowed (#{extra.join(', ')}).\n" unless extra.empty?
          msg << " Got: #{h.inspect}"
          raise ArgumentError, msg
      end
    end
end

#arg_hash_keys_required(required_keys, *args) ⇒ Object

Require that all arguments return a set of keys containing at least the specified elements.



46
47
48
49
50
51
# File 'lib/arg_checks.rb', line 46

def arg_hash_keys_required(required_keys, *args)
    args.each do |h|
        missing = required_keys - h.keys
        raise ArgumentError, "Hash is missing required keys (#{missing.join(', ')}). Got:\n#{h.inspect}" if missing.length > 0
    end
end

#arg_max(max, *args) ⇒ Object

Require that all arguments be greater than or equal to max.



36
37
38
# File 'lib/arg_checks.rb', line 36

def arg_max(max, *args)
    args.each{|obj| raise ArgumentError, "Argument exceeds maxmimum value (max is #{max.to_s})" unless max >= obj}
end

#arg_min(min, *args) ⇒ Object

Require that all arguments be less than or equal to min.



41
42
43
# File 'lib/arg_checks.rb', line 41

def arg_min(min, *args)
    args.each{|obj| raise ArgumentError, "Argument is lower than minimum value (min is #{min.to_s})" unless min <= obj }
end

#arg_required(*args) ⇒ Object

Require that all arguments be non-nil.



19
20
21
# File 'lib/arg_checks.rb', line 19

def arg_required(*args)
    args.each{|obj| raise ArgumentError, "Argument cannot be nil" if obj.nil? }
end

#arg_responds_to(method, *args) ⇒ Object

Require that all arguments respond_to? the specified method.



13
14
15
16
# File 'lib/arg_checks.rb', line 13

def arg_responds_to(method, *args)
    arg_type Symbol, method
    args.each{|obj| raise ArgumentError, "Object #{obj} (class #{obj.class}) doesn't respond to #{method}." unless obj.respond_to? method}
end

#arg_type(klass, *args) ⇒ Object

Require that all arguments be of the specified class, or one of its subclasses (uses is_a?). Consider using arg_responds_to? or respond_to? instead, to make your API more flexible.

Raises:

  • (ArgumentError)


7
8
9
10
# File 'lib/arg_checks.rb', line 7

def arg_type(klass, *args)
    raise ArgumentError, "First argument must be a class. Got #{klass.to_s}" unless klass.is_a? Class
    args.each{|obj| raise ArgumentError, "Wrong type for argument '#{obj}' (should be #{klass})" unless obj.is_a? klass }
end

#named_arg_required(name, obj) ⇒ Object

Require that the second argument be non-nil. Upon failure the first argument is used in the error message.

Raises:

  • (ArgumentError)


24
25
26
27
# File 'lib/arg_checks.rb', line 24

def named_arg_required(name, obj)
    named_arg_required('name', name) unless name.eql?('name')
    raise ArgumentError, "Argument \"#{name}\" cannot be nil" if obj.nil?
end

#named_args_required(obj_hash) ⇒ Object

Require that all values in the hash be non-nil. Returns the list of all keys whose values were nil.

Raises:

  • (ArgumentError)


30
31
32
33
# File 'lib/arg_checks.rb', line 30

def named_args_required(obj_hash)
    keys_with_nil_values = obj_hash.select{|k,v| v.nil?}.collect{|e| e[0]}
    raise ArgumentError, "Nil argument not allowed in #{keys_with_nil_values.join(', ')}" unless keys_with_nil_values.empty?
end