Permalink
Browse files

Added flash, dependencies and default routes

  • Loading branch information...
1 parent 165597f commit d87af56a389b5e4d306ba7b63a0c76b890c4fc70 Elad Meidar committed Feb 21, 2010
View
@@ -28,7 +28,3 @@
Dir.glob("app/controllers/**/*.rb") {|file| load file}
Dir.glob("app/models/**/*.rb") {|file| load file}
Dir.glob("app/helpers/**/*.rb") {|file| load file}
-
-
-puts Sinatra::Application.routes['GET'].inspect
-exit 0
@@ -1,9 +1,7 @@
-get '/' do
- "overwriting works"
-end
get "/welcome/?" do
- @name = "Amazing!"
+ flash[:notice] = 'i am so awesome'
+ @name = "sutff"
erb :"welcome/index"
end
@@ -0,0 +1,41 @@
+module Sinatra
+
+ module Authentication
+
+
+ def current_user
+ session[:authorized_user] ||= false
+ end
+
+ def logout!
+ session.delete :authorized_user
+ end
+
+ def authenticated?
+ session[:authorized_user]
+ end
+
+ def authenticate!
+ session[:authorized_user] = true
+ end
+
+ def authorize!(redirect="/denied")
+ redirect(redirect) unless authenticated?
+ end
+
+ get "/denied" do
+ "Access denied!"
+ end
+
+ post "/denied" do
+ "Access denied!"
+ end
+
+ alias_method :no_guest!, :authorize!
+ alias_method :acts_as_member?, :authenticated?
+ alias_method :bye!, :logout!
+
+ end
+
+ helpers Authentication
+end
View
@@ -0,0 +1,77 @@
+require 'sinatra/base'
+
+class FlashObject
+
+ VALID_KEYS = [:notice, :message, :error, :warning]
+ private
+ attr_accessor :flash_hash
+
+ public
+ def initialize
+ @flash_hash = Hash.new(nil)
+ end
+
+ def [](flash_key)
+ if VALID_KEYS.include?(flash_key)
+ @flash_hash[flash_key]
+ else
+ raise "'#{flash_key}' is not a valid flash key"
+ end
+ end
+
+ def []=(flash_key, value)
+ if VALID_KEYS.include?(flash_key)
+ @flash_hash[flash_key] = value
+ else
+ raise "'#{flash_key}' is not a valid flash key"
+ end
+ end
+
+ def clear
+ @flash_hash = Hash.new(nil)
+ end
+
+ def empty?
+ @flash_hash.empty?
+ end
+
+ VALID_KEYS.each do |key|
+ define_method("pending_#{key}?") do
+ !(@flash_hash[key].nil?)
+ end
+ end
+end
+
+module Sinatra
+ module Flash
+
+ def flash
+ @_flash ||= FlashObject.new
+ end
+
+ def redirect(uri, *args)
+ session[:_flash] = flash unless flash.empty?
+ status 302
+ response['Location'] = uri
+ halt(*args)
+ end
+
+ def flash_helper(specific_flash_key = nil)
+ flash_keys = (specific_flash_key.nil? ? FlashObject::VALID_KEYS : [specific_flash_key.to_sym])
+ flash_content = ""
+ flash_keys.each do |flash_key|
+ if flash.send("pending_#{flash_key}?")
+ flash_content += "<div id='flash_#{flash_key}'>#{flash[flash_key]}</div>"
+ end
+ end
+ flash_content
+ end
+ end
+
+ helpers Flash
+
+ before do
+ @_flash, session[:_flash] = session[:_flash], nil if session[:_flash]
+ end
+
+end
View
@@ -0,0 +1,18 @@
+module Sinatra
+
+ module Liquid
+
+ def liquid(template, options={}, locals={})
+ render :liquid, template, options, locals
+ end
+
+ def render_liquid(template, data, options, locals, &block)
+ locals['content'] = block.nil? ? '' : yield
+ ::Liquid::Template.parse(data).render(locals)
+ end
+
+ end
+
+ helpers Liquid
+
+end
@@ -0,0 +1,21 @@
+module Sinatra
+
+ module Markdown
+
+ def markdown(template, options={}, locals={})
+ render :markdown, template, options, locals
+ end
+
+ def md(template, options={}, locals={})
+ render :markdown, template, options, locals
+ end
+
+ def render_markdown(template, data, options, locals, &block)
+ RDiscount.new(data).to_html
+ end
+
+ end
+
+ helpers Markdown
+
+end
@@ -0,0 +1,26 @@
+
+module Sinatra
+
+ module Partials
+
+ def partial(template, *args)
+
+ tmp = template.to_s.split('/')
+ template = tmp[0..-2].join('/') + "/_#{tmp[-1]}"
+ options = args.last.is_a?(Hash) ? args.pop : {}
+ options.merge!(:layout => false)
+ if options[:render].nil?
+ erb(:"#{template}", options)
+ else
+ render_engine = options[:render]
+ options.delete :render
+ instance_eval("#{render_engine}(:\"#{template}\",options)")
+ end
+
+ end
+
+ end
+
+ helpers Partials
+
+end
View
@@ -0,0 +1,18 @@
+module Sinatra
+
+ module Ruty
+
+ def ruty(template, options={}, locals={})
+ render :ruty, template, options, locals
+ end
+
+ def render_ruty(template, data, options, locals, &block)
+ locals['content'] = block.nil? ? '' : yield
+ ::Ruty::Template.new(data).render(locals)
+ end
+
+ end
+
+ helpers Liquid
+
+end
@@ -0,0 +1,21 @@
+module Sinatra
+
+ module Textile
+
+ def textile(template, options={}, locals={})
+ render :textile, template, options, locals
+ end
+
+ def tx(template, options={}, locals={})
+ render :textile, template, options, locals
+ end
+
+ def render_textile(template, data, options, locals, &block)
+ RedCloth.new(data).to_html
+ end
+
+ end
+
+ helpers Textile
+
+end
@@ -20,6 +20,9 @@
<h1> Welcome to Sinatra-MVC </h1>
</div>
<div class="body">
+<div id="flash">
+ <%= flash_helper %>
+</div>
<%= yield %>
</div>
<%= partial :"welcome/footer" %>
@@ -0,0 +1,8 @@
+get "/" do
+ redirect "/welcome/"
+end
+
+not_found do
+ status 404
+ "We don't know thee #{request.path_info}"
+end
@@ -0,0 +1,42 @@
+# require "yaml"
+# require 'sinatra/base'
+#
+# ## Session Manager/Authenication
+#
+# # stuff here..
+#
+# ## Template Engine
+#
+# #require 'haml'
+# require 'erb'
+# #require 'liquid'
+# #require 'ruty'
+# #require 'mustache'
+#
+# ## Database
+#
+# #require 'ohm'
+# #require 'sequel'
+# #require 'datamapper'
+
+require 'sinatra/base'
+module Sinatra
+ module Dependencies
+
+ DEFAULT_DEPENDENCIES = [:yaml, :erb, :json]
+
+ def load_dependencies(*args)
+ (args + DEFAULT_DEPENDENCIES).each do |lib|
+ begin
+ require lib.to_s
+ rescue LoadError
+ puts "== Unable to load dependency - #{lib}"
+ exit 0
+ end
+ end
+ end
+ end
+
+ register Dependencies
+end
+

0 comments on commit d87af56

Please sign in to comment.