mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
infrastructure
This commit is contained in:
parent
0aa2cc7308
commit
fd19d70134
3 changed files with 117 additions and 0 deletions
27
sinatra-contrib/lib/sinatra/contrib.rb
Normal file
27
sinatra-contrib/lib/sinatra/contrib.rb
Normal file
|
@ -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
|
46
sinatra-contrib/lib/sinatra/contrib/setup.rb
Normal file
46
sinatra-contrib/lib/sinatra/contrib/setup.rb
Normal file
|
@ -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
|
44
sinatra-contrib/lib/sinatra/contrib/version.rb
Normal file
44
sinatra-contrib/lib/sinatra/contrib/version.rb
Normal file
|
@ -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
|
Loading…
Add table
Reference in a new issue