class Hanami::Action::Flash

A container to transport data with the HTTP session, with a lifespan of just one HTTP request or redirect.

Behaves like a hash, returning entries for the current request, except for {#[]=}, which updates the hash for the next request.

@since 0.3.0 @api public

Constants

KEY

@since 2.0.0 @api private

Attributes

next[R]

@return [Hash] The flash hash for the next request, written to by {#[]=}.

@see []=

@since 2.0.0 @api public

Public Class Methods

new(hash = {}) click to toggle source

Returns a new flash object.

@param hash [Hash, nil] the flash hash for the current request; ‘nil` will become an empty hash.

@since 0.3.0 @api public

# File lib/hanami/action/flash.rb, line 39
def initialize(hash = {})
  @flash = hash || {}
  @next = {}
end

Public Instance Methods

[](key) click to toggle source

Returns the value for the given key in the current hash.

@param key [Object] the key

@return [Object, nil] the value

@since 0.3.0 @api public

# File lib/hanami/action/flash.rb, line 62
def [](key)
  @flash[key]
end
[]=(key, value) click to toggle source

Updates the next hash with the given key and value.

@param key [Object] the key @param value [Object] the value

@since 0.3.0 @api public

# File lib/hanami/action/flash.rb, line 73
def []=(key, value)
  @next[key] = value
end
discard(key = (no_arg = true)) click to toggle source

Removes entries from the next hash.

@overload discard(key)

Removes the given key from the next hash

@param key [Object] key to discard

@overload discard

Clears the next hash

@since 2.0.0 @api public

# File lib/hanami/action/flash.rb, line 136
def discard(key = (no_arg = true))
  if no_arg
    @next.clear
  else
    @next.delete(key)
  end
end
each(&block) click to toggle source

Calls the given block once for each element in the current hash.

@yieldparam element [Array<(Object, Object)>] array containing the key and value from the

hash

@return [now]

@since 1.2.0 @api public

# File lib/hanami/action/flash.rb, line 86
def each(&block)
  @flash.each(&block)
end
empty?() click to toggle source

Returns ‘true` if the current hash contains no elements.

@return [Boolean]

@since 0.3.0 @api public

# File lib/hanami/action/flash.rb, line 110
def empty?
  @flash.empty?
end
keep(key = (no_arg = true)) click to toggle source

Copies entries from the current hash to the next hash

@overload keep(key)

Copies the entry for the given key from the current hash to the next
hash

@param key [Object] key to copy

@overload keep

Copies all entries from the current hash to the next hash

@since 2.0.0 @api public

# File lib/hanami/action/flash.rb, line 157
def keep(key = (no_arg = true))
  if no_arg
    @next.merge!(@flash)
  else
    self[key] = self[key] # rubocop:disable Lint/SelfAssignment
  end
end
key?(key) click to toggle source

Returns ‘true` if the given key is present in the current hash.

@return [Boolean]

@since 2.0.0 @api public

# File lib/hanami/action/flash.rb, line 120
def key?(key)
  @flash.key?(key)
end
map(&block) click to toggle source

Returns an array of objects returned by the block, called once for each element in the current hash.

@yieldparam element [Array<(Object, Object)>] array containing the key and value from the

hash

@return [Array]

@since 1.2.0 @api public

# File lib/hanami/action/flash.rb, line 100
def map(&block)
  @flash.map(&block)
end
now() click to toggle source

Returns the flash hash for the current request.

@return [Hash] the flash hash for the current request

@since 2.0.0 @api public

# File lib/hanami/action/flash.rb, line 50
def now
  @flash
end
sweep() click to toggle source

Replaces the current hash with the next hash and clears the next hash

@since 2.0.0 @api public

# File lib/hanami/action/flash.rb, line 169
def sweep
  @flash = @next.dup
  @next.clear
  self
end