Class: MongoRack::SessionHash
- Defined in:
- lib/mongo_rack/session_hash.rb
Instance Method Summary collapse
-
#[]=(key, value) ⇒ Object
Assigns a new value to the hash:.
-
#default(key = nil) ⇒ Object
Checks for default value.
-
#delete(key) ⇒ Object
Removes a specified key from the hash.
-
#dup ⇒ Object
Returns an exact copy of the hash.
-
#fetch(key, *extras) ⇒ Object
Fetches the value for the specified key, same as doing hash.
-
#initialize(constructor = {}) ⇒ SessionHash
constructor
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.
-
#key?(key) ⇒ Boolean
(also: #include?, #has_key?, #member?)
Checks the hash for a key matching the argument passed in:.
-
#merge(hash) ⇒ Object
Merges the instantized and the specified hashes together, giving precedence to the values from the second hash Does not overwrite the existing hash.
- #regular_update ⇒ Object
- #regular_writer ⇒ Object
-
#stringify_keys! ⇒ Object
:nodoc:.
-
#symbolize_keys! ⇒ Object
:nodoc:.
-
#to_hash ⇒ Object
Convert to a Hash with String keys.
-
#to_options! ⇒ Object
:nodoc:.
-
#update(other_hash) ⇒ Object
(also: #merge!)
Updates the instantized hash with values from the second:.
-
#values_at(*indices) ⇒ Object
Returns an array of the values at the specified indices:.
Methods included from SessionAccess
Constructor Details
#initialize(constructor = {}) ⇒ SessionHash
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.
8 9 10 11 12 13 14 15 16 |
# 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 |
Instance Method Details
#[]=(key, value) ⇒ Object
Assigns a new value to the hash:
hash = SessionHash.new
hash[:key] = "value"
35 36 37 |
# File 'lib/mongo_rack/session_hash.rb', line 35 def []=(key, value) regular_writer(convert_key(key), convert_value(value)) end |
#default(key = nil) ⇒ Object
Checks for default value. If key does not exits returns default for hash
19 20 21 22 23 24 25 |
# 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) ⇒ Object
Removes a specified key from the hash.
99 100 101 |
# File 'lib/mongo_rack/session_hash.rb', line 99 def delete(key) super(convert_key(key)) end |
#dup ⇒ Object
Returns an exact copy of the hash.
88 89 90 |
# File 'lib/mongo_rack/session_hash.rb', line 88 def dup SessionHash.new(self) end |
#fetch(key, *extras) ⇒ Object
Fetches the value for the specified key, same as doing hash
72 73 74 |
# File 'lib/mongo_rack/session_hash.rb', line 72 def fetch(key, *extras) super(convert_key(key), *extras) end |
#key?(key) ⇒ Boolean Also known as: include?, has_key?, member?
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
63 64 65 |
# File 'lib/mongo_rack/session_hash.rb', line 63 def key?(key) super(convert_key(key)) end |
#merge(hash) ⇒ Object
Merges the instantized and the specified hashes together, giving precedence to the values from the second hash Does not overwrite the existing hash.
94 95 96 |
# File 'lib/mongo_rack/session_hash.rb', line 94 def merge(hash) self.dup.update(hash) end |
#regular_update ⇒ Object
28 |
# File 'lib/mongo_rack/session_hash.rb', line 28 alias_method :regular_update, :update |
#regular_writer ⇒ Object
27 |
# File 'lib/mongo_rack/session_hash.rb', line 27 alias_method :regular_writer, :[]= |
#stringify_keys! ⇒ Object
:nodoc:
104 |
# File 'lib/mongo_rack/session_hash.rb', line 104 def stringify_keys!; self end |
#symbolize_keys! ⇒ Object
:nodoc:
106 |
# File 'lib/mongo_rack/session_hash.rb', line 106 def symbolize_keys!; self end |
#to_hash ⇒ Object
Convert to a Hash with String keys.
111 112 113 |
# File 'lib/mongo_rack/session_hash.rb', line 111 def to_hash Hash.new(default).merge(self) end |
#to_options! ⇒ Object
:nodoc:
108 |
# File 'lib/mongo_rack/session_hash.rb', line 108 def ; self end |
#update(other_hash) ⇒ Object Also known as: merge!
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!"}
49 50 51 52 |
# 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 |
#values_at(*indices) ⇒ Object
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"]
83 84 85 |
# File 'lib/mongo_rack/session_hash.rb', line 83 def values_at(*indices) indices.collect {|key| self[convert_key(key)]} end |