Class: Hanami::Action::Flash

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/action/flash.rb

Overview

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

Constant Summary collapse

KEY =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0

"_flash"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Flash

Returns a new flash object.

Parameters:

  • hash (Hash, nil) (defaults to: {})

    the flash hash for the current request; ‘nil` will become an empty hash.

Since:

  • 0.3.0



39
40
41
42
# File 'lib/hanami/action/flash.rb', line 39

def initialize(hash = {})
  @flash = hash || {}
  @next = {}
end

Instance Attribute Details

#nextHash (readonly)

Returns The flash hash for the next request, written to by #[]=.

Returns:

  • (Hash)

    The flash hash for the next request, written to by #[]=.

See Also:

Since:

  • 2.0.0



31
32
33
# File 'lib/hanami/action/flash.rb', line 31

def next
  @next
end

Instance Method Details

#[](key) ⇒ Object?

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

Parameters:

  • key (Object)

    the key

Returns:

  • (Object, nil)

    the value

Since:

  • 0.3.0



62
63
64
# File 'lib/hanami/action/flash.rb', line 62

def [](key)
  @flash[key]
end

#[]=(key, value) ⇒ Object

Updates the next hash with the given key and value.

Parameters:

  • key (Object)

    the key

  • value (Object)

    the value

Since:

  • 0.3.0



73
74
75
# File 'lib/hanami/action/flash.rb', line 73

def []=(key, value)
  @next[key] = value
end

#discard(key) ⇒ Object #discardObject

Removes entries from the next hash.

Overloads:

  • #discard(key) ⇒ Object

    Removes the given key from the next hash

    Parameters:

    • key (Object)

      key to discard

  • #discardObject

    Clears the next hash

Since:

  • 2.0.0



136
137
138
139
140
141
142
# 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 {|element| ... } ⇒ now

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

Yield Parameters:

  • element (Array<(Object, Object)>)

    array containing the key and value from the hash

Returns:

Since:

  • 1.2.0



86
87
88
# File 'lib/hanami/action/flash.rb', line 86

def each(&block)
  @flash.each(&block)
end

#empty?Boolean

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

Returns:

  • (Boolean)

Since:

  • 0.3.0



110
111
112
# File 'lib/hanami/action/flash.rb', line 110

def empty?
  @flash.empty?
end

#keep(key) ⇒ Object #keepObject

Copies entries from the current hash to the next hash

Overloads:

  • #keep(key) ⇒ Object

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

    Parameters:

    • key (Object)

      key to copy

  • #keepObject

    Copies all entries from the current hash to the next hash

Since:

  • 2.0.0



157
158
159
160
161
162
163
# 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) ⇒ Boolean

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

Returns:

  • (Boolean)

Since:

  • 2.0.0



120
121
122
# File 'lib/hanami/action/flash.rb', line 120

def key?(key)
  @flash.key?(key)
end

#map {|element| ... } ⇒ Array

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

Yield Parameters:

  • element (Array<(Object, Object)>)

    array containing the key and value from the hash

Returns:

  • (Array)

Since:

  • 1.2.0



100
101
102
# File 'lib/hanami/action/flash.rb', line 100

def map(&block)
  @flash.map(&block)
end

#nowHash

Returns the flash hash for the current request.

Returns:

  • (Hash)

    the flash hash for the current request

Since:

  • 2.0.0



50
51
52
# File 'lib/hanami/action/flash.rb', line 50

def now
  @flash
end

#sweepObject

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

Since:

  • 2.0.0



169
170
171
172
173
# File 'lib/hanami/action/flash.rb', line 169

def sweep
  @flash = @next.dup
  @next.clear
  self
end