Permalink
Browse files

infrastructure

  • Loading branch information...
1 parent 202a8b6 commit e729d9b138d09674318475646cbb2711698bc766 @rkh committed Mar 24, 2011
Showing with 117 additions and 0 deletions.
  1. +27 −0 lib/sinatra/contrib.rb
  2. +46 −0 lib/sinatra/contrib/setup.rb
  3. +44 −0 lib/sinatra/contrib/version.rb
@@ -0,0 +1,27 @@
+require 'sinatra/contrib/setup'
+
+module Sinatra
+ module Contrib
+ ##
+ # Common middleware that doesn't bring run time overhead if not used
+ # or breaks if external dependencies are missing. Will extend
+ # Sinatra::Application by default.
+ module Common
+ register :ConfigFile
+ register :Decompile
+ register :Namespace
+ helpers :LinkHeader
+ end
+
+ ##
+ # Other extensions you don't want to be loaded unless needed.
+ module Custom
+ end
+
+ ##
+ # Stuff that aren't Sinatra extensions, technically.
+ autoload :TestHelpers
+ end
+
+ register Sinatra::Contrib::Common
+end
@@ -0,0 +1,46 @@
+require 'sinatra/base'
+require 'sinatra/contrib/version'
+require 'backports'
+
+module Sinatra
+ module Contrib
+ module Loader
+ def extensions
+ @extensions ||= {:helpers => [], :register => []}
+ end
+
+ def register(name, path = nil)
+ autoload name, path, :register
+ end
+
+ def helpers(name, path = nil)
+ autoload name, path, :helpers
+ end
+
+ def autoload(name, path = nil, method = nil)
+ path ||= "sinatra/#{name.to_s.underscore}"
+ extensions[method] << name if method
+ Sinatra.autoload(name, path)
+ end
+
+ def registered(base)
+ @extensions.each do |meth, list|
+ base.send(meth, *list.map { |n| Sinatra.const_get n })
+ end
+ end
+ end
+
+ module Common
+ extend Loader
+ end
+
+ module Custom
+ extend Loader
+ end
+
+ extend Loader
+ def self.registered(base)
+ base.register Common, Custom
+ end
+ end
+end
@@ -0,0 +1,44 @@
+module Sinatra
+ module Contrib
+ def self.version
+ VERSION
+ end
+
+ module VERSION
+ extend Comparable
+
+ MAJOR = 1
+ MINOR = 2
+ TINY = 0
+ SIGNATURE = [MAJOR, MINOR, TINY]
+ STRING = SIGNATURE.join '.'
+
+ def self.major; MAJOR end
+ def self.minor; MINOR end
+ def self.tiny; TINY end
+ def self.to_s; STRING end
+
+ def self.hash
+ STRING.hash
+ end
+
+ def self.<=>(other)
+ other = other.split('.').map { |i| i.to_i } if other.respond_to? :split
+ SIGNATURE <=> Array(other)
+ end
+
+ def self.inspect
+ STRING.inspect
+ end
+
+ def self.respond_to?(meth, *)
+ meth.to_s !~ /^__|^to_str$/ and STRING.respond_to? meth unless super
+ end
+
+ def self.method_missing(meth, *args, &block)
+ return super unless STRING.respond_to?(meth)
+ STRING.send(meth, *args, &block)
+ end
+ end
+ end
+end

0 comments on commit e729d9b

Please sign in to comment.