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

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.

Parameters:

  • arr (Enumerable#each)

    Enumerable of objects to type check.

  • type_or_types (Type, Array<Type>)

    The allowed type(s).

  • msg (String) (defaults to: nil)

    The raised StandardError message, if provided.

Returns:

  • (Object)

    The given arr on successful assertion.

Raises:

  • (StandardError)

    If the assertion fails.



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.

Parameters:

  • arr (Enumerable#each)

    Enumerable of objects to type check.

  • type_or_types (Type, Array<Type>)

    The allowed type(s).

  • msg (String) (defaults to: nil)

    The raised StandardError message, if provided.

Returns:

  • (Object)

    The given arr on successful assertion.

Raises:

  • (StandardError)

    If the assertion fails.



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.

Parameters:

  • hash (Hash)

    The hash which should include the required keys.

  • keys (Array<String, Symbol>)

    The keys whose presence to assert.

  • msg (String) (defaults to: nil)

    The raised KeyError message, if provided.

Returns:

  • (Hash)

    The given hash on successful assertion.

Raises:

  • (KeyError)

    If the assertion fails.



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.

Parameters:

  • obj_or_objs (Object, Enumerable#each)

    The object(s) to duck check.

  • methods (Array<Symbol>)

    The methods to :respond_to?.

  • msg (String) (defaults to: nil)

    The raised StandardError message, if provided.

Returns:

  • (Object)

    The given obj_or_objs on successful assertion.

Raises:

  • (StandardError)

    If the assertion fails.



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.

Parameters:

  • obj (Object)

    The Object to test.

  • type_or_types (Type, Array<Type>)

    The type/types that obj must belong to or an exception is thrown.

  • msg (String) (defaults to: nil)

    The raised StandardError message, if provided.

Returns:

  • (Object)

    The given obj on successful assertion.

Raises:

  • (StandardError)

    If the assertion fails.



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