Module: Preconditions::PreconditionMixinMethods
- Included in:
- Preconditions
- Defined in:
- lib/preconditions.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#check_argument(exp, msg = nil, *fmt) ⇒ Object
Check that given expression is satisfied.
-
#check_block(msg = nil, *fmt, &block) ⇒ Object
Check that given block expression is satisfied.
-
#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.
-
#check_responds_to(obj, meth_sym, msg = nil, *fmt) ⇒ Object
Check that given object responds to given method.
-
#check_type(obj, expected_type, msg = nil, *fmt) ⇒ Object
Check that given object is of given type.
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.
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.
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
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
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.
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 |