143 def deep_map(object, path = [], transform_keys:, &block)
144 if object.is_a?(Hash)
145 result = yield path, object
146
147 return result if result
148
149 object.inject({}) do |hash, (key, value)|
150 key = key.send(transform_keys)
151 result = yield [*path, key], value
152
153 hash.merge! key => (result || deep_map(value, [*path, key], transform_keys:, &block))
154 end
155 elsif object.is_a?(Array)
156 result = yield path, object
157
158 return result if result
159
160 object.map.with_index do |value, idx|
161 result = yield [*path, idx], value
162
163 result || deep_map(value, [*path, idx], transform_keys:, &block)
164 end
165 else
166 result = yield path, object
167 result or fail Shrine::Error, "leaf reached"
168 end
169 end