Skip to content

Commit

Permalink
Merge pull request #148 from dblock/hashie-extensions
Browse files Browse the repository at this point in the history
Reorganized extensions, consolidated Hashie StringifyKeys implementation.
  • Loading branch information
dblock committed May 1, 2014
2 parents 6bb94f8 + c982c75 commit cb7b041
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 144 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
21 changes: 10 additions & 11 deletions lib/hashie.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
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'
autoload :MethodAccess, 'hashie/extensions/method_access'
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'
Expand Down
3 changes: 2 additions & 1 deletion lib/hashie/dash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
82 changes: 0 additions & 82 deletions lib/hashie/extensions/key_conversion.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
19 changes: 19 additions & 0 deletions lib/hashie/extensions/pretty_inspect.rb
Original file line number Diff line number Diff line change
@@ -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
44 changes: 44 additions & 0 deletions lib/hashie/extensions/stringify_keys.rb
Original file line number Diff line number Diff line change
@@ -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
44 changes: 44 additions & 0 deletions lib/hashie/extensions/symbolize_keys.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 9 additions & 2 deletions lib/hashie/hash.rb
Original file line number Diff line number Diff line change
@@ -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 = {})
Expand Down
47 changes: 0 additions & 47 deletions lib/hashie/hash_extensions.rb

This file was deleted.

3 changes: 2 additions & 1 deletion lib/hashie/mash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions spec/hashie/hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
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
Expand Down
8 changes: 8 additions & 0 deletions spec/hashie/mash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -469,4 +469,12 @@ class SubMash < Hashie::Mash
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

0 comments on commit cb7b041

Please sign in to comment.