-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #148 from dblock/hashie-extensions
Reorganized extensions, consolidated Hashie StringifyKeys implementation.
- Loading branch information
Showing
12 changed files
with
146 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters