Sha256: cae6b904784156a5e70aab0432683fc3324b4b962ae1d2ddd8d8fb7a2cdf8235

Contents?: true

Size: 1.98 KB

Versions: 4

Compression:

Stored size: 1.98 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

4 entries across 4 versions & 1 rubygems

Version Path
nitro-0.21.0 lib/nitro/flash.rb
nitro-0.21.2 lib/nitro/flash.rb
nitro-0.22.0 lib/nitro/flash.rb
nitro-0.23.0 lib/nitro/flash.rb