Sha256: 56e2a2896a7410495ddfb323ed4998c1fe23edb8b09f7e2dfae9786384537214
Contents?: true
Size: 1.27 KB
Versions: 9
Compression:
Stored size: 1.27 KB
Contents
module Goliath module Rack module Validation class FailedCoerce < StandardError attr_reader :error def initialize(error) @error = error end end module Coerce NOT_CLASS_ERROR = "Params as must be a class" INVALID_COERCE_TYPE = "%s does not respond to coerce" def coerce_setup!(opts={}) as = opts.delete(:as) if as unless Class === as raise Exception.new(NOT_CLASS_ERROR) end @coerce_instance = as.new unless @coerce_instance.respond_to?(:coerce) raise Exception.new(INVALID_COERCE_TYPE % @coerce_instance) end end end def call(env) begin coerce_value(env['params']) if @coerce_instance nil rescue FailedCoerce => e return e.error unless @optional end super if defined?(super) end def coerce_value(params) opts = {:default => @default, :message => @message} value_before_coerce = fetch_key(params) value_after_coerce = @coerce_instance.coerce(value_before_coerce, opts) fetch_key(params, value_after_coerce) end end end end end
Version data entries
9 entries across 9 versions & 1 rubygems