Module: Hanami::Action::Session

Defined in:
lib/hanami/action/session.rb

Overview

Session API

This module isn't included by default.

Since:

  • 0.1.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.

The key that returns raw session from the Rack env

Since:

  • 0.1.0

'rack.session'.freeze
ERRORS_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.

The key that is used by flash to transport errors

Since:

  • 0.3.0

:__errors

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(action) ⇒ 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.

Add session to default exposures

Since:

  • 0.4.4


27
28
29
30
31
# File 'lib/hanami/action/session.rb', line 27

def self.included(action)
  action.class_eval do
    _expose :session, :flash
  end
end

Instance Method Details

#errorsHanami::Validations::Errors

Read errors from flash or delegate to the superclass

Returns:

  • (Hanami::Validations::Errors)

    A collection of validation errors

See Also:

Since:

  • 0.3.0


72
73
74
# File 'lib/hanami/action/session.rb', line 72

def errors
  flash[ERRORS_KEY] || params.respond_to?(:errors) && params.errors
end

#sessionHash

Gets the session from the request and expose it as an Hash.

Examples:

require 'hanami/controller'
require 'hanami/action/session'

class Show
  include Hanami::Action
  include Hanami::Action::Session

  def call(params)
    # ...

    # get a value
    session[:user_id] # => '23'

    # set a value
    session[:foo] = 'bar'

    # remove a value
    session[:bax] = nil
  end
end

Returns:

  • (Hash)

    the HTTP session from the request

Since:

  • 0.1.0


60
61
62
# File 'lib/hanami/action/session.rb', line 60

def session
  @_env[SESSION_KEY] ||= {}
end