Module: Wgit::Assertable
- Included in:
- Crawler, Database::DatabaseAdapter, Document, HTMLToText, Indexer, RobotsParser, Url
- Defined in:
- lib/wgit/assertable.rb
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"
- MIXED_ENUMERABLE_MSG =
Enumerable with more than one type across it's elements.
"Expected an Enumerable with elements of a single \ common type"
- 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_common_arr_types(arr, type_or_types, msg = nil) ⇒ Object
(also: #assert_common_arr_type)
All objects 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.
51 52 53 54 55 |
# File 'lib/wgit/assertable.rb', line 51 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_common_arr_types(arr, type_or_types, msg = nil) ⇒ Object Also known as: assert_common_arr_type
All objects within arr must match one of the types listed in type_or_types; or an exception is raised using msg, if provided. Ancestors of the same type are allowed and considered common.
66 67 68 69 70 71 72 73 74 |
# File 'lib/wgit/assertable.rb', line 66 def assert_common_arr_types(arr, type_or_types, msg = nil) raise format(NON_ENUMERABLE_MSG, arr.class) unless arr.respond_to?(:each) type = arr.first.class type_match = arr.all? { |obj| type.ancestors.include?(obj.class) } raise MIXED_ENUMERABLE_MSG unless type_match assert_arr_types(arr, type_or_types, msg) end |
#assert_required_keys(hash, keys, msg = nil) ⇒ Hash
The hash must include? the keys or a KeyError is raised.
103 104 105 106 107 108 109 |
# File 'lib/wgit/assertable.rb', line 103 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.
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/wgit/assertable.rb', line 84 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.
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/wgit/assertable.rb', line 31 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 |