Module: StackableFlash::StackLayer

Defined in:
lib/stackable_flash/stack_layer.rb

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/stackable_flash/stack_layer.rb', line 6

def [](key)
  if StackableFlash.stacking
    #Do it in a non-stacking block so we get normal behavior from flash[:notice] calls
    StackableFlash.not_stacked do
      actual = super(key)
      if actual.nil?
        send(:[]=, key, StackableFlash::FlashStack.new)
      end
    end
  else
    # All StackableFlash functionality is completely bypassed
    super(key)
  end
  return super(key)
end

#[]=(key, value) ⇒ Object

Make an array at the key, while providing a seamless upgrade to existing flashes

Initial set to Array

Principle of least surprise flash = [‘message1’,‘message2’] flash # => [‘message1’,‘message2’]

Initial set to String

Principle of least surprise flash = ‘original’ flash # => [‘original’]

Overwrite!

Principle of least surprise flash = ‘original’ flash = ‘altered’ flash # => [‘altered’]

The same line of code does all of the above. Leave existing behavior in place, but send the full array as the value so it doesn’t get killed.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/stackable_flash/stack_layer.rb', line 45

def []=(key, value)
  if StackableFlash.stacking
    super(key,
      StackableFlash::FlashStack.new.replace(
        value.kind_of?(Array) ?
          value :
          # Preserves nil values in the result... I suppose that's OK, users can compact if needed :)
          Array.new(1, value)
      )
    )
  else
    # All StackableFlash functionality is completely bypassed
    super(key, value)
  end
end