module Grape::Validations::Types
def build_coercer(type, method: nil, strict: false)
-
(Object)
- object to be used
Parameters:
-
method
(Class, #call
) -- the coercion method to use -
type
(Class
) -- the type to which input strings
def build_coercer(type, method: nil, strict: false) cache_instance(type, method, strict) do create_coercer_instance(type, method, strict) end end
def cache_instance(type, method, strict, &_block)
def cache_instance(type, method, strict, &_block) key = cache_key(type, method, strict) return @__cache[key] if @__cache.key?(key) instance = yield @__cache_write_lock.synchronize do @__cache[key] = instance end instance end
def cache_key(type, method, strict)
def cache_key(type, method, strict) [type, method, strict].each_with_object(+'_') do |val, memo| next if val.nil? memo << '_' << val.to_s end end
def collection_of_custom?(type)
-
(Boolean)
- true if +type+ is a collection of a type that implements
Parameters:
-
type
(Array
) -- type to check, Class
def collection_of_custom?(type) (type.is_a?(Array) || type.is_a?(Set)) && type.length == 1 && (custom?(type.first) || special?(type.first)) end
def create_coercer_instance(type, method, strict)
def create_coercer_instance(type, method, strict) # Maps a custom type provided by Grape, it doesn't map types wrapped by collections!!! type = Types.map_special(type) # Use a special coercer for multiply-typed parameters. if Types.multiple?(type) MultipleTypeCoercer.new(type, method) # Use a special coercer for custom types and coercion methods. elsif method || Types.custom?(type) CustomTypeCoercer.new(type, method) # Special coercer for collections of types that implement a parse method. # CustomTypeCoercer (above) already handles such types when an explicit coercion # method is supplied. elsif Types.collection_of_custom?(type) Types::CustomTypeCollectionCoercer.new( Types.map_special(type.first), type.is_a?(Set) ) else DryTypeCoercer.coercer_instance_for(type, strict) end end
def custom?(type)
-
(Boolean)
- whether or not the type can be used as a custom type
Parameters:
-
type
(Class
) -- type to check
def custom?(type) !primitive?(type) && !structure?(type) && !multiple?(type) && type.respond_to?(:parse) && type.method(:parse).arity == 1 end
def group?(type)
-
(Boolean)
- +true+ if the type is a supported group type
Parameters:
-
type
(Array
) -- type to check, Class
def group?(type) GROUPS.include? type end
def map_special(type)
def map_special(type) SPECIAL.fetch(type, type) end
def multiple?(type)
-
(Boolean)
- +true+ if the given value will be treated as
Parameters:
-
type
(Array
) -- type (or type list!) to check, Set
def multiple?(type) (type.is_a?(Array) || type.is_a?(Set)) && type.size > 1 end
def primitive?(type)
-
(Boolean)
- whether or not the type is known by Grape as a valid
Parameters:
-
type
(Class
) -- type to check
def primitive?(type) PRIMITIVES.include?(type) end
def special?(type)
-
(Boolean)
- +true+ if special routines are available
Parameters:
-
type
(Class
) -- type to check
def special?(type) SPECIAL.key? type end
def structure?(type)
-
(Boolean)
- whether or not the type is known by Grape as a valid
Parameters:
-
type
(Class
) -- type to check
def structure?(type) STRUCTURES.include?(type) end