Module: Sinatra::Params

Defined in:
lib/sinatra/params.rb

Instance Method Summary collapse

Instance Method Details

#param(name, type, opts = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/sinatra/params.rb', line 12

def param(name, type, opts = {})
    params.define_singleton_method(name) { self[name] }

    raise ParameterNotPresentException.new(name) unless opts[:optional?] or not params[name].nil?

    source_type = params[name].class
    params[name] = params[name].send(source_type.convert_to(type))

    raise ParameterOutOfRangeException.new(name, :min) unless opts[:min].nil? or params[name] >= opts[:min]
    raise ParameterOutOfRangeException.new(name, :max) unless opts[:max].nil? or params[name] <= opts[:max]
    raise ParameterOutOfRangeException.new(name, :in) unless opts[:in].nil? or opts[:in].include? params[name]
end