Sha256: 938143a6715e42d8d89d09000e1618421d6c926854f35acc4b2dd27b90d93a3b

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

require "stackable_flash/flash_stack"

module StackableFlash
  module StackLayer
    def self.included(base)
      base.class_eval do
        alias_method_chain :[]=, :stacking
      end
    end

    define_method "[]_with_stacking=" do |key, value|
      if StackableFlash.stacking
        # Do it in a non-stacking block so we get normal behavior from flash[:notice] calls
        StackableFlash.not_stacked do
          # Make an array at the key, while providing a seamless upgrade to existing flashes
          #
          # Initial set to Array
          #
          # Principle of least surprise
          # flash[:notice] = ['message1','message2']
          # flash[:notice] # => ['message1','message2']
          #
          # Initial set to String
          #
          # Principle of least surprise
          # flash[:notice] = 'original'
          # flash[:notice] # => ['original']
          #
          # Overwrite!
          #
          # Principle of least surprise
          # flash[:notice] = 'original'
          # flash[:notice] = 'altered'
          # flash[:notice] # => ['altered']
          #
          # The same line of code does all of the above:
          self[key] = StackableFlash::FlashStack.new.replace(value.kind_of?(Array) ? value : Array(value))

          # Leave existing behavior in place, but send the full array as the value so it doesn't get killed.
          send("[]_without_stacking=", key, self[key])
        end
      else
        # All StackableFlash functionality is completely bypassed
        send("[]_without_stacking=", key, value)
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stackable_flash-0.0.3 lib/stackable_flash/stack_layer.rb