diff options
-rw-r--r-- | Gemfile | 12 | ||||
-rw-r--r-- | Gemfile.lock | 40 | ||||
-rw-r--r-- | lib/rack-flash.rb | 171 |
3 files changed, 27 insertions, 196 deletions
@@ -20,15 +20,15 @@ gem "dm-validations", "=1.2.0" gem "sanitize", "=2.0.3" gem "rmail", "=1.1.0" gem "xmpp4r", "=0.5.6" -gem "eventmachine", "=0.12.10" -gem "thin", "=1.2.4" +gem "thin", "=1.6.3" gem "haml", "=3.0.15" gem "RedCloth" -gem "rack", "=1.3.4" -gem "rack-protection", "=1.1.4" -gem "rack-test", "=0.6.1" +gem "rack" +gem "rack-protection" +gem "rack-test" +gem "rack-flash3" gem "tilt", "=1.3.3" -gem "sinatra", "=1.3.1" +gem "sinatra", "=1.4.6" gem "locale", "=2.1.0" group :test do diff --git a/Gemfile.lock b/Gemfile.lock index d5d830f..8a4017f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,7 +2,7 @@ GEM remote: https://rubygems.org/ specs: RedCloth (4.2.9) - addressable (2.3.7) + addressable (2.3.8) bcrypt-ruby (3.0.1) crack (0.4.2) safe_yaml (~> 1.0.0) @@ -61,7 +61,7 @@ GEM do_sqlite3 (0.10.15) data_objects (= 0.10.15) docile (1.1.5) - eventmachine (0.12.10) + eventmachine (1.0.7) fastercsv (1.5.5) haml (3.0.15) ipaddress (0.8.0) @@ -72,10 +72,12 @@ GEM log4r (1.0.6) multi_json (1.0.4) nokogiri (1.5.11) - rack (1.3.4) - rack-protection (1.1.4) + rack (1.6.0) + rack-flash3 (1.0.5) rack - rack-test (0.6.1) + rack-protection (1.5.3) + rack + rack-test (0.6.3) rack (>= 1.0) rmail (1.1.0) ruby_protobuf (0.4.11) @@ -87,16 +89,16 @@ GEM multi_json (~> 1.0) simplecov-html (~> 0.9.0) simplecov-html (0.9.0) - sinatra (1.3.1) - rack (~> 1.3, >= 1.3.4) - rack-protection (~> 1.1, >= 1.1.2) - tilt (~> 1.3, >= 1.3.3) + sinatra (1.4.6) + rack (~> 1.4) + rack-protection (~> 1.4) + tilt (>= 1.3, < 3) spruz (0.2.13) stringex (1.3.3) - thin (1.2.4) - daemons (>= 1.0.9) - eventmachine (>= 0.12.6) - rack (>= 1.0.0) + thin (1.6.3) + daemons (~> 1.0, >= 1.0.9) + eventmachine (~> 1.0) + rack (~> 1.0) tilt (1.3.3) timecop (0.3.5) uuidtools (2.1.5) @@ -120,21 +122,21 @@ DEPENDENCIES dm-transactions (= 1.2.0) dm-types (= 1.2.0) dm-validations (= 1.2.0) - eventmachine (= 0.12.10) haml (= 3.0.15) ipaddress (= 0.8.0) json (= 1.5.4) locale (= 2.1.0) log4r (= 1.0.6) - rack (= 1.3.4) - rack-protection (= 1.1.4) - rack-test (= 0.6.1) + rack + rack-flash3 + rack-protection + rack-test rmail (= 1.1.0) ruby_protobuf (= 0.4.11) sanitize (= 2.0.3) simplecov - sinatra (= 1.3.1) - thin (= 1.2.4) + sinatra (= 1.4.6) + thin (= 1.6.3) tilt (= 1.3.3) timecop (= 0.3.5) webmock (= 1.7.6) diff --git a/lib/rack-flash.rb b/lib/rack-flash.rb deleted file mode 100644 index 7a235d2..0000000 --- a/lib/rack-flash.rb +++ /dev/null @@ -1,171 +0,0 @@ -# -# borrowed from http://github.com/nakajima/rack-flash - thanks! -# - -require 'rack/builder' - -module Rack - class Builder - attr :ins - def use(middleware, *args, &block) - middleware.instance_variable_set "@rack_builder", self - def middleware.rack_builder - @rack_builder - end - @ins << Proc.new { |app| - middleware.new(app, *args, &block) - } - end - - def run(app) - klass = app.class - klass.instance_variable_set "@rack_builder", self - def klass.rack_builder - @rack_builder - end - @ins << app #lambda { |nothing| app } - end - - def leaf_app - ins.last - end - end -end - -module Rack - class Flash - # Raised when the session passed to FlashHash initialize is nil. This - # is usually an indicator that session middleware is not in use. - class SessionUnavailable < StandardError; end - - # Implements bracket accessors for storing and retrieving flash entries. - class FlashHash - attr_reader :flagged - - def initialize(store, opts={}) - raise Rack::Flash::SessionUnavailable \ - .new('Rack::Flash depends on session middleware.') unless store - - @opts = opts - @store = store - - if accessors = @opts[:accessorize] - accessors.each { |opt| def_accessor(opt) } - end - end - - # Remove an entry from the session and return its value. Cache result in - # the instance cache. - def [](key) - key = key.to_sym - cache[key] ||= values.delete(key) - end - - # Store the entry in the session, updating the instance cache as well. - def []=(key,val) - key = key.to_sym - cache[key] = values[key] = val - end - - # Store a flash entry for only the current request, swept regardless of - # whether or not it was actually accessed. Useful for AJAX requests, where - # you want a flash message, even though you're response isn't redirecting. - def now - cache - end - - # Checks for the presence of a flash entry without retrieving or removing - # it from the cache or store. - def has?(key) - [cache, values].any? { |store| store.keys.include?(key.to_sym) } - end - alias_method :include?, :has? - - # Mark existing entries to allow for sweeping. - def flag! - @flagged = values.keys - end - - # Remove flagged entries from flash session, clear flagged list. - def sweep! - Array(flagged).each { |key| values.delete(key) } - flagged.clear - end - - # Hide the underlying :__FLASH__ session key and only expose values stored - # in the flash. - def inspect - '#<FlashHash @values=%s @cache=%s>' % [values.inspect, cache.inspect] - end - - # Human readable for logging. - def to_s - values.inspect - end - - private - - # Maintain an instance-level cache of retrieved flash entries. These - # entries will have been removed from the session, but are still available - # through the cache. - def cache - @cache ||= {} - end - - # Helper to access flash entries from :__FLASH__ session value. This key - # is used to prevent collisions with other user-defined session values. - def values - @store[:__FLASH__] ||= {} - end - - # Generate accessor methods for the given entry key if :accessorize is true. - def def_accessor(key) - raise ArgumentError.new('Invalid entry type: %s' % key) if respond_to?(key) - - class << self; self end.class_eval do - define_method(key) { |*args| val = args.first; val ? (self[key]=val) : self[key] } - define_method("#{key}=") { |val| self[key] = val } - define_method("#{key}!") { |val| cache[key] = val } - end - end - end - - # ------------------------------------------------------------------------- - # - Rack Middleware implementation - - def initialize(app, opts={}) - if klass = app_class(app, opts) - klass.class_eval do - def flash; env['x-rack.flash'] end - end - end - - @app, @opts = app, opts - end - - def call(env) - env['x-rack.flash'] ||= Rack::Flash::FlashHash.new(env['rack.session'], @opts) - - if @opts[:sweep] - env['x-rack.flash'].flag! - end - - res = @app.call(env) - - if @opts[:sweep] - env['x-rack.flash'].sweep! - end - - res - end - - private - - def app_class(app, opts) - return nil if opts.has_key?(:helper) and not opts[:helper] - opts[:flash_app_class] || - defined?(Sinatra::Base) && Sinatra::Base || - self.class.rack_builder.leaf_app.class - end - end -end |