public
Last active

Hash mix-in to add IndifferentAccess behavior without duplicating a given hash instance

  • Download Gist
hash_extensions.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
module Hash::Extensions
# Author: Tilo Sloboda , 2012-09-24
# Gist: https://gist.github.com/3778285
#
# Mix-In Module Hash::Extensions::IndifferentAccess
#
# Adds "Hash with Indifferent Access" behavior to an existing Hash
# without creating a copy of that Hash.
#
# Limitation: does not work for embedded hashes.
#
# Purpose:
# When passing a parameter hash to Resque/Redis, the hash keys
# get persisted as Strings, not Symbols. including this mix-in
# into the param hash, allows to access the very same hash with symbols.
#
# e.g.:
# def perform(params_hash)
# class << params_hash
# include Hash::Extensions::IndifferentAccess # add behavior without creating duplicate
# end
# params.create_symbols_only(true) # if new keys should be created as symbols
#
# # ... Rescue Worker Code
#
# end
 
module IndifferentAccess
 
def self.included(base)
base.class_eval do
alias_method :regular_write, :[]=
%w(default update fetch delete key? values_at).each do |m|
alias_method "regular_#{m}", m
end
 
# forces new hash keys to always be symbols; default: false
def create_symbols_only( boolean=true )
@__CREATE_SYMBOLS_ONLY = boolean
end
 
def [](key)
if self.include?(key)
self.regular_fetch(key) # regular reader
else
if key.class == Symbol && self.include?(key.to_s)
return self.regular_fetch( key.to_s )
elsif key.class == String && self.include?(key.to_sym)
return self.regular_fetch( key.to_sym )
else
return nil # the key wasn't defined
end
end
end
def []=(key,value)
if self.include?(key)
self.regular_write(key , value)
else
if key.class == Symbol && self.include?(key.to_s)
self.regular_write(key.to_s , value)
elsif key.class == String && self.include?(key.to_sym)
self.regular_write(key.to_sym , value)
else # new parameter which was not there before
if @__CREATE_SYMBOLS_ONLY
self.regular_write(key.to_sym, value)
else
self.regular_write(key, value) # whatever was handed in
end
end
end
end
end # end of class eval
end # end of included()
 
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.