module Surrealist::TypeHelper
Service class for type checking
Constants
- DRY_TYPE_CLASS
Dry-types class matcher
Public Class Methods
coerce(value, type)
click to toggle source
Coerces value is it should be coerced
@param [any] value value that will be coerced @param [Class] type class representing data type
@return [any] coerced value
# File lib/surrealist/type_helper.rb, line 35 def coerce(value, type) return value unless dry_type?(type) return value if type.try(value).input == value type[value] end
valid_type?(value, type)
click to toggle source
Checks if value returned from a method is an instance of type class specified in schema or NilClass.
@param [any] value value returned from a method. @param [Class] type class representing data type.
@return [boolean]
# File lib/surrealist/type_helper.rb, line 17 def valid_type?(value, type) return true if type == Any if type == Bool Surrealist::Carrier::BOOLEANS.include?(value) elsif defined?(Dry::Types) && dry_type?(type) type.try(value).success? else value.nil? || value.is_a?(type) end end
Private Class Methods
dry_type?(type)
click to toggle source
Checks if type is an instance of dry-type
@param [Object] type type to be checked
@return [Boolean] is type an instance of dry-type
# File lib/surrealist/type_helper.rb, line 49 def dry_type?(type) if type.respond_to?(:primitive) || type.class.name.nil? true else type.class.name =~ DRY_TYPE_CLASS end end