Class: Hanami::Action::Flash Private

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

Overview

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

Container useful to transport data with the HTTP session It has a life span of one HTTP request or redirect.

Since:

  • 0.3.0

Constant Summary collapse

SESSION_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.

Session key where the data is stored

Since:

  • 0.3.0

:__flash
KEPT_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.

Session key where keep data is store for redirect

Since:

  • 1.3.0

:__kept_key

Instance Method Summary collapse

Constructor Details

#initialize(session) ⇒ Hanami::Action::Flash

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

Initialize a new Flash instance

Parameters:

  • session (Rack::Session::Abstract::SessionHash)

    the session

See Also:

Since:

  • 0.3.0


31
32
33
34
35
36
37
# File 'lib/hanami/action/flash.rb', line 31

def initialize(session)
  @session         = session
  @keep            = false

  session[KEPT_KEY] ||= []
  session[SESSION_KEY] = {}
end

Instance Method Details

#[](key) ⇒ Object, NilClass

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

Get the value associated to the given key, if any

Returns:

  • (Object, NilClass)

    the value

Since:

  • 0.3.0


56
57
58
# File 'lib/hanami/action/flash.rb', line 56

def [](key)
  _data.fetch(key) { search_in_kept_data(key) }
end

#[]=(key, value) ⇒ Object

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

Set the given value for the given key

Parameters:

  • key (#to_s)

    the key

  • value (Object)

    the value

Since:

  • 0.3.0


46
47
48
# File 'lib/hanami/action/flash.rb', line 46

def []=(key, value)
  _data[key] = value
end

#clearvoid

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

This method returns an undefined value.

Removes entirely the flash from the session if it has stale contents or if empty.

Since:

  • 0.3.0


86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/hanami/action/flash.rb', line 86

def clear
  # FIXME we're just before a release and I can't find a proper way to reproduce
  # this bug that I've found via a browser.
  #
  # It may happen that `#flash` is nil, and those two methods will fail
  unless _data.nil?
    update_kept_request_count
    keep_data if @keep
    expire_kept
    remove
  end
end

#each(&blk) ⇒ Object

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

Iterates through current request data and kept data

Parameters:

  • blk (Proc)

Since:

  • 1.2.0


65
66
67
# File 'lib/hanami/action/flash.rb', line 65

def each(&blk)
  _values.each(&blk)
end

#empty?TrueClass, FalseClass

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

Check if there are contents stored in the flash from the current or the previous request.

Returns:

  • (TrueClass, FalseClass)

    the result of the check

Since:

  • 0.3.0


106
107
108
# File 'lib/hanami/action/flash.rb', line 106

def empty?
  _values.empty?
end

#inspectString

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

Returns:

  • (String)

Since:

  • 1.0.0


113
114
115
# File 'lib/hanami/action/flash.rb', line 113

def inspect
  "#<#{self.class}:#{'0x%x' % (__id__ << 1)} {:data=>#{_data.inspect}, :kept=>#{kept_data.inspect}} >"
end

#keep!TrueClass, NilClass

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

Set @keep to true, is use when triggering a redirect, and the content of _data is not empty.

Returns:

  • (TrueClass, NilClass)

See Also:

Since:

  • 1.3.0


124
125
126
127
# File 'lib/hanami/action/flash.rb', line 124

def keep!
  return if empty?
  @keep = true
end

#map(&blk) ⇒ Array

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

Iterates through current request data and kept data

Parameters:

  • blk (Proc)

Returns:

  • (Array)

Since:

  • 1.2.0


75
76
77
# File 'lib/hanami/action/flash.rb', line 75

def map(&blk)
  _values.map(&blk)
end