Permalink
Browse files

Step 3: Implementing callbacks

  • Loading branch information...
1 parent 9357ba5 commit d85579f34b1daeee087b5c2a8384997c9b34ff74 Carl Lerche committed Aug 27, 2009
Showing with 28 additions and 0 deletions.
  1. +28 −0 sinatrails.rb
View
@@ -13,6 +13,18 @@ def inherited(klass)
klass.class_eval { @_routes = [] }
end
+ # A helper method that provides a nice API
+ # for setting arbitrary settings
+ def set(name, value)
+ send("_set_#{name}", value)
+ end
+
+ # Nicer way of implementing the common set :foo, true
+ # idiom.
+ def enable(name)
+ set(name, true)
+ end
+
# Returns a valid rack application for the
# current sinatra application declaration.
def to_app
@@ -28,6 +40,15 @@ def to_app
to(controller.action(route[:action]))
end
end
+
+ if @_sessions
+ # If we are using sessions, then wrap the application with the
+ # session cookie store middleware.
+ app = ActionDispatch::Session::CookieStore.new(app, {:key => "_secret_key",
+ :secret => Digest::SHA2.hexdigest(Time.now.to_s + rand(100).to_s)})
+ end
+
+ app
end
def get(uri, options = {}, &block)
@@ -54,6 +75,13 @@ def route(http_method, uri, options, &block)
# Now, we finally create the action method.
define_method(action_name, &block)
end
+
+ # If we are setting the sessions, then include the
+ # appropriate module.
+ def _set_sessions(value)
+ @_sessions = value
+ include ActionController::Session if value
+ end
end
# The default behavior is that the action's return value

0 comments on commit d85579f

Please sign in to comment.