Class: Brine::TypeChecking::TypeChecks

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

Overview

Define a registry of checks for types which can return a Matcher for registered 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:

  • Provide (Hash<String, Matcher>)

    a 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.



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

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)

    Specify the type whose Matcher should be returned.

Returns:

  • (RSpec::Matcher)

    Return the Matcher registered for ‘type`.



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

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 matcher Provide a matcher to verify that input is an instance of type.

Parameters:

  • type (Class)

    Specify the type for which the Matcher will be registered.



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

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