Sha256: 64bcee41e4892c8dcb347ae775251b9de4f344fbf6d6e8ea705161687deac747

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

module StackableFlash
  class FlashStack < Array

    # 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

2 entries across 2 versions & 1 rubygems

Version Path
stackable_flash-0.0.2 lib/stackable_flash/flash_stack.rb
stackable_flash-0.0.1 lib/stackable_flash/flash_stack.rb