Module: SessionVars

Defined in:
documented/sessionvars.rb

Overview

Provides a way to manage session variables.

This module allows for dynamic access to session variables using a hash.

Constant Summary collapse

@@svars =
Hash.new

Class Method Summary collapse

Class Method Details

.[](name) ⇒ String?

Retrieves the value of a session variable by name.

Parameters:

  • name (String) —

    the name of the session variable to retrieve

Returns:

  • (String, nil) —

    the value of the session variable, or nil if not found



13
14
15
# File 'documented/sessionvars.rb', line 13

def self.[](name)
  @@svars[name]
end

.[]=(name, val) ⇒ void

This method returns an undefined value.

Sets the value of a session variable by name.

Parameters:

  • name (String) —

    the name of the session variable to set

  • val (String, nil) —

    the value to set, or nil to delete the variable



21
22
23
24
25
26
27
# File 'documented/sessionvars.rb', line 21

def self.[]=(name, val)
  if val.nil?
    @@svars.delete(name)
  else
    @@svars[name] = val
  end
end

.list ⇒ Hash

Returns a duplicate of the current session variables.

Returns:

  • (Hash) —

    a hash containing all session variables



31
32
33
# File 'documented/sessionvars.rb', line 31

def self.list
  @@svars.dup
end

.method_missing(arg1, arg2 = '') ⇒ String?

Handles dynamic method calls for session variable access. This allows for setting and getting session variables using method names.

Parameters:

  • arg1 (String) —

    the name of the session variable

  • arg2 (String, nil) (defaults to: '') —

    the value to set, if applicable

Returns:

  • (String, nil) —

    the value of the session variable, or nil if not found



40
41
42
43
44
45
46
47
48
49
50
# File 'documented/sessionvars.rb', line 40

def self.method_missing(arg1, arg2 = '')
  if arg1[-1, 1] == '='
    if arg2.nil?
      @@svars.delete(arg1.to_s.chop)
    else
      @@svars[arg1.to_s.chop] = arg2
    end
  else
    @@svars[arg1.to_s]
  end
end