summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Doubrovkine (dB.) @dblockdotorg <dblock@dblock.org>2014-04-30 21:00:18 -0400
committerDaniel Doubrovkine (dB.) @dblockdotorg <dblock@dblock.org>2014-04-30 21:00:18 -0400
commitcb7b041d80df142be724850e214000d9524d3f40 (patch)
treedb9c3bbea04f7bbafd2d1a285029e29c2db37b85
parent6bb94f89891f8035a77a5115880ba9096abfd4f8 (diff)
parentc982c75fb600de57ab47bde5df321585d4788163 (diff)
downloadhashie-cb7b041d80df142be724850e214000d9524d3f40.tar.gz
Merge pull request #148 from dblock/hashie-extensions
Reorganized extensions, consolidated Hashie StringifyKeys implementation.
-rw-r--r--CHANGELOG.md2
-rw-r--r--lib/hashie.rb21
-rw-r--r--lib/hashie/dash.rb3
-rw-r--r--lib/hashie/extensions/key_conversion.rb82
-rw-r--r--lib/hashie/extensions/pretty_inspect.rb19
-rw-r--r--lib/hashie/extensions/stringify_keys.rb44
-rw-r--r--lib/hashie/extensions/symbolize_keys.rb44
-rw-r--r--lib/hashie/hash.rb11
-rw-r--r--lib/hashie/hash_extensions.rb47
-rw-r--r--lib/hashie/mash.rb3
-rw-r--r--spec/hashie/hash_spec.rb6
-rw-r--r--spec/hashie/mash_spec.rb8
12 files changed, 146 insertions, 144 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9869b39..d7d0a90 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -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)
diff --git a/lib/hashie.rb b/lib/hashie.rb
index e3443f5..175e455 100644
--- a/lib/hashie.rb
+++ b/lib/hashie.rb
@@ -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'
diff --git a/lib/hashie/dash.rb b/lib/hashie/dash.rb
index e6af9ac..a787c00 100644
--- a/lib/hashie/dash.rb
+++ b/lib/hashie/dash.rb
@@ -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
diff --git a/lib/hashie/extensions/key_conversion.rb b/lib/hashie/extensions/key_conversion.rb
index 1f903ee..50ba36f 100644
--- a/lib/hashie/extensions/key_conversion.rb
+++ b/lib/hashie/extensions/key_conversion.rb
@@ -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
diff --git a/lib/hashie/extensions/pretty_inspect.rb b/lib/hashie/extensions/pretty_inspect.rb
new file mode 100644
index 0000000..e7a4518
--- /dev/null
+++ b/lib/hashie/extensions/pretty_inspect.rb
@@ -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
diff --git a/lib/hashie/extensions/stringify_keys.rb b/lib/hashie/extensions/stringify_keys.rb
new file mode 100644
index 0000000..fe00951
--- /dev/null
+++ b/lib/hashie/extensions/stringify_keys.rb
@@ -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
diff --git a/lib/hashie/extensions/symbolize_keys.rb b/lib/hashie/extensions/symbolize_keys.rb
new file mode 100644
index 0000000..32f4938
--- /dev/null
+++ b/lib/hashie/extensions/symbolize_keys.rb
@@ -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
diff --git a/lib/hashie/hash.rb b/lib/hashie/hash.rb
index fae6222..c8e45ad 100644
--- a/lib/hashie/hash.rb
+++ b/lib/hashie/hash.rb
@@ -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 = {})
diff --git a/lib/hashie/hash_extensions.rb b/lib/hashie/hash_extensions.rb
deleted file mode 100644
index 24ffb41..0000000
--- a/lib/hashie/hash_extensions.rb
+++ /dev/null
@@ -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
diff --git a/lib/hashie/mash.rb b/lib/hashie/mash.rb
index 34317f9..3618bc9 100644
--- a/lib/hashie/mash.rb
+++ b/lib/hashie/mash.rb
@@ -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
diff --git a/spec/hashie/hash_spec.rb b/spec/hashie/hash_spec.rb
index c3da75b..0604d71 100644
--- a/spec/hashie/hash_spec.rb
+++ b/spec/hashie/hash_spec.rb
@@ -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
diff --git a/spec/hashie/mash_spec.rb b/spec/hashie/mash_spec.rb
index 477ab42..122953f 100644
--- a/spec/hashie/mash_spec.rb
+++ b/spec/hashie/mash_spec.rb
@@ -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