Permalink
Browse files

cleanup

  • Loading branch information...
rkh committed Oct 28, 2011
1 parent c043f98 commit 827db6119be910c7824f2bafda08ff41042d642e
Showing with 1 addition and 678 deletions.
  1. +0 −27 LICENSE
  2. +1 −172 README.md
  3. +0 −156 lib/sinatra/namespace.rb
  4. +0 −20 sinatra-namespace.gemspec
  5. +0 −300 spec/sinatra/namespace_spec.rb
  6. +0 −3 spec/spec_helper.rb
View
27 LICENSE
@@ -1,27 +0,0 @@
-copyright (c) 2010 Konstantin Haase. All rights reserved.
-
-Developed by: Konstantin Haase
- http://github.com/rkh/big_band
-
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to
-deal with the Software without restriction, including without limitation the
-rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
-sell copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
- 1. Redistributions of source code must retain the above copyright notice,
- this list of conditions and the following disclaimers.
- 2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimers in the
- documentation and/or other materials provided with the distribution.
- 3. Neither the name of Konstantin Haase, nor the names of other contributors
- may be used to endorse or promote products derived from this Software without
- specific prior written permission.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
-WITH THE SOFTWARE.
View
173 README.md
@@ -1,172 +1 @@
-# Sinatra::Namespace
-
-Adds namespaces to [Sinatra](http://sinatrarb.com). Allows namespaces to have local helpers.
-
-## Features
-
-### Nesting by prefix
-
- require "sinatra"
- require "sinatra/namespace"
-
- namespace '/blog' do
- get { haml :index }
-
- get '/:entry_id' do |id|
- @entry = Entry.find(id)
- haml :entry
- end
- end
-
-### Nesting by condition
-
-For example by host name:
-
- require "sinatra"
- require "sinatra/namespace"
-
- namespace :host_name => "www.example.com" do
- # ...
- end
-
- namespace :host_name => "api.example.com" do
- # ...
- end
-
-Or any other condition routes support:
-
- require "sinatra"
- require "sinatra/namespace"
-
- namespace :agent => /Songbird/ do
- # ...
- end
-
- namespace :provides => :json do
- # ...
- end
-
-Those can of course be combined, even with patterns:
-
- namespace '/api', :agent => /MyAgent/, :provides => :xml do
- # ...
- end
-
-### Local helpers, filters and error handling
-
- require "sinatra"
- require "sinatra/namespace"
-
- helpers do
- def title
- "foo bar"
- end
-
- def posts
- Post.all
- end
- end
-
- get "/" do
- haml :index
- end
-
- namspace "/ruby" do
- before { @an_important_note = "filters work, too" }
-
- after do
- @an_important_note = nil # don't tell
- end
-
- not_found do
- "ruby does not know this ditty"
- end
-
- helpers do
- def posts
- super.where :topic => "ruby"
- end
- end
-
- get { haml :index }
-
- get "/new" do
- haml :new, {}, :object => Post.new(:topic => "ruby")
- end
- end
-
-### With modules
-
-Modular style (you can of course use the `namespace` method there, too):
-
- require "sinatra/base"
- require "sinatra/namespace"
-
- class Application < Sinatra::Base
- register Sinatra::Namespace
-
- def title
- "foo bar"
- end
-
- def posts
- Post.all
- end
-
- module Ruby
- def posts
- super.where :topic => "ruby"
- end
-
- # '/ruby'
- get { haml :index }
-
- # '/ruby/new'
- get "/new" do
- haml :new, {}, :object => Post.new(:topic => "ruby")
- end
- end
-
- namespace '/admin' do
- # ...
- end
- end
-
-So, how does one create a namespace from a module without that auto detection? Simple:
-
- Application.make_namespace SomeModule, :prefix => "/somewhere"
-
-
-Installation
-------------
-
- gem install sinatra-namespace
-
-Alternatives
-------------
-
-Sinatra::Namespace is made for sharing some state/helpers.
-If that is no what you are looking for, you have two alternative directions.
-
-Simple prefixing, shares all state/helpers:
-
- require "sinatra"
-
- admin_prefix = "/this/is/the/admin/prefix"
- get(admin_prefix) { haml :admin_index }
- get("#{admin_prefix}/new_user") { haml :new_user }
- get("#{admin_prefix}/admin_stuff") { haml :admin_stuff }
-
-Middleware, shares no state/helpers:
-
- require "sinatra/base"
-
- class Application < Sinatra::Base
- class AdminNamespace < Sinatra::Base
- get("admin/prefix") { haml :admin_index }
- get("admin/new_user") { haml :new_user }
- get("admin/admin_stuff") { haml :admin_stuff }
- end
-
- use AdminNamespace
- end
+This project is now part of [sinatra-contrib](https://github.com/sinatra/sinatra-contrib/).
View
@@ -1,156 +0,0 @@
-require 'sinatra/base'
-
-module Sinatra
- module Namespace
- module NestedMethods
- DONT_FORWARD = %w[call configure disable enable new register reset! run! set use template layout]
- attr_reader :prefix, :options, :base
-
- def get(name = nil, options = {}, &block) prefixed(:get, name, options, &block) end
- def put(name = nil, options = {}, &block) prefixed(:put, name, options, &block) end
- def post(name = nil, options = {}, &block) prefixed(:post, name, options, &block) end
- def delete(name = nil, options = {}, &block) prefixed(:delete, name, options, &block) end
- def head(name = nil, options = {}, &block) prefixed(:head, name, options, &block) end
- def before(name = "*", &block) prefixed(:before, name, &block) end
- def after(name = "*", &block) prefixed(:after, name, &block) end
-
- def helpers(*list, &block)
- include(*list) unless list.empty?
- class_eval(&block) if block
- end
-
- def settings
- return base if base.is_a? Class
- base.settings
- end
-
- def respond_to?(*args)
- return true if super
- base.respond_to?(*args) and forward(args.first)
- end
-
- def errors
- @errors ||= {}
- end
-
- def not_found(&block)
- error(404, &block)
- end
-
- def error(codes = Exception, &block)
- [*codes].each { |c| errors[c] = block }
- end
-
- private
-
- def prefixed_path(name)
- if prefix.is_a? Regexp or name.is_a? Regexp
- path = /#{prefix}#{name}/
- path = /^#{path}$/ if base.is_a? Class
- path
- else
- prefix.to_s + name.to_s
- end
- end
-
- def prefixed(method, name, *args, &block)
- if name.respond_to? :key?
- args.unshift name
- name = nil
- end
- options.each { |o, a| settings.send(o, *a ) }
- base.send(method, prefixed_path(name), *args, &block)
- end
-
- def forward?(name)
- not DONT_FORWARD.include? name.to_s
- end
-
- def method_missing(name, *args, &block)
- return super unless base.respond_to? name and forward? name
- base.send(name, *args, &block)
- end
- end
-
- module ClassMethods
- def namespace(prefix = nil, options = {}, &block)
- Namespace.setup(self, prefix, options, Module.new, &block)
- end
-
- def make_namespace(mod, options = {})
- options[:base] ||= self
- Namespace.make_namespace(mod, options)
- end
-
- def make_namespace?(klass, meth)
- return false if !auto_namespace? or klass.is_a? NestedMethods
- meths = NestedMethods.instance_methods.map { |m| m.to_s }
- if auto_namespace != true
- meths = [auto_namespace[:only]].flatten.map { |m| m.to_s } if auto_namespace.include? :only
- [auto_namespace[:except]].flatten.each { |m| meths.delete m.to_s } if auto_namespace.include? :except
- end
- meths.include? meth.to_s
- end
- end
-
- module ModularMethods
- def setup(base, prefix = nil, options = {}, mixin = nil, &block)
- prefix, options = nil, prefix if options.empty? and prefix.respond_to? :key?
- prefix ||= ""
- mixin ||= self
- mixin.class_eval { @prefix, @options, @base = prefix, options, base }
- mixin.extend ClassMethods, NestedMethods
- mixin.send(:define_method, :error_block!) do |*keys|
- if block = keys.inject(nil) { |b,k| b ||= mixin.errors[k] }
- instance_eval(&block)
- else
- super(*keys)
- end
- end
- mixin.before { extend mixin }
- mixin.class_eval(&block) if block
- mixin
- end
- end
-
- module NamespaceDetector
- Module.send(:include, self)
- def method_missing(meth, *args, &block)
- return super if is_a? Class or !name
- base = Object
- detected = name.split('::').any? do |name|
- base = base.const_get(name)
- base < Sinatra::Base
- end
- if detected and base.make_namespace?(self, meth)
- Sinatra::Namespace.make_namespace self, :base => base
- send(meth, *args, &block)
- else
- super
- end
- end
- end
-
- extend ModularMethods
-
- def self.make_namespace(mod, options = {})
- base = options.delete(:base) || options.delete(:for)
- options[:prefix] ||= '/' << mod.name.gsub(/^#{base.name}::/, '').
- gsub(/::/, '/').gsub(/([a-z\d]+)([A-Z][a-z])/,'\1_\2').downcase
- setup base, options.delete(:prefix), options, mod
- end
-
- def self.included(klass)
- klass.extend ModularMethods
- super
- end
-
- def self.registered(klass)
- klass.extend ClassMethods
- klass.enable :auto_namespace
- end
- end
-
- Delegator.delegate :namespace
- register Namespace
-end
View
@@ -1,20 +0,0 @@
-Gem::Specification.new do |s|
- # Get the facts.
- s.name = "sinatra-namespace"
- s.version = "0.6.1"
- s.description = "Adds namespaces to Sinatra, allows namespaces to have local helpers."
-
- # Dependencies
- s.add_dependency "sinatra", "~> 1.1"
- s.add_development_dependency "sinatra-test-helper", "~> 0.5.0"
- s.add_development_dependency "rspec", "~> 1.3.0"
-
- # Those should be about the same in any BigBand extension.
- s.authors = ["Konstantin Haase"]
- s.email = "konstantin.mailinglists@googlemail.com"
- s.files = `git ls-files`.split("\n")
- s.has_rdoc = 'yard'
- s.homepage = "http://github.com/rkh/#{s.name}"
- s.require_paths = ["lib"]
- s.summary = s.description
-end
Oops, something went wrong.

0 comments on commit 827db61

Please sign in to comment.