@@ -37,7 +37,39 @@ class CustomTypeCoercer
3737
3838 TYPE_CHECK_METHODS = %i[ coerced? parsed? ] . freeze
3939 COLLECTION_TYPES = [ Array , Set ] . freeze
40- private_constant :TYPE_CHECK_METHODS , :COLLECTION_TYPES
40+
41+ # What a coercer calls to coerce and to check. Frozen objects rather
42+ # than lambdas: a lambda carries the coercer as its self and the values
43+ # it closes over as they are, so a compiled API holding one could not
44+ # be frozen all the way down.
45+ HashSymbolizer = Data . define ( :coercion ) do
46+ def call ( val ) = coercion . call ( val ) . deep_symbolize_keys
47+ end
48+
49+ # +dup+ before +map!+: the collection is whatever the user's coercion
50+ # method returned — possibly frozen, possibly the input itself — so we
51+ # must not mutate it in place. +dup+ also preserves the class, keeping a
52+ # +Set+ a +Set+ (unlike +map+, which would return an +Array+).
53+ CollectionSymbolizer = Data . define ( :coercion ) do
54+ def call ( val ) = coercion . call ( val ) . dup . map! { |item | item . is_a? ( Hash ) ? item . deep_symbolize_keys : item }
55+ end
56+
57+ InstanceCheck = Data . define ( :type ) do
58+ def call ( value ) = value . is_a? ( type )
59+ end
60+
61+ EnumerableCheck = Data . define ( :type ) do
62+ def call ( value ) = value . is_a? ( Enumerable ) && value . all? { |val | matches? ( type . first , val ) }
63+
64+ private
65+
66+ def matches? ( type , value )
67+ return value . all? { |val | matches? ( type . first , val ) } if type . is_a? ( Enumerable ) && value . is_a? ( Enumerable )
68+
69+ !type . is_a? ( Enumerable ) && value . is_a? ( type )
70+ end
71+ end
72+ private_constant :TYPE_CHECK_METHODS , :COLLECTION_TYPES , :HashSymbolizer , :CollectionSymbolizer , :InstanceCheck , :EnumerableCheck
4173
4274 # A new coercer for the given type specification
4375 # and coercion method.
@@ -72,8 +104,8 @@ def coerced?(val)
72104
73105 def build_coercion_method ( type , method )
74106 coercion_method = infer_coercion_method ( type , method )
75- return hash_symbolizer ( coercion_method ) if type == Hash
76- return collection_symbolizer ( coercion_method ) if COLLECTION_TYPES . include? ( type )
107+ return HashSymbolizer . new ( coercion_method ) if type == Hash
108+ return CollectionSymbolizer . new ( coercion_method ) if COLLECTION_TYPES . include? ( type )
77109
78110 coercion_method
79111 end
@@ -85,39 +117,13 @@ def infer_coercion_method(type, method)
85117 method . method ( :parse )
86118 end
87119
88- def hash_symbolizer ( method )
89- -> ( val ) { method . call ( val ) . deep_symbolize_keys }
90- end
91-
92- # +dup+ before +map!+: the collection is whatever the user's coercion
93- # method returned — possibly frozen, possibly the input itself — so we
94- # must not mutate it in place. +dup+ also preserves the class, keeping a
95- # +Set+ a +Set+ (unlike +map+, which would return an +Array+).
96- def collection_symbolizer ( method )
97- -> ( val ) { method . call ( val ) . dup . map! { |item | symbolize_if_hash ( item ) } }
98- end
99-
100- def symbolize_if_hash ( item )
101- item . is_a? ( Hash ) ? item . deep_symbolize_keys : item
102- end
103-
104120 def infer_type_check ( type )
105121 method_name = TYPE_CHECK_METHODS . detect { |m | type . respond_to? ( m ) }
106122 return type . method ( method_name ) if method_name
107123 return type if type . respond_to? ( :call )
108- return enumerable_type_check ( type ) if type . is_a? ( Enumerable )
109-
110- -> ( value ) { value . is_a? type }
111- end
112-
113- def enumerable_type_check ( type )
114- -> ( value ) { value . is_a? ( Enumerable ) && value . all? { |val | recursive_type_check ( type . first , val ) } }
115- end
116-
117- def recursive_type_check ( type , value )
118- return value . all? { |val | recursive_type_check ( type . first , val ) } if type . is_a? ( Enumerable ) && value . is_a? ( Enumerable )
124+ return EnumerableCheck . new ( type ) if type . is_a? ( Enumerable )
119125
120- ! type . is_a? ( Enumerable ) && value . is_a? ( type )
126+ InstanceCheck . new ( type )
121127 end
122128 end
123129 end
0 commit comments