GithubHelp home page GithubHelp logo

yb66 / sinatra-default_parameters Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 23 KB

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.

License: MIT License

Ruby 100.00%
ruby sinatra par

sinatra-default_parameters's Introduction

Sinatra Default Parameters

Build status

Master branch: Build Status

Develop branch: Build Status

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. 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.

sinatra-default_parameters's People

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.