Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

infrastructure

  • Loading branch information...
commit e729d9b138d09674318475646cbb2711698bc766 1 parent 202a8b6
Konstantin Haase authored March 24, 2011
27  lib/sinatra/contrib.rb
... ...
@@ -0,0 +1,27 @@
  1
+require 'sinatra/contrib/setup'
  2
+
  3
+module Sinatra
  4
+  module Contrib
  5
+    ##
  6
+    # Common middleware that doesn't bring run time overhead if not used
  7
+    # or breaks if external dependencies are missing. Will extend
  8
+    # Sinatra::Application by default.
  9
+    module Common
  10
+      register :ConfigFile
  11
+      register :Decompile
  12
+      register :Namespace
  13
+      helpers :LinkHeader
  14
+    end
  15
+
  16
+    ##
  17
+    # Other extensions you don't want to be loaded unless needed.
  18
+    module Custom
  19
+    end
  20
+
  21
+    ##
  22
+    # Stuff that aren't Sinatra extensions, technically.
  23
+    autoload :TestHelpers
  24
+  end
  25
+
  26
+  register Sinatra::Contrib::Common
  27
+end
46  lib/sinatra/contrib/setup.rb
... ...
@@ -0,0 +1,46 @@
  1
+require 'sinatra/base'
  2
+require 'sinatra/contrib/version'
  3
+require 'backports'
  4
+
  5
+module Sinatra
  6
+  module Contrib
  7
+    module Loader
  8
+      def extensions
  9
+        @extensions ||= {:helpers => [], :register => []}
  10
+      end
  11
+
  12
+      def register(name, path = nil)
  13
+        autoload name, path, :register
  14
+      end
  15
+
  16
+      def helpers(name, path = nil)
  17
+        autoload name, path, :helpers
  18
+      end
  19
+
  20
+      def autoload(name, path = nil, method = nil)
  21
+        path ||= "sinatra/#{name.to_s.underscore}"
  22
+        extensions[method] << name if method
  23
+        Sinatra.autoload(name, path)
  24
+      end
  25
+
  26
+      def registered(base)
  27
+        @extensions.each do |meth, list|
  28
+          base.send(meth, *list.map { |n| Sinatra.const_get n })
  29
+        end
  30
+      end
  31
+    end
  32
+
  33
+    module Common
  34
+      extend Loader
  35
+    end
  36
+
  37
+    module Custom
  38
+      extend Loader
  39
+    end
  40
+
  41
+    extend Loader
  42
+    def self.registered(base)
  43
+      base.register Common, Custom
  44
+    end
  45
+  end
  46
+end
44  lib/sinatra/contrib/version.rb
... ...
@@ -0,0 +1,44 @@
  1
+module Sinatra
  2
+  module Contrib
  3
+    def self.version
  4
+      VERSION
  5
+    end
  6
+
  7
+    module VERSION
  8
+      extend Comparable
  9
+
  10
+      MAJOR     = 1
  11
+      MINOR     = 2
  12
+      TINY      = 0
  13
+      SIGNATURE = [MAJOR, MINOR, TINY]
  14
+      STRING    = SIGNATURE.join '.'
  15
+
  16
+      def self.major; MAJOR  end
  17
+      def self.minor; MINOR  end
  18
+      def self.tiny;  TINY   end
  19
+      def self.to_s;  STRING end
  20
+
  21
+      def self.hash
  22
+        STRING.hash
  23
+      end
  24
+
  25
+      def self.<=>(other)
  26
+        other = other.split('.').map { |i| i.to_i } if other.respond_to? :split
  27
+        SIGNATURE <=> Array(other)
  28
+      end
  29
+
  30
+      def self.inspect
  31
+        STRING.inspect
  32
+      end
  33
+
  34
+      def self.respond_to?(meth, *)
  35
+        meth.to_s !~ /^__|^to_str$/ and STRING.respond_to? meth unless super
  36
+      end
  37
+
  38
+      def self.method_missing(meth, *args, &block)
  39
+        return super unless STRING.respond_to?(meth)
  40
+        STRING.send(meth, *args, &block)
  41
+      end
  42
+    end
  43
+  end
  44
+end

0 notes on commit e729d9b

Please sign in to comment.
Something went wrong with that request. Please try again.