Module: Wgit::Assertable
Overview
Module containing assertion methods including type checking and duck typing.
Constant Summary collapse
- DEFAULT_TYPE_FAIL_MSG =
Default type fail message.
'Expected: %s, Actual: %s'- NON_ENUMERABLE_MSG =
Wrong method message.
'Expected an Enumerable responding to #each, not: %s'- DEFAULT_DUCK_FAIL_MSG =
Default duck fail message.
"%s doesn't respond_to? %s"- DEFAULT_REQUIRED_KEYS_MSG =
Default required keys message.
"Some or all of the required keys are not \ present: %s"
Instance Method Summary collapse
-
#assert_arr_types(arr, type_or_types, msg = nil) ⇒ Object
(also: #assert_arr_type)
Each object within arr must match one of the types listed in type_or_types; or an exception is raised using msg, if provided.
-
#assert_required_keys(hash, keys, msg = nil) ⇒ Hash
The hash must include? the keys or a KeyError is raised.
-
#assert_respond_to(obj_or_objs, methods, msg = nil) ⇒ Object
The obj_or_objs must respond_to? all of the given methods or an Exception is raised using msg, if provided.
-
#assert_types(obj, type_or_types, msg = nil) ⇒ Object
(also: #assert_type)
Tests if the obj is_a? given type; raises an Exception if not.
Instance Method Details
#assert_arr_types(arr, type_or_types, msg = nil) ⇒ Object Also known as: assert_arr_type
Each object within arr must match one of the types listed in type_or_types; or an exception is raised using msg, if provided.
44 45 46 47 48 |
# File 'lib/wgit/assertable.rb', line 44 def assert_arr_types(arr, type_or_types, msg = nil) raise format(NON_ENUMERABLE_MSG, arr.class) unless arr.respond_to?(:each) arr.each { |obj| assert_types(obj, type_or_types, msg) } end |
#assert_required_keys(hash, keys, msg = nil) ⇒ Hash
The hash must include? the keys or a KeyError is raised.
77 78 79 80 81 82 83 |
# File 'lib/wgit/assertable.rb', line 77 def assert_required_keys(hash, keys, msg = nil) msg ||= format(DEFAULT_REQUIRED_KEYS_MSG, keys.join(', ')) all_present = keys.all? { |key| hash.keys.include? key } raise KeyError, msg unless all_present hash end |
#assert_respond_to(obj_or_objs, methods, msg = nil) ⇒ Object
The obj_or_objs must respond_to? all of the given methods or an Exception is raised using msg, if provided.
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/wgit/assertable.rb', line 58 def assert_respond_to(obj_or_objs, methods, msg = nil) methods = *methods if obj_or_objs.respond_to?(:each) obj_or_objs.each { |obj| _assert_respond_to(obj, methods, msg) } else _assert_respond_to(obj_or_objs, methods, msg) end obj_or_objs end |
#assert_types(obj, type_or_types, msg = nil) ⇒ Object Also known as: assert_type
Tests if the obj is_a? given type; raises an Exception if not.
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/wgit/assertable.rb', line 24 def assert_types(obj, type_or_types, msg = nil) msg ||= format(DEFAULT_TYPE_FAIL_MSG, type_or_types, obj.class) match = if type_or_types.respond_to?(:any?) type_or_types.any? { |type| obj.is_a?(type) } else obj.is_a?(type_or_types) end raise msg unless match obj end |