sinatra-default_parameters 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ bin/
19
+ vendor/
20
+ .rspec
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - jruby-19mode
6
+ - rbx-19mode
7
+
8
+ # whitelist
9
+ branches:
10
+ only:
11
+ - master
12
+ - develop
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development do
6
+ gem "sinatra"
7
+ gem "wirble"
8
+ gem "yard"
9
+ gem "redcarpet"
10
+ end
11
+
12
+ group :test do
13
+ gem "rake"
14
+ gem "rack-test"
15
+ gem "rspec"
16
+ gem "simplecov"
17
+ end
data/LICENCE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Iain Barnett
2
+
3
+ MIT Licence
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicence, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # Sinatra Default Parameters #
2
+
3
+ ## Build status ##
4
+
5
+ Master branch:
6
+ [![Build Status](https://travis-ci.org/yb66/sinatra-default_parameters.png?branch=master)](https://travis-ci.org/yb66/sinatra-default_parameters)
7
+
8
+ Develop branch:
9
+ [![Build Status](https://travis-ci.org/yb66/sinatra-default_parameters.png?branch=develop)](https://travis-ci.org/yb66/sinatra-default_parameters)
10
+
11
+ ## What? ##
12
+
13
+ A quick and simple way to set defaults for the params helper on a per route basis.
14
+
15
+ ## Why? ##
16
+
17
+ 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.
18
+
19
+ ## How? ##
20
+
21
+ It's quite simple.
22
+
23
+ require 'sinatra/default_parameters'
24
+
25
+ helpers Sinatra::DefaultParameters # This too if you're using modular style apps.
26
+
27
+ get "/" do
28
+ set_defaults a: 1, b: 2, c: 3
29
+ params.inspect
30
+ end
31
+
32
+ # GET "/"
33
+ # => {"a"=>1, "b"=>2, "c"=>3}
34
+ # GET "/?a=7&b=4"
35
+ # => {"a"=>"7", "b"=>"4", "c"=>3}
36
+ # GET "/?d=4"
37
+ # => {"a"=>1, "b"=>2, "c"=>3, "d"=>"4"}
38
+ # GET "/?a=7&b=4&d=4"
39
+ # => {"a"=>"7", "b"=>"4", "c"=>3, "d"=>"4"}
40
+
41
+ Simples, innit.
42
+
43
+ ***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.
44
+
45
+
46
+ ## Licence ##
47
+
48
+ See the LICENCE file.
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ desc "(Re-) generate documentation and place it in the docs/ dir."
4
+ task :docs => :"docs:yard"
5
+ namespace :docs do
6
+ require 'yard'
7
+ YARD::Rake::YardocTask.new do |t|
8
+ t.files = ['lib/**/*.rb']
9
+ t.options = ['-odocs/', '--no-private']
10
+ end
11
+
12
+ desc "Docs including private methods."
13
+ YARD::Rake::YardocTask.new(:all) do |t|
14
+ t.files = ['lib/**/*.rb']
15
+ t.options = ['-odocs/']
16
+ end
17
+
18
+ desc "How to use the docs."
19
+ task :usage do
20
+ puts "Open the index.html file in the docs directory to read them. Does not include methods marked private unless you ran the 'all' version (you'll only need these if you plan to hack on the library itself)."
21
+ end
22
+ end
23
+
24
+ require 'rspec/core/rake_task'
25
+
26
+ desc "Run specs"
27
+ RSpec::Core::RakeTask.new do |t|
28
+ t.pattern = "./spec/**/*_spec.rb"
29
+ end
30
+
31
+ desc 'Default: run specs.'
32
+ task :default => :spec
data/examples/app.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'sinatra/base'
2
+ require 'sinatra/default_parameters'
3
+
4
+ # modular app
5
+ class Example < Sinatra::Base
6
+ helpers Sinatra::DefaultParameters
7
+
8
+ get "/" do
9
+ set_defaults start: 10, finish: 20
10
+ # This will force the order
11
+ # It's easier to compare/test the output then
12
+ Hash[ params.sort ].inspect
13
+ end
14
+
15
+ end
16
+
@@ -0,0 +1,12 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ Bundler.require
6
+
7
+ root = File.expand_path File.dirname(__FILE__)
8
+ require File.join( root , "./app.rb" )
9
+
10
+ # everything was moved into a separate module/file to make it easier to set up tests
11
+
12
+ run Example
@@ -0,0 +1,36 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'sinatra/base'
4
+
5
+ module Sinatra
6
+
7
+ # 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.
8
+ # @author Iain Barnett
9
+ module DefaultParameters
10
+
11
+ # A helper to set default keys/values on the params helper hash.
12
+ # @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.
13
+ # @params [Hash] defaults A list of the defaults.
14
+ # @example
15
+ # get "/comments/?" do
16
+ # set_defaults start: 10, finish: 20
17
+ # params.inspect
18
+ # end
19
+ # # GET http://localhost:9292/comments
20
+ # # => {"start"=>10, "finish"=>20}
21
+ # # GET http://localhost:9292/comments?start=5&units=seconds
22
+ # # => {"start"=>"5", "units"=>"seconds", "finish"=>20}
23
+ # @return [Hash] It returns the params hash, but since you've got access to that helper in the request scope it doesn't matter.
24
+ def set_defaults( defaults={} )
25
+ # stringify the keys!
26
+ h = defaults.each_with_object({}) do |(k,v),h|
27
+ h[k.to_s] = defaults[k]
28
+ end
29
+ # `merge` into `h` first to preserve the `params` already set.
30
+ # Then `merge!` that into params to set it.
31
+ params.merge!( h.merge params )
32
+ end
33
+
34
+ end
35
+ helpers DefaultParameters
36
+ end
@@ -0,0 +1,7 @@
1
+ # encoding: UTF-8
2
+
3
+ module Sinatra
4
+ module DefaultParameters
5
+ VERSION = "0.0.1"
6
+ end
7
+ end
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/sinatra/default_parameters/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Iain Barnett"]
6
+ gem.email = ["iainspeed@gmail.com"]
7
+ gem.description = %q{A quick and simple way to set defaults for the params helper on a per route basis.}
8
+ gem.summary = %q{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.}
9
+ gem.homepage = "https://github.com/yb66/sinatra-default_parameters"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "sinatra-default_parameters"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Sinatra::DefaultParameters::VERSION
17
+ gem.add_dependency("sinatra", "~>1.3")
18
+ end
@@ -0,0 +1,51 @@
1
+ # encoding: UTF-8
2
+
3
+ require "spec_helper"
4
+
5
+ require_relative "../lib/sinatra/default_parameters.rb"
6
+
7
+
8
+ describe "DefaultParameters" do
9
+
10
+ before do
11
+ get '/'
12
+ end
13
+
14
+ # check the app works ok
15
+ include_context "All pages"
16
+ it_should_behave_like "Any route"
17
+
18
+ describe "The output" do
19
+ subject { last_response.body }
20
+ context "Given no parameters via the request" do
21
+ let(:expected) { %q!{"finish"=>20, "start"=>10}! }
22
+ it { should == expected }
23
+ end
24
+ context "Given extra parameters via the request" do
25
+ before do
26
+ get '/?unit=seconds&type=race'
27
+ end
28
+ let(:expected) {
29
+ %q!{"finish"=>20, "start"=>10, "type"=>"race", "unit"=>"seconds"}!
30
+ }
31
+ it { should == expected }
32
+ end
33
+
34
+ context "Given parameters that have keys the same as the defaults" do
35
+ before do
36
+ get '/?start=5'
37
+ end
38
+ let(:expected) { %q!{"finish"=>20, "start"=>"5"}! }
39
+ it { should == expected }
40
+ context "Given extra parameters too" do
41
+ before do
42
+ get '/?unit=seconds&type=race&start=5'
43
+ end
44
+ let(:expected) {
45
+ %q!{"finish"=>20, "start"=>"5", "type"=>"race", "unit"=>"seconds"}!
46
+ }
47
+ it { should == expected }
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'rspec'
4
+ require "rack/test"
5
+
6
+ # code coverage
7
+ require 'simplecov'
8
+ SimpleCov.start do
9
+ add_filter "/vendor/"
10
+ add_filter "/bin/"
11
+ end
12
+
13
+ ENV['RACK_ENV'] ||= 'test'
14
+ ENV["EXPECT_WITH"] ||= "racktest"
15
+
16
+
17
+ Spec_dir = File.expand_path( File.dirname __FILE__ )
18
+
19
+
20
+ Dir[ File.join( Spec_dir, "/support/**/*.rb")].each do |f|
21
+ require f
22
+ end
23
+
24
+
25
+ RSpec.configure do |config|
26
+ config.treat_symbols_as_metadata_keys_with_true_values = true
27
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'sinatra/base'
4
+ require_relative "../../../lib/sinatra/default_parameters.rb"
5
+ # require_relative "./helpers.rb"
6
+
7
+ require_relative "../../../examples/app.rb"
8
+
9
+ shared_context "All pages" do
10
+ include Rack::Test::Methods
11
+ let(:app){ Example }
12
+ #include MODULE::RSpec::Helpers
13
+ end
14
+
15
+ shared_examples_for "Any route" do
16
+ subject { last_response }
17
+ it { should be_ok }
18
+ end
@@ -0,0 +1,13 @@
1
+ # encoding: UTF-8
2
+
3
+ module MODULE
4
+ module RSpec
5
+ module Helpers
6
+
7
+ # def blah( ... )
8
+ #
9
+ # end
10
+
11
+ end # Helpers
12
+ end # RSpec
13
+ end # Midas
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-default_parameters
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Iain Barnett
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sinatra
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ description: A quick and simple way to set defaults for the params helper on a per
31
+ route basis.
32
+ email:
33
+ - iainspeed@gmail.com
34
+ executables: []
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - .gitignore
39
+ - .travis.yml
40
+ - Gemfile
41
+ - LICENCE
42
+ - README.md
43
+ - Rakefile
44
+ - examples/app.rb
45
+ - examples/config.ru
46
+ - lib/sinatra/default_parameters.rb
47
+ - lib/sinatra/default_parameters/version.rb
48
+ - sinatra-default_parameters.gemspec
49
+ - spec/default_parameters_spec.rb
50
+ - spec/spec_helper.rb
51
+ - spec/support/shared/all_routes.rb
52
+ - spec/support/shared/helpers.rb
53
+ homepage: https://github.com/yb66/sinatra-default_parameters
54
+ licenses: []
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 1.8.25
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: Extends the request context with a helper to give a quick and simple way
77
+ to set defaults for the params helper on a per route basis.
78
+ test_files:
79
+ - spec/default_parameters_spec.rb
80
+ - spec/spec_helper.rb
81
+ - spec/support/shared/all_routes.rb
82
+ - spec/support/shared/helpers.rb