Reorganized extensions, consolidated Hashie StringifyKeys implementation.

This commit is contained in:
dblock 2014-04-30 13:34:37 -04:00
parent 6bb94f8989
commit c982c75fb6
12 changed files with 146 additions and 144 deletions

View File

@ -2,6 +2,8 @@
* [#146](https://github.com/intridea/hashie/issues/146): Mash#respond_to? inconsistent with #method_missing and does not respond to #permitted? - [@dblock](https://github.com/dblock).
* [#89](https://github.com/intridea/hashie/issues/89): Added Hashie::Extensions::Mash::ActiveModel for compatibility with Rails 4.x Strong Parameters - [@dblock](https://github.com/dblock).
* [#148](https://github.com/intridea/hashie/pull/148): Consolidated Hashie::Hash#stringify_keys implementation - [@dblock](https://github.com/dblock).
* Your contribution here.
## 2.1.1 (4/12/2014)

View File

@ -1,17 +1,14 @@
module Hashie
autoload :Clash, 'hashie/clash'
autoload :Dash, 'hashie/dash'
autoload :Hash, 'hashie/hash'
autoload :HashExtensions, 'hashie/hash_extensions'
autoload :Mash, 'hashie/mash'
autoload :PrettyInspect, 'hashie/hash_extensions'
autoload :Trash, 'hashie/trash'
autoload :Rash, 'hashie/rash'
autoload :Clash, 'hashie/clash'
autoload :Dash, 'hashie/dash'
autoload :Hash, 'hashie/hash'
autoload :Mash, 'hashie/mash'
autoload :Trash, 'hashie/trash'
autoload :Rash, 'hashie/rash'
module Extensions
autoload :Coercion, 'hashie/extensions/coercion'
autoload :DeepMerge, 'hashie/extensions/deep_merge'
autoload :KeyConversion, 'hashie/extensions/key_conversion'
autoload :IgnoreUndeclared, 'hashie/extensions/ignore_undeclared'
autoload :IndifferentAccess, 'hashie/extensions/indifferent_access'
autoload :MergeInitializer, 'hashie/extensions/merge_initializer'
@ -19,9 +16,11 @@ module Hashie
autoload :MethodQuery, 'hashie/extensions/method_access'
autoload :MethodReader, 'hashie/extensions/method_access'
autoload :MethodWriter, 'hashie/extensions/method_access'
autoload :StringifyKeys, 'hashie/extensions/key_conversion'
autoload :SymbolizeKeys, 'hashie/extensions/key_conversion'
autoload :StringifyKeys, 'hashie/extensions/stringify_keys'
autoload :SymbolizeKeys, 'hashie/extensions/symbolize_keys'
autoload :DeepFetch, 'hashie/extensions/deep_fetch'
autoload :PrettyInspect, 'hashie/extensions/pretty_inspect'
autoload :KeyConversion, 'hashie/extensions/key_conversion'
module Mash
autoload :ActiveModel, 'hashie/extensions/mash/active_model'

View File

@ -13,7 +13,8 @@ module Hashie
# It is preferrable to a Struct because of the in-class
# API for defining properties as well as per-property defaults.
class Dash < Hash
include PrettyInspect
include Hashie::Extensions::PrettyInspect
alias_method :to_s, :inspect
# Defines a property on the Dash. Options are

View File

@ -1,87 +1,5 @@
module Hashie
module Extensions
module StringifyKeys
# Convert all keys in the hash to strings.
#
# @example
# test = {:abc => 'def'}
# test.stringify_keys!
# test # => {'abc' => 'def'}
def stringify_keys!
keys.each do |k|
stringify_keys_recursively!(self[k])
self[k.to_s] = delete(k)
end
self
end
# Return a new hash with all keys converted
# to strings.
def stringify_keys
dup.stringify_keys!
end
protected
# Stringify all keys recursively within nested
# hashes and arrays.
def stringify_keys_recursively!(object)
if self.class === object
object.stringify_keys!
elsif ::Array === object
object.each do |i|
stringify_keys_recursively!(i)
end
object
elsif object.respond_to?(:stringify_keys!)
object.stringify_keys!
else
object
end
end
end
module SymbolizeKeys
# Convert all keys in the hash to symbols.
#
# @example
# test = {'abc' => 'def'}
# test.symbolize_keys!
# test # => {:abc => 'def'}
def symbolize_keys!
keys.each do |k|
symbolize_keys_recursively!(self[k])
self[k.to_sym] = delete(k)
end
self
end
# Return a new hash with all keys converted
# to symbols.
def symbolize_keys
dup.symbolize_keys!
end
protected
# Symbolize all keys recursively within nested
# hashes and arrays.
def symbolize_keys_recursively!(object)
if self.class === object
object.symbolize_keys!
elsif ::Array === object
object.each do |i|
symbolize_keys_recursively!(i)
end
object
elsif object.respond_to?(:symbolize_keys!)
object.symbolize_keys!
else
object
end
end
end
module KeyConversion
def self.included(base)
base.send :include, SymbolizeKeys

View File

@ -0,0 +1,19 @@
module Hashie
module Extensions
module PrettyInspect
def self.included(base)
base.send :alias_method, :hash_inspect, :inspect
base.send :alias_method, :inspect, :hashie_inspect
end
def hashie_inspect
ret = "#<#{self.class}"
stringify_keys.keys.sort.each do |key|
ret << " #{key}=#{self[key].inspect}"
end
ret << '>'
ret
end
end
end
end

View File

@ -0,0 +1,44 @@
module Hashie
module Extensions
module StringifyKeys
# Convert all keys in the hash to strings.
#
# @example
# test = {:abc => 'def'}
# test.stringify_keys!
# test # => {'abc' => 'def'}
def stringify_keys!
keys.each do |k|
stringify_keys_recursively!(self[k])
self[k.to_s] = delete(k)
end
self
end
# Return a new hash with all keys converted
# to strings.
def stringify_keys
dup.stringify_keys!
end
protected
# Stringify all keys recursively within nested
# hashes and arrays.
def stringify_keys_recursively!(object)
if self.class === object
object.stringify_keys!
elsif ::Array === object
object.each do |i|
stringify_keys_recursively!(i)
end
object
elsif object.respond_to?(:stringify_keys!)
object.stringify_keys!
else
object
end
end
end
end
end

View File

@ -0,0 +1,44 @@
module Hashie
module Extensions
module SymbolizeKeys
# Convert all keys in the hash to symbols.
#
# @example
# test = {'abc' => 'def'}
# test.symbolize_keys!
# test # => {:abc => 'def'}
def symbolize_keys!
keys.each do |k|
symbolize_keys_recursively!(self[k])
self[k.to_sym] = delete(k)
end
self
end
# Return a new hash with all keys converted
# to symbols.
def symbolize_keys
dup.symbolize_keys!
end
protected
# Symbolize all keys recursively within nested
# hashes and arrays.
def symbolize_keys_recursively!(object)
if self.class === object
object.symbolize_keys!
elsif ::Array === object
object.each do |i|
symbolize_keys_recursively!(i)
end
object
elsif object.respond_to?(:symbolize_keys!)
object.symbolize_keys!
else
object
end
end
end
end
end

View File

@ -1,11 +1,18 @@
require 'hashie/hash_extensions'
require 'hashie/extensions/stringify_keys'
require 'hashie/extensions/pretty_inspect'
module Hashie
# A Hashie Hash is simply a Hash that has convenience
# functions baked in such as stringify_keys that may
# not be available in all libraries.
class Hash < ::Hash
include HashExtensions
include Hashie::Extensions::PrettyInspect
include Hashie::Extensions::StringifyKeys
# Convert this hash into a Mash
def to_mash
::Hashie::Mash.new(self)
end
# Converts a mash back to a hash (with stringified or symbolized keys)
def to_hash(options = {})

View File

@ -1,47 +0,0 @@
module Hashie
module HashExtensions
def self.included(base)
# Don't tread on existing extensions of Hash by
# adding methods that are likely to exist.
%w(stringify_keys stringify_keys!).each do |hashie_method|
base.send :alias_method, hashie_method, "hashie_#{hashie_method}" unless base.instance_methods.include?(hashie_method)
end
end
# Destructively convert all of the keys of a Hash
# to their string representations.
def hashie_stringify_keys!
keys.each do |k|
self[k.to_s] = delete(k) unless String === k
end
self
end
# Convert all of the keys of a Hash
# to their string representations.
def hashie_stringify_keys
dup.stringify_keys!
end
# Convert this hash into a Mash
def to_mash
::Hashie::Mash.new(self)
end
end
module PrettyInspect
def self.included(base)
base.send :alias_method, :hash_inspect, :inspect
base.send :alias_method, :inspect, :hashie_inspect
end
def hashie_inspect
ret = "#<#{self.class}"
stringify_keys.keys.sort.each do |key|
ret << " #{key}=#{self[key].inspect}"
end
ret << '>'
ret
end
end
end

View File

@ -55,8 +55,9 @@ module Hashie
# mash.author # => <Mash>
#
class Mash < Hash
include Hashie::Extensions::PrettyInspect
ALLOWED_SUFFIXES = %w(? ! = _)
include Hashie::PrettyInspect
alias_method :to_s, :inspect

View File

@ -13,6 +13,12 @@ describe Hash do
expect(hash).to eq Hashie::Hash['a' => 'hey', '123' => 'bob']
end
it '#stringify_keys! turns all keys into strings non-recursively' do
hash = Hashie::Hash[:a => 'hey', 123 => { 345 => 'hey' }]
hash.stringify_keys!
expect(hash).to eq Hashie::Hash['a' => 'hey', '123' => { 345 => 'hey' }]
end
it '#stringify_keys returns a hash with stringified keys' do
hash = Hashie::Hash[:a => 'hey', 123 => 'bob']
stringified_hash = hash.stringify_keys

View File

@ -469,4 +469,12 @@ describe Hashie::Mash do
expect(mash.to_hash(symbolize_keys: true)[:outer].keys).not_to include('inner')
end
end
describe '#stringify_keys' do
it 'turns all keys into strings recursively' do
hash = Hashie::Mash[:a => 'hey', 123 => { 345 => 'hey' }]
hash.stringify_keys!
expect(hash).to eq Hashie::Hash['a' => 'hey', '123' => { '345' => 'hey' }]
end
end
end