Sha256: 7374ff1834f141e159e713176fbd81ba489a0225eb5d6ff8b31343000e66be32

Contents?: true

Size: 1.1 KB

Versions: 5

Compression:

Stored size: 1.1 KB

Contents

module StackableFlash
  class FlashStack < Array

    # TODO: Add smart filters for flashes, like 'sticky' via method_missing.

    # Handle the following use case:
    #   flash[:notice] = 'First Part'
    #   flash[:notice] += ' Second Part'
    # => ['First Part Second Part']
    define_method "+_with_stacking", lambda {|to_add|
      if StackableFlash.stacking
        if to_add.kind_of?(Array)
          self.send("+_without_stacking", to_add)
        else
          # Make sure it responds to +, otherwise just push it onto the stack
          if self.last.respond_to?(:+)
            self[self.length -1] = self.last + to_add
          else
            self << to_add
          end
        end
      else
        self.send("+_without_stacking", to_add)
      end
    }
    alias_method_chain :+, :stacking

    def stack
      if StackableFlash.stacking
        # Format the stacked flashes according to stack_with_proc lambda
        StackableFlash::Config.config[:stack_with_proc].call(self)
      else
        # All StackableFlash functionality is completely bypassed
        self
      end
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
stackable_flash-0.0.7 lib/stackable_flash/flash_stack.rb
stackable_flash-0.0.6 lib/stackable_flash/flash_stack.rb
stackable_flash-0.0.5 lib/stackable_flash/flash_stack.rb
stackable_flash-0.0.4 lib/stackable_flash/flash_stack.rb
stackable_flash-0.0.3 lib/stackable_flash/flash_stack.rb