class RspecApiDocs::After::TypeChecker

Constants

TypeError
UnknownType

Attributes

type[R]
value[R]

Public Class Methods

call(*args) click to toggle source
# File lib/rspec_api_docs/after/type_checker.rb, line 9
def self.call(*args)
  new(*args).check
end
new(type:, value:) click to toggle source
# File lib/rspec_api_docs/after/type_checker.rb, line 13
def initialize(type:, value:)
  @type = type
  @value = value
end

Public Instance Methods

check() click to toggle source
# File lib/rspec_api_docs/after/type_checker.rb, line 18
def check
  case type
  when /integer/i
    is_integer?(value) or raise_type_error
  when /float/i
    is_float?(value) or raise_type_error
  when /boolean/i
    is_bool?(value) or raise_type_error
  when /string/i
    # NO OP
  else
    raise UnknownType, "unknown type #{type.inspect}"
  end
end

Private Instance Methods

is_bool?(str) click to toggle source
# File lib/rspec_api_docs/after/type_checker.rb, line 47
def is_bool?(str)
  %w[true false].include?(str)
end
is_float?(str) click to toggle source
# File lib/rspec_api_docs/after/type_checker.rb, line 41
def is_float?(str)
  Float(str) && true
rescue ArgumentError
  false
end
is_integer?(str) click to toggle source
# File lib/rspec_api_docs/after/type_checker.rb, line 35
def is_integer?(str)
  Integer(str) && true
rescue ArgumentError
  false
end
raise_type_error() click to toggle source
# File lib/rspec_api_docs/after/type_checker.rb, line 51
def raise_type_error
  raise TypeError, "wrong type #{value.inspect}, expected #{type.inspect}"
end