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
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
Removes a specified key from the hash.
# File lib/mongo_rack/session_hash.rb, line 99 def delete(key) super(convert_key(key)) end
Returns an exact copy of the hash.
# File lib/mongo_rack/session_hash.rb, line 88 def dup SessionHash.new(self) end
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
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
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
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
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
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