diff options
-rw-r--r-- | lib/rack/core_ext/hash.rb | 19 | ||||
-rw-r--r-- | lib/rack/session/abstract/id.rb | 9 |
2 files changed, 23 insertions, 5 deletions
diff --git a/lib/rack/core_ext/hash.rb b/lib/rack/core_ext/hash.rb new file mode 100644 index 00000000..f86a3611 --- /dev/null +++ b/lib/rack/core_ext/hash.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +# Hash has `transform_keys` since Ruby 2.5. +# For Ruby < 2.5, we need to add the following + +module Rack + module HashExtensions + refine Hash do + def transform_keys(&block) + hash = {} + each do |key, value| + hash[block.call(key)] = value + end + hash + end unless {}.respond_to?(:transform_keys) + end + end +end + diff --git a/lib/rack/session/abstract/id.rb b/lib/rack/session/abstract/id.rb index c9f9f458..69dafe78 100644 --- a/lib/rack/session/abstract/id.rb +++ b/lib/rack/session/abstract/id.rb @@ -9,6 +9,8 @@ require 'rack/request' require 'rack/response' require 'securerandom' +require_relative '../../core_ext/hash' + module Rack module Session @@ -17,6 +19,7 @@ module Rack # SessionHash is responsible to lazily load the session from store. class SessionHash + using ::Rack::HashExtensions include Enumerable attr_writer :id @@ -162,11 +165,7 @@ module Rack end def stringify_keys(other) - hash = {} - other.each do |key, value| - hash[key.to_s] = value - end - hash + other.transform_keys(&:to_s) end end |