Class: HasGlobalSession::IntegratedSession

Inherits:
Object
  • Object
show all
Defined in:
lib/has_global_session/integrated_session.rb

Overview

Helper class that enables the end user to treat the global and local session as if they were the same object. This is accomplished by implementing approximately the same interface as a Hash, and dispatching to one or the other session object depending on various factors.

This class isn’t intended to be used directly by the end user. Instead, set integrated: true in the configuration file and the Web framework integration code will manage an integrated session object for you, as well as overriding the framework’s default session accessor to return an integrated session instead.

When using an integrated session, you can always get to the underlying objects by using the #local and #global readers of this class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(local, global) ⇒ IntegratedSession

Construct a new integrated session.

Parameters

local(Object)

Local session that acts like a Hash

global(GlobalSession)

GlobalSession



27
28
29
30
# File 'lib/has_global_session/integrated_session.rb', line 27

def initialize(local, global)
  @local = local
  @global = global
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object

:nodoc:



113
114
115
116
117
118
119
120
121
# File 'lib/has_global_session/integrated_session.rb', line 113

def method_missing(meth, *args) # :nodoc:
  if @global.respond_to?(meth)
    @global.__send__(meth, *args)
  elsif @local.respond_to?(meth)
    @local.__send__(meth, *args)
  else
    super
  end
end

Instance Attribute Details

#globalObject (readonly)

Return the global-session object.



20
21
22
# File 'lib/has_global_session/integrated_session.rb', line 20

def global
  @global
end

#localObject (readonly)

Return the local-session objects, whose type may vary depending on the Web framework.



17
18
19
# File 'lib/has_global_session/integrated_session.rb', line 17

def local
  @local
end

Instance Method Details

#[](key) ⇒ Object

Retrieve a value from the global session if the supplied key is supported by the global session, else retrieve it from the local session.

Parameters

key(String)

the key

Return

value(Object)

The value associated with key, or nil if key is not present



40
41
42
43
44
45
46
47
# File 'lib/has_global_session/integrated_session.rb', line 40

def [](key)
  key = key.to_s
  if @global.supports_key?(key)
    @global[key]
  else
    @local[key]
  end
end

#[]=(key, value) ⇒ Object

Set a value in the global session (if the supplied key is supported) or the local session otherwise.

Parameters

key(String)

The key to set

value(Object)

The value to set

Return

value(Object)

Always returns the value that was set



58
59
60
61
62
63
64
65
66
67
# File 'lib/has_global_session/integrated_session.rb', line 58

def []=(key, value)
  key = key.to_s
  if @global.supports_key?(key)
    @global[key] = value
  else
    @local[key] = value
  end

  return value
end

#each_pair(&block) ⇒ Object

Iterate over each key/value pair in both the global and local session.

Block

An iterator which will be called with each key/value pair

Return

Returns the value of the last expression evaluated by the block



104
105
106
107
# File 'lib/has_global_session/integrated_session.rb', line 104

def each_pair(&block)
  @global.each_pair(&block)
  @local.each_pair(&block)
end

#has_key?(key) ⇒ Boolean

Determine whether the global or local session contains a value with the specified key.

Parameters

key(String)

The name of the key

Return

contained(true|false)

Whether the session currently has a value for the specified key.

Returns:

  • (Boolean)


76
77
78
79
# File 'lib/has_global_session/integrated_session.rb', line 76

def has_key?(key)
  key = key.to_s
  @global.has_key?(key) || @local.has_key?(key)
end

#keysObject

Return the keys that are currently present in either the global or local session.

Return

keys(Array)

List of keys contained in the global or local session.



85
86
87
# File 'lib/has_global_session/integrated_session.rb', line 85

def keys
  @global.keys + @local.keys
end

#respond_to?(meth) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


109
110
111
# File 'lib/has_global_session/integrated_session.rb', line 109

def respond_to?(meth) # :nodoc:
  return @global.respond_to?(meth) || @local.respond_to?(meth) || super
end

#valuesObject

Return the values that are currently present in the global or local session.

Return

values(Array)

List of values contained in the global or local session.



93
94
95
# File 'lib/has_global_session/integrated_session.rb', line 93

def values
  @global.values + @local.values
end