Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added flash, dependencies and default routes
- Loading branch information
Elad Meidar
committed
Feb 21, 2010
1 parent
165597f
commit d87af56
Showing
12 changed files
with
277 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
get "/" do | ||
redirect "/welcome/" | ||
end | ||
|
||
not_found do | ||
status 404 | ||
"We don't know thee #{request.path_info}" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|