class Goliath::Rack::Validation::NumericRange
use Goliath::Rack::Validation::NumericRange, {:key => ‘num’, :max => 10}
use Goliath::Rack::Validation::NumericRange, {:key => ‘num’, :min => 1}
use Goliath::Rack::Validation::NumericRange, {:key => ‘num’, :min => 1.2, :max => 3.5, :default => 2.9, :as => Float}
use Goliath::Rack::Validation::NumericRange, {:key => ‘num’, :min => 1, :max => 30, :default => 10}
@example
If no default the :min or :max values will be applied to the parameter.
falls outside the range, or is not provided the default will be used, if provided.
A middleware to validate that a parameter value is within a given range. If the value
def call(env)
def call(env) if !env['params'].has_key?(@key) || env['params'][@key].nil? env['params'][@key] = value else if env['params'][@key].instance_of?(Array) then env['params'][@key] = env['params'][@key].first end env['params'][@key] = coerce(env['params'][@key]) if (!@min.nil? && env['params'][@key] < @min) || (!@max.nil? && env['params'][@key] > @max) env['params'][@key] = value end end @app.call(env) end
def coerce(val)
def coerce(val) (@coerce_as == Float) ? val.to_f : val.to_i end
def initialize(app, opts = {})
-
(Goliath::Rack::Validation::NumericRange)
- The validator
Options Hash:
(**opts)
-
:default
(Integer
) -- The default to set if outside the range -
:as
(Class
) -- How to convert: Float will use .to_f, Integer (the default) will use .to_i -
:max
(Integer
) -- The maximum value -
:min
(Integer
) -- The minimum value -
:key
(String
) -- The key to look for in the parameters
Parameters:
-
opts
(Hash
) -- The options hash -
app
() -- The app object
def initialize(app, opts = {}) @app = app @key = opts[:key] raise Exception.new("NumericRange key required") if @key.nil? @min = opts[:min] @max = opts[:max] raise Exception.new("NumericRange requires :min or :max") if @min.nil? && @max.nil? @coerce_as = opts[:as] raise Exception.new("NumericRange requires :as to be Float or Integer (default)") unless [nil, Integer, Float].include?(@coerce_as) @default = opts[:default] end
def value
def value @default || @min || @max end