From fd19d70134495d36ad43cd839b84b7b11e1e7c0d Mon Sep 17 00:00:00 2001 From: Konstantin Haase Date: Thu, 24 Mar 2011 08:46:25 +0100 Subject: [PATCH] infrastructure --- sinatra-contrib/lib/sinatra/contrib.rb | 27 +++++++++++ sinatra-contrib/lib/sinatra/contrib/setup.rb | 46 +++++++++++++++++++ .../lib/sinatra/contrib/version.rb | 44 ++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 sinatra-contrib/lib/sinatra/contrib.rb create mode 100644 sinatra-contrib/lib/sinatra/contrib/setup.rb create mode 100644 sinatra-contrib/lib/sinatra/contrib/version.rb diff --git a/sinatra-contrib/lib/sinatra/contrib.rb b/sinatra-contrib/lib/sinatra/contrib.rb new file mode 100644 index 00000000..7f33e5b3 --- /dev/null +++ b/sinatra-contrib/lib/sinatra/contrib.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 diff --git a/sinatra-contrib/lib/sinatra/contrib/setup.rb b/sinatra-contrib/lib/sinatra/contrib/setup.rb new file mode 100644 index 00000000..eebafaa2 --- /dev/null +++ b/sinatra-contrib/lib/sinatra/contrib/setup.rb @@ -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 diff --git a/sinatra-contrib/lib/sinatra/contrib/version.rb b/sinatra-contrib/lib/sinatra/contrib/version.rb new file mode 100644 index 00000000..1f32d8a3 --- /dev/null +++ b/sinatra-contrib/lib/sinatra/contrib/version.rb @@ -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