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.



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.



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.



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

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