Sha256: c7fe1275a25942b67e8a6f52cba86bc57fb0eb285ec984f0177b22b3cdd4d28a

Contents?: true

Size: 1.81 KB

Versions: 2

Compression:

Stored size: 1.81 KB

Contents

module Nitro

	# This module adds flashing support to the Controllers. 
	
	module Flashing
	
		def self.append_features(base)
			super			
	    base.pre 'flash.discard'
	    base.post 'flash.clean'
    end
    
		# A Flash is a special hash object that lives in the session. 
		# The values stored in the Flash are typically maintained 
		# for the duration of one request. 
		# 
		# You may want to use the Flash to pass error messages or 
		# other short lived objects.
		
		class Flash < Hash
		
			def initialize
				super
				@dirty = {}
			end
		
			def []=(key, val)
				super
				keep(key)
			end

			# Keep the specific key or the whole Flash.
						
			def keep(key = nil)
				set_dirty(key, false)
			end
			
			# Discard the specific key or the whole Flash.
			
			def discard(key = nil)
				set_dirty(key)
			end			
			
      def clean # :nodoc:
        keys.each do |k|
          unless @dirty[k]
            set_dirty(k)
          else
            delete(k)
            @dirty.delete(k)
          end
        end
        
        # remove externaly updated keys.
        
        (@dirty.keys - keys).each { |k| @dirty.delete k } 
      end
      			
		private
			
			def set_dirty(key = nil, flag = true)
				if key
			  	@dirty[key] = flag
				else
					keys.each { |k| @dirty[k] = flag }
				end			
			end
			
		end

		# A Flash for the current request only.
		#--
		# gmosx: crap, not really needed!
		#++
		
		class FlashNow
		end
	
	private

		def flash
			session[:FLASH] ||= Flash.new
		end
				
		# Some useful aspects.
			
    # Marks flash entries as used and expose the flash to the 
    # view.
    
    def init_flash
      flash.discard
    end

    # Deletes the flash entries that were not marked for 
    # keeping.
    
    def clean_flash
      flash.clean
    end

	end	

end

# * George Moschovitis <gm@navel.gr>

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
nitro-0.20.0 lib/nitro/flash.rb
nitro-0.19.0 lib/nitro/flash.rb