Module: Wgit::Assertable
Overview
Module containing assert methods including type checking which can be used for asserting the integrity of method definitions etc.
Constant Summary collapse
- DEFAULT_TYPE_FAIL_MSG =
"Expected: %s, Actual: %s"- WRONG_METHOD_MSG =
"arr must be Enumerable, use a different method"- DEFAULT_DUCK_FAIL_MSG =
"%s doesn't respond_to? %s"
Instance Method Summary collapse
-
#assert_arr_types(arr, type_or_types, msg = nil) ⇒ Object
(also: #assert_arr_type, #arr_type, #arr_types)
Each object within arr must match one of the types listed in type_or_types or an exception is thrown using msg if provided.
-
#assert_respond_to(obj_or_objs, methods, msg = nil) ⇒ Object
(also: #respond_to)
The obj_or_objs must respond_to? all of the given methods or an Exception is raised using msg or a default message.
-
#assert_types(obj, type_or_types, msg = nil) ⇒ Object
(also: #assert_type, #type, #types)
obj.instance_of? must return true for one of the types listed in type_or_types or an exception is thrown using msg if provided.
Instance Method Details
#assert_arr_types(arr, type_or_types, msg = nil) ⇒ Object Also known as: assert_arr_type, arr_type, arr_types
Each object within arr must match one of the types listed in type_or_types or an exception is thrown using msg if provided. type_or_types can be a single Class or an Enumerable of Class objects, Strings and Symbols will not work.
31 32 33 34 35 36 |
# File 'lib/wgit/assertable.rb', line 31 def assert_arr_types(arr, type_or_types, msg = nil) raise WRONG_METHOD_MSG unless arr.respond_to?(:each) arr.each do |obj| assert_types(obj, type_or_types, msg) end end |
#assert_respond_to(obj_or_objs, methods, msg = nil) ⇒ Object Also known as: respond_to
The obj_or_objs must respond_to? all of the given methods or an Exception is raised using msg or a default message. Returns obj_or_objs on sucessful assertion.
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/wgit/assertable.rb', line 41 def assert_respond_to(obj_or_objs, methods, msg = nil) if obj_or_objs.respond_to?(:each) obj_or_objs.each do |obj| _assert_respond_to(obj, methods, msg) end 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, type, types
obj.instance_of? must return true for one of the types listed in type_or_types or an exception is thrown using msg if provided. type_or_types can be a single Class or an Enumerable of Class objects, Strings and Symbols will not work.
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/wgit/assertable.rb', line 16 def assert_types(obj, type_or_types, msg = nil) msg ||= DEFAULT_TYPE_FAIL_MSG % [type_or_types, obj.class] if type_or_types.respond_to?(:any?) match = type_or_types.any? { |type| obj.instance_of?(type) } else match = obj.instance_of?(type_or_types) end raise msg unless match obj end |