class Hanami::Action::Flash

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

@since 0.3.0 @api private

Constants

KEPT_KEY

Session key where keep data is store for redirect

@since 1.3.0 @api private

SESSION_KEY

Session key where the data is stored

@since 0.3.0 @api private

Public Class Methods

new(session) click to toggle source

Initialize a new Flash instance

@param session [Rack::Session::Abstract::SessionHash] the session

@return [Hanami::Action::Flash] the flash

@see www.rubydoc.info/gems/rack/Rack/Session/Abstract/SessionHash @see Hanami::Action::Rack#session_id

# File lib/hanami/action/flash.rb, line 31
def initialize(session)
  @session         = session
  @keep            = false

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

Public Instance Methods

[](key) click to toggle source

Get the value associated to the given key, if any

@return [Object,NilClass] the value

@since 0.3.0 @api private

# File lib/hanami/action/flash.rb, line 56
def [](key)
  _data.fetch(key) { search_in_kept_data(key) }
end
[]=(key, value) click to toggle source

Set the given value for the given key

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

@since 0.3.0 @api private

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

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

@return [void]

@since 0.3.0 @api private

# 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) click to toggle source

Iterates through current request data and kept data

@param blk [Proc]

@since 1.2.0

# File lib/hanami/action/flash.rb, line 65
def each(&blk)
  _values.each(&blk)
end
empty?() click to toggle source

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

@return [TrueClass,FalseClass] the result of the check

@since 0.3.0 @api private

# File lib/hanami/action/flash.rb, line 106
def empty?
  _values.empty?
end
inspect() click to toggle source

@return [String]

@since 1.0.0

# 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!() click to toggle source

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

@since 1.3.0 @api private

@see Hanami::Action::Flash#empty?

# File lib/hanami/action/flash.rb, line 124
def keep!
  return if empty?
  @keep = true
end
map(&blk) click to toggle source

Iterates through current request data and kept data

@param blk [Proc] @return [Array]

@since 1.2.0

# File lib/hanami/action/flash.rb, line 75
def map(&blk)
  _values.map(&blk)
end

Private Instance Methods

_data() click to toggle source

The flash registry that holds the data for the current requests

@return [Hash] the flash

@since 0.3.0 @api private

# File lib/hanami/action/flash.rb, line 137
def _data
  @session[SESSION_KEY] || {}
end
_values() click to toggle source

Returns the values from current session and kept.

@return [Hash]

@since 0.3.0 @api private

# File lib/hanami/action/flash.rb, line 162
def _values
  _data.merge(kept_data)
end
expire_kept() click to toggle source

Removes from kept data those who have lived for more than two requests

@return [Hash] the current value of KEPT_KEY

@since 1.3.0 @api private

# File lib/hanami/action/flash.rb, line 194
def expire_kept
  new_kept_data = kept.reject do |kept_data|
    parsed = Hanami::Utils::Json.parse(kept_data)
    parsed['count'] >= 2 if is_hash?(parsed) && parsed['count'].is_a?(Integer)
  end

  update_kept(new_kept_data)
end
is_hash?(data) click to toggle source

Check if data is a hash

@param data @return [TrueClass, FalseClass]

@since 1.3.0 @api private

# File lib/hanami/action/flash.rb, line 265
def is_hash?(data)
  data && data.is_a?(Hash)
end
keep_data() click to toggle source

Merge current data into KEPT_KEY hash

@return [Hash] the current value of KEPT_KEY

@since 1.3.0 @api private

# File lib/hanami/action/flash.rb, line 182
def keep_data
  new_kept_data = kept << Hanami::Utils::Json.generate({ count: 0, data: _data })

  update_kept(new_kept_data)
end
kept() click to toggle source

Get the kept request data

@return [Array]

@since 1.3.0 @api private

# File lib/hanami/action/flash.rb, line 172
def kept
  @session[KEPT_KEY] || []
end
kept_data() click to toggle source

Values from kept

@return [Hash]

@since 1.3.0 @api private

# File lib/hanami/action/flash.rb, line 254
def kept_data
  kept.each_with_object({}) { |kept_data, result| result.merge!(Hanami::Utils::Json.parse(kept_data)['data']) }
end
remove() click to toggle source

Remove the flash entirely from the session if empty.

@return [void]

@since 0.3.0 @api private

@see Hanami::Action::Flash#empty?

# File lib/hanami/action/flash.rb, line 149
def remove
  if empty?
    @session.delete(SESSION_KEY)
    @session.delete(KEPT_KEY)
  end
end
search_in_kept_data(key) click to toggle source

Search in the kept data for a match on the key

@param key [#to_s] the key @return [Object, NilClass]

@since 1.3.0 @api private

# File lib/hanami/action/flash.rb, line 226
def search_in_kept_data(key)
  string_key = key.to_s

  data = kept.find do |kept_data|
    parsed = Hanami::Utils::Json.parse(kept_data)
    parsed['data'].fetch(string_key, nil) if is_hash?(parsed['data'])
  end

  Hanami::Utils::Json.parse(data)['data'][string_key] if data
end
update_kept(new_kept_data) click to toggle source

Set the given new_kept_data to KEPT_KEY

@param new_kept_data @return [Hash] the current value of KEPT_KEY

@since 1.3.0 @api private

# File lib/hanami/action/flash.rb, line 244
def update_kept(new_kept_data)
  @session[KEPT_KEY] = new_kept_data
end
update_kept_request_count() click to toggle source

Update the count of request for each kept data

@return [Hash] the current value of KEPT_KEY

@since 1.3.0 @api private

# File lib/hanami/action/flash.rb, line 209
def update_kept_request_count
  new_kept_data = kept.map do |kept_data|
    parsed = Hanami::Utils::Json.parse(kept_data)
    parsed['count'] += 1 if is_hash?(parsed) && parsed['count'].is_a?(Integer)
    Hanami::Utils::Json.generate(parsed)
  end

  update_kept(new_kept_data)
end