Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Initial Import

  • Loading branch information...
commit 1f1b8c7ad1d0d60dab57095f934dc525b3385f0f 0 parents
Tanner Burson tannerburson authored
17 method_absorber.rb
... ... @@ -0,0 +1,17 @@
  1 +require 'sinatra/base'
  2 +
  3 +module Sinatra
  4 + module MethodAbsorber
  5 + class Absorber
  6 + instance_methods.each { |m| undef_method m unless m =~ /^__/ }
  7 + def method_missing(*args)
  8 + ''
  9 + end
  10 + end
  11 +
  12 + def e(obj)
  13 + obj || Absorber.new
  14 + end
  15 + end
  16 + helpers MethodAbsorber
  17 +end
31 nested_layouts.rb
... ... @@ -0,0 +1,31 @@
  1 +require 'sinatra/base'
  2 +
  3 +module Sinatra
  4 + module NestedLayouts
  5 + module Helpers
  6 +
  7 + def erb_nested_layout(page,options={})
  8 + layout_path = begin
  9 + (class<<self;self;end).layout_name
  10 + rescue NoMethodError
  11 + options[:layout]
  12 + end
  13 + unless layout_path
  14 + layout_path = '../layout'
  15 + end
  16 + if options[:layout] != false
  17 + options.merge!(:layout => layout_path.to_sym)
  18 + end
  19 + old_erb page, options
  20 + end
  21 +
  22 + end
  23 +
  24 + def self.registered(app)
  25 + # alias the new erb helper
  26 + app.helpers NestedLayouts::Helpers
  27 + app.send(:alias_method,:old_erb, :erb)
  28 + app.send(:alias_method,:erb, :erb_nested_layout)
  29 + end
  30 + end
  31 +end
