lib/cuba.rb in cuba-2.2.1 vs lib/cuba.rb in cuba-3.0.0.rc1
- old
+ new
@@ -1,26 +1,17 @@
require "rack"
-require "tilt"
-require "cuba/version"
-class Rack::Response
- # 301 Moved Permanently
- # 302 Found
- # 303 See Other
- # 307 Temporary Redirect
- def redirect(target, status = 302)
- self.status = status
- self["Location"] = target
- end
-end
-
class Cuba
class RedefinitionError < StandardError
end
@@methods = []
+ class << self
+ undef method_added
+ end
+
def self.method_added(meth)
@@methods << meth
end
def self.reset!
@@ -38,37 +29,52 @@
def self.define(&block)
app.run new(&block)
end
- def self.build
- Class.new(self)
- end
-
def self.prototype
@prototype ||= app.to_app
end
def self.call(env)
prototype.call(env)
end
+ def self.plugin(mixin)
+ include mixin
+ extend mixin::ClassMethods if defined?(mixin::ClassMethods)
+
+ mixin.setup(self) if mixin.respond_to?(:setup)
+ end
+
+ def self.settings
+ @settings ||= {}
+ end
+
+ def self.inherited(child)
+ child.settings.replace(settings)
+ end
+
attr :env
attr :req
attr :res
attr :captures
def initialize(&blk)
@blk = blk
@captures = []
end
+ def settings
+ self.class.settings
+ end
+
def call(env)
- dup._call(env)
+ dup.call!(env)
end
- def _call(env)
+ def call!(env)
@env = env
@req = Rack::Request.new(env)
@res = Rack::Response.new
# This `catch` statement will either receive a
@@ -84,39 +90,16 @@
res.status = 404
res.finish
end
end
- # @private Used internally by #render to cache the
- # Tilt templates.
- def _cache
- Thread.current[:_cache] ||= Tilt::Cache.new
+ def session
+ env["rack.session"] || raise(RuntimeError,
+ "You're missing a session handler. You can get started " +
+ "by adding Cuba.use Rack::Session::Cookie")
end
- private :_cache
- # Render any type of template file supported by Tilt.
- #
- # @example
- #
- # # Renders home, and is assumed to be HAML.
- # render("home.haml")
- #
- # # Renders with some local variables
- # render("home.haml", site_name: "My Site")
- #
- # # Renders with HAML options
- # render("home.haml", {}, ugly: true, format: :html5)
- #
- # # Renders in layout
- # render("layout.haml") { render("home.haml") }
- #
- def render(template, locals = {}, options = {}, &block)
- _cache.fetch(template, locals) {
- Tilt.new(template, 1, options)
- }.render(self, locals, &block)
- end
-
# The heart of the path / verb / any condition matching.
#
# @example
#
# on get do
@@ -153,11 +136,11 @@
return unless args.all? { |arg| match(arg) }
# The captures we yield here were generated and assembled
# by evaluating each of the `arg`s above. Most of these
# are carried out by #consume.
- yield *captures
+ yield(*captures)
halt res.finish
end
end
@@ -167,11 +150,11 @@
script, path = env["SCRIPT_NAME"], env["PATH_INFO"]
yield
ensure
- env["SCRIPT_NAME"], env["PATH_INFO"] = script, path unless @matched
+ env["SCRIPT_NAME"], env["PATH_INFO"] = script, path
end
private :try
def consume(pattern)
return unless match = env["PATH_INFO"].match(/\A\/(#{pattern})((?:\/|\z))/)
@@ -288,9 +271,13 @@
halt app.call(req.env)
end
def halt(response)
throw :halt, response
+ end
+
+ class << self
+ undef method_added
end
# In order to prevent people from overriding the standard Cuba
# methods like `get`, `put`, etc, we add this as a safety measure.
def self.method_added(meth)