Created
February 26, 2013 19:33
-
-
Save jkutner/5041382 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SessionHash < Hash | |
#... | |
def [](key) | |
load_for_read! | |
@data[key.to_s] | |
end | |
def []=(key, value) | |
load_for_write! | |
@data[key.to_s] = value | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def indifferent_params(object) | |
case object | |
when Hash | |
new_hash = indifferent_hash | |
object.each { |key, value| new_hash[key] = indifferent_params(value) } | |
new_hash | |
when Array | |
object.map { |item| indifferent_params(item) } | |
else | |
object | |
end | |
end | |
# Creates a Hash with indifferent access. | |
def indifferent_hash | |
Hash.new {|hash,key| hash[key.to_s] if Symbol === key } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The first way is the way Rack does this:
https://github.com/rack/rack/blob/master/lib/rack/session/abstract/id.rb#L59
The second way is how Sinatra does it for params:
https://github.com/sinatra/sinatra/blob/master/lib/sinatra/base.rb#L996