class MongoRack::SessionHash

Public Instance Methods

[]=(key, value) click to toggle source

Assigns a new value to the hash:

hash = SessionHash.new
hash[:key] = "value"
# File lib/mongo_rack/session_hash.rb, line 35
def []=(key, value)
  regular_writer(convert_key(key), convert_value(value))
end
Also aliased as: regular_writer
default(key = nil) click to toggle source

Checks for default value. If key does not exits returns default for hash

# File lib/mongo_rack/session_hash.rb, line 19
def default(key = nil)
  if key.is_a?(Symbol) && include?(key = key.to_s)
    self[key]
  else
    super
  end
end
delete(key) click to toggle source

Removes a specified key from the hash.

# File lib/mongo_rack/session_hash.rb, line 99
def delete(key)
  super(convert_key(key))
end
dup() click to toggle source

Returns an exact copy of the hash.

# File lib/mongo_rack/session_hash.rb, line 88
def dup
  SessionHash.new(self)
end
fetch(key, *extras) click to toggle source

Fetches the value for the specified key, same as doing hash

# File lib/mongo_rack/session_hash.rb, line 72
def fetch(key, *extras)
  super(convert_key(key), *extras)
end
has_key?(key) click to toggle source
Alias for: key?
include?(key) click to toggle source
Alias for: key?
key?(key) click to toggle source

Checks the hash for a key matching the argument passed in:

hash = SessionHash.new
hash["key"] = "value"
hash.key? :key  # => true
hash.key? "key" # => true
# File lib/mongo_rack/session_hash.rb, line 63
def key?(key)
  super(convert_key(key))
end
Also aliased as: include?, has_key?, member?
member?(key) click to toggle source
Alias for: key?
merge(hash) click to toggle source

Merges the instantized and the specified hashes together, giving precedence to the values from the second hash Does not overwrite the existing hash.

# File lib/mongo_rack/session_hash.rb, line 94
def merge(hash)
  self.dup.update(hash)
end
merge!(other_hash) click to toggle source
Alias for: update
regular_update(other_hash) click to toggle source
Alias for: update
regular_writer(key, value) click to toggle source
Alias for: []=
update(other_hash) click to toggle source

Updates the instantized hash with values from the second:

hash_1 = SessionHash.new
hash_1[:key] = "value"

hash_2 = SessionHash.new
hash_2[:key] = "New Value!"

hash_1.update(hash_2) # => {"key"=>"New Value!"}
# File lib/mongo_rack/session_hash.rb, line 49
def update(other_hash)
  other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) }
  self
end
Also aliased as: regular_update, merge!
values_at(*indices) click to toggle source

Returns an array of the values at the specified indices:

hash = SessionHash.new
hash[:a] = "x"
hash[:b] = "y"
hash.values_at("a", "b") # => ["x", "y"]
# File lib/mongo_rack/session_hash.rb, line 83
def values_at(*indices)
  indices.collect {|key| self[convert_key(key)]}
end

Public Class Methods

new(constructor = {}) click to toggle source

Need to enable users to access session using either a symbol or a string as key This call wraps hash to provide this kind of access. No default allowed here. If a key is not found nil will be returned.

# File lib/mongo_rack/session_hash.rb, line 8
def initialize(constructor = {})
  if constructor.is_a?(Hash)
    super(constructor)
    update(constructor)        
    self.default = nil        
  else
    super(constructor)
  end
end