Module: Preconditions::PreconditionMixinMethods

Included in:
Preconditions
Defined in:
lib/preconditions.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(receiver) ⇒ Object



75
76
77
# File 'lib/preconditions.rb', line 75

def self.included(receiver)
  receiver.extend PreconditionMixinMethods
end

Instance Method Details

#check_argument(exp, msg = nil, *fmt) ⇒ Object

Check that given expression is satisfied. Should be used in the context of an checking an argument, though this is not strictly required.

Parameters:

  • exp

    a boolean expression; if true, the argument is good, if false it is not

  • msg (defaults to: nil)

    optional format string message to print

  • fmt

    arguments to format into format string

Raises:

  • (ArgumentError)

    if exp is false



26
27
28
29
30
31
# File 'lib/preconditions.rb', line 26

def check_argument(exp, msg = nil, *fmt)
  if !exp
    raise_exception(ArgumentError, msg, *fmt)
  end
  exp
end

#check_block(msg = nil, *fmt, &block) ⇒ Object

Check that given block expression is satisfied. Should be used in the context of an checking an argument, though this is not strictly required.

Parameters:

  • msg (defaults to: nil)

    optional format string message to print

  • fmt

    arguments to format into format string

Raises:

  • (ArgumentError)

    if block evaluates to false



39
40
41
42
43
44
45
# File 'lib/preconditions.rb', line 39

def check_block(msg = nil, *fmt, &block)
  res = yield
  if !res
    raise_exception(ArgumentError, msg, *fmt)
  end
  res
end

#check_not_nil(arg, msg = nil, *fmt) ⇒ Object

Check that arg is not nil, raising an ArgumentError with an optional message and format if it is nil

Parameters:

  • arg

    the argument to check

  • msg (defaults to: nil)

    optional format string message to print

  • fmt

    arguments to format into format string

Raises:

  • (ArgumentError)

    if the arg is nil



13
14
15
16
17
18
# File 'lib/preconditions.rb', line 13

def check_not_nil(arg, msg = nil, *fmt)
  if arg.nil?
    raise_exception(ArgumentError, msg, *fmt)
  end
  arg
end

#check_responds_to(obj, meth_sym, msg = nil, *fmt) ⇒ Object

Check that given object responds to given method

Parameters:

  • obj

    object to check

  • meth_sym

    symbolic name of method

  • msg (defaults to: nil)

    optional format string message to print

  • fmt

    arguments to format into format string

Returns:

  • obj

Raises:

  • (NameError)

    if meth_sym is not a method on obj



68
69
70
71
72
73
# File 'lib/preconditions.rb', line 68

def check_responds_to(obj, meth_sym, msg=nil, *fmt)
  if !obj.respond_to?(meth_sym)
    raise_exception(NameError, msg, *fmt)
  end
  obj
end

#check_type(obj, expected_type, msg = nil, *fmt) ⇒ Object

Check that given object is of given type.

Parameters:

  • obj

    object to check

  • expected_type

    type of which object is expected to be an instance

  • msg (defaults to: nil)

    optional format string message to print

  • fmt

    arguments to format into format string

Returns:

  • obj

Raises:

  • (TypeError)

    if obj is not an instance of expected_type



54
55
56
57
58
59
# File 'lib/preconditions.rb', line 54

def check_type(obj, expected_type, msg=nil, *fmt)
  if !obj.nil? && !obj.kind_of?(expected_type)
    raise_exception(TypeError, msg, *fmt)
  end
  obj
end