Module: SparkleFormation::Utils::TypeCheckers

Included in:
SparkleFormation, SparkleStruct
Defined in:
lib/sparkle_formation/utils.rb

Overview

Type check helpers

Instance Method Summary collapse

Instance Method Details

#__t_check(val, types) ⇒ NilClass

Validate given value is type defined within valid types

Parameters:

  • val (Object)

    value

  • types (Class, Array<Class>)

    valid types

Returns:

  • (NilClass)

Raises:

  • (TypeError)


15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/sparkle_formation/utils.rb', line 15

def __t_check(val, types)
  types = [types] unless types.is_a?(Array)
  if types.none? { |t| val.is_a?(t) }
    ignore_paths = Gem::Specification.find_by_name("sparkle_formation").full_require_paths
    file_name, line_no = ::Kernel.caller.detect do |l|
      ignore_paths.none? { |i_path| l.include?(i_path) }
    end.split(":")[0, 2]
    file_name = file_name.to_s.sub(::Dir.pwd, ".")
    ::Kernel.raise TypeError.new "Received invalid value type `#{val.class}`! " \
                                 "(Allowed types: `#{types.join("`, `")}`) -> #{file_name} @ line #{line_no}"
  end
end

#__t_hashish(val) ⇒ NilClass

Validate given value is a Hash type

Returns:

  • (NilClass)

Raises:

  • (TypeError)


40
41
42
# File 'lib/sparkle_formation/utils.rb', line 40

def __t_hashish(val)
  __t_check(val, Hash)
end

#__t_stringish(val) ⇒ NilClass

Validate given value is String or Symbol type

Returns:

  • (NilClass)

Raises:

  • (TypeError)


32
33
34
# File 'lib/sparkle_formation/utils.rb', line 32

def __t_stringish(val)
  __t_check(val, [String, Symbol])
end