Class: Brine::TypeChecking::TypeChecks

Inherits:
Object
  • Object
show all
Includes:
RSpec::Matchers
Defined in:
lib/brine/type_checking.rb

Overview

A registry of checks for types which can return a Matcher for provided types.

Instance Method Summary collapse

Constructor Details

#initialize(map = {Object: be_a_kind_of(Hash), String: be_a_kind_of(String), Number: be_a_kind_of(Numeric), Integer: be_a_kind_of(Integer), Array: be_a_kind_of(Array), DateTime: be_a_kind_of(Time), Boolean: satisfy{|it| it == true || it == false } }) ⇒ TypeChecks

Initialize an instance with default checks or those provided.

Parameters:

  • A (Hash<String, Matcher>)

    hash of Matchers by type, where the Matcher value will be used as the Matcher for the specified type. It is expected that no map will be provided and the default mapping will therefore be used.



31
32
33
34
35
36
37
38
39
# File 'lib/brine/type_checking.rb', line 31

def initialize(map={Object: be_a_kind_of(Hash),
                    String: be_a_kind_of(String),
                    Number: be_a_kind_of(Numeric),
                    Integer: be_a_kind_of(Integer),
                    Array: be_a_kind_of(Array),
                    DateTime: be_a_kind_of(Time),
                    Boolean: satisfy{|it| it == true || it == false } })
  @map = map
end

Instance Method Details

#for_type(type) ⇒ RSpec::Matcher

Return the Matcher for the specified type or die if not present.

Parameters:

  • type (Class)

    The type whose Matcher should be returned.

Returns:

  • (RSpec::Matcher)

    The Matcher configured for ‘type`.



48
49
50
# File 'lib/brine/type_checking.rb', line 48

def for_type(type)
  @map[type.to_sym] || raise("Unsupported type #{type}")
end

#register_matcher(type, matcher) ⇒ Object

Register the provided matcher for the specified type.

@param type The type for which the Matcher will be registered. @param matcher A matcher to verify that input is an instance of type.



58
59
60
# File 'lib/brine/type_checking.rb', line 58

def register_matcher(type, matcher)
  @map[type.to_sym] = matcher
end