129 rack_flash.rb
... ... @@ -0,0 +1,129 @@
  1 +module Rack
  2 + class Flash
  3 + # Raised when the session passed to FlashHash initialize is nil. This
  4 + # is usually an indicator that session middleware is not in use.
  5 + class SessionUnavailable < StandardError; end
  6 +
  7 + # Implements bracket accessors for storing and retrieving flash entries.
  8 + class FlashHash
  9 + attr_reader :flagged
  10 +
  11 + def initialize(store, opts={})
  12 + raise Rack::Flash::SessionUnavailable \
  13 + .new('Rack::Flash depends on session middleware.') unless store
  14 +
  15 + @opts = opts
  16 + @store = store
  17 + @store[:__FLASH__] ||= {}
  18 +
  19 + if accessors = @opts[:accessorize]
  20 + accessors.each { |opt| def_accessor(opt) }
  21 + end
  22 + end
  23 +
  24 + # Remove an entry from the session and return its value. Cache result in
  25 + # the instance cache.
  26 + def [](key)
  27 + key = key.to_sym
  28 + cache[key] ||= values.delete(key)
  29 + end
  30 +
  31 + # Store the entry in the session, updating the instance cache as well.
  32 + def []=(key,val)
  33 + key = key.to_sym
  34 + cache[key] = values[key] = val
  35 + end
  36 +
  37 + # Store a flash entry for only the current request, swept regardless of
  38 + # whether or not it was actually accessed. Useful for AJAX requests, where
  39 + # you want a flash message, even though you're response isn't redirecting.
  40 + def now
  41 + cache
  42 + end
  43 +
  44 + # Checks for the presence of a flash entry without retrieving or removing
  45 + # it from the cache or store.
  46 + def has?(key)
  47 + [cache, values].any? { |store| store.keys.include?(key.to_sym) }
  48 + end
  49 + alias_method :include?, :has?
  50 +
  51 + # Mark existing entries to allow for sweeping.
  52 + def flag!
  53 + @flagged = values.keys
  54 + end
  55 +
  56 + # Remove flagged entries from flash session, clear flagged list.
  57 + def sweep!
  58 + Array(flagged).each { |key| values.delete(key) }
  59 + flagged.clear
  60 + end
  61 +
  62 + # Hide the underlying :__FLASH__ session key and only expose values stored
  63 + # in the flash.
  64 + def inspect
  65 + '#<FlashHash @values=%s @cache=%s>' % [values.inspect, cache.inspect]
  66 + end
  67 +
  68 + # Human readable for logging.
  69 + def to_s
  70 + values.inspect
  71 + end
  72 +
  73 + private
  74 +
  75 + # Maintain an instance-level cache of retrieved flash entries. These
  76 + # entries will have been removed from the session, but are still available
  77 + # through the cache.
  78 + def cache
  79 + @cache ||= {}
  80 + end
  81 +
  82 + # Helper to access flash entries from :__FLASH__ session value. This key
  83 + # is used to prevent collisions with other user-defined session values.
  84 + def values
  85 + @store[:__FLASH__]
  86 + end
  87 +
  88 + # Generate accessor methods for the given entry key if :accessorize is true.
  89 + def def_accessor(key)
  90 + raise ArgumentError.new('Invalid entry type: %s' % key) if respond_to?(key)
  91 +
  92 + class << self; self end.class_eval do
  93 + define_method(key) { |*args| val = args.first; val ? (self[key]=val) : self[key] }
  94 + define_method("#{key}=") { |val| self[key] = val }
  95 + define_method("#{key}!") { |val| cache[key] = val }
  96 + end
  97 + end
  98 + end
  99 +
  100 + # -------------------------------------------------------------------------
  101 + # - Rack Middleware implementation
  102 +
  103 + def initialize(app, opts={})
  104 + if defined?(Sinatra::Base)
  105 + Sinatra::Base.class_eval do
  106 + def flash; env['rack-flash'] end
  107 + end
  108 + end
  109 +
  110 + @app, @opts = app, opts
  111 + end
  112 +
  113 + def call(env)
  114 + env['rack-flash'] ||= Rack::Flash::FlashHash.new(env['rack.session'], @opts)
  115 +
  116 + if @opts[:sweep]
  117 + env['rack-flash'].flag!
  118 + end
  119 +
  120 + res = @app.call(env)
  121 +
  122 + if @opts[:sweep]
  123 + env['rack-flash'].sweep!
  124 + end
  125 +
  126 + res
  127 + end
  128 + end
  129 +end
13 rack_request.rb
... ... @@ -0,0 +1,13 @@
  1 +require 'rack/request'
  2 +
  3 +module Rack
  4 + class Request
  5 + def subdomains
  6 + @env['rack.env.subdomains'] ||= lambda {
  7 + return [] if (host.nil? ||
  8 + /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host))
  9 + host.split('.')[0...-2]
  10 + }.call
  11 + end
  12 + end
  13 +end
11 render_partial.rb
... ... @@ -0,0 +1,11 @@
  1 +require 'sinatra/base'
  2 +
  3 +module Sinatra
  4 + module RenderPartial
  5 + def partial(page, options={})
  6 + erb page, options.merge!(:layout => false)
  7 + end
  8 + end
  9 +
  10 + helpers RenderPartial
  11 +end
32 session_auth.rb
... ... @@ -0,0 +1,32 @@
  1 +module Sinatra
  2 + module SessionAuth
  3 +
  4 + def current_user
  5 + self.options.user_model.get! session[:user]
  6 + end
  7 +
  8 + def login_required
  9 + if session[:user]
  10 + return true
  11 + else
  12 + session[:return_to] = request.fullpath
  13 + redirect '/login'
  14 + return false
  15 + end
  16 + end
  17 +
  18 + def redirect_to_stored
  19 + if return_to = session[:return_to]
  20 + session[:return_to] = nil
  21 + redirect return_to
  22 + else
  23 + redirect '/'
  24 + end
  25 + end
  26 +
  27 + def logout_user
  28 + session[:user] = nil
  29 + end
  30 +
  31 + end
  32 +end

0 comments on commit 1f1b8c7

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