https://github.com/yb66/sinatra-default_parameters
Extends the request context with a helper to give a quick and simple way to set defaults for the params helper on a per route basis.
https://github.com/yb66/sinatra-default_parameters
par ruby sinatra
Last synced: 9 months ago
JSON representation
Extends the request context with a helper to give a quick and simple way to set defaults for the params helper on a per route basis.
- Host: GitHub
- URL: https://github.com/yb66/sinatra-default_parameters
- Owner: yb66
- License: mit
- Created: 2013-02-18T01:35:21.000Z (almost 13 years ago)
- Default Branch: master
- Last Pushed: 2018-05-04T18:03:54.000Z (over 7 years ago)
- Last Synced: 2024-03-15T02:46:03.187Z (almost 2 years ago)
- Topics: par, ruby, sinatra
- Language: Ruby
- Size: 22.5 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES.md
Awesome Lists containing this project
README
# Sinatra Default Parameters #
## Build status ##
Master branch:
[](https://travis-ci.org/yb66/sinatra-default_parameters)
Develop branch:
[](https://travis-ci.org/yb66/sinatra-default_parameters)
## What? ##
A quick and simple way to set defaults for the params helper on a per route basis.
## Why? ##
I was inspired to make this into a gem after [answering this question on StackOverflow](http://stackoverflow.com/a/14885171/335847). I liked the look of it and thought I'll use this again, hence a gem.
## How? ##
It's quite simple.
require 'sinatra/default_parameters'
helpers Sinatra::DefaultParameters # This too if you're using modular style apps.
get "/" do
set_defaults a: 1, b: 2, c: 3
params.inspect
end
# GET "/"
# => {"a"=>1, "b"=>2, "c"=>3}
# GET "/?a=7&b=4"
# => {"a"=>"7", "b"=>"4", "c"=>3}
# GET "/?d=4"
# => {"a"=>1, "b"=>2, "c"=>3, "d"=>"4"}
# GET "/?a=7&b=4&d=4"
# => {"a"=>"7", "b"=>"4", "c"=>3, "d"=>"4"}
Simples, innit.
***Note:*** As you can see from the examples it doesn't do any type casting or other checks, it just does this one very small thing, the rest is up to you.
## Licence ##
See the LICENCE file.