summaryrefslogtreecommitdiff
path: root/lib/hashie/hash.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/hashie/hash.rb')
-rw-r--r--lib/hashie/hash.rb25
1 files changed, 16 insertions, 9 deletions
diff --git a/lib/hashie/hash.rb b/lib/hashie/hash.rb
index 0eade03..7a97ef6 100644
--- a/lib/hashie/hash.rb
+++ b/lib/hashie/hash.rb
@@ -18,20 +18,21 @@ module Hashie
def to_hash(options = {})
out = {}
each_key do |k|
- assignment_key = if options[:stringify_keys]
- k.to_s
- elsif options[:symbolize_keys]
- k.to_s.to_sym
- else
- k
- end
+ assignment_key =
+ if options[:stringify_keys]
+ k.to_s
+ elsif options[:symbolize_keys]
+ k.to_s.to_sym
+ else
+ k
+ end
if self[k].is_a?(Array)
out[assignment_key] ||= []
self[k].each do |array_object|
- out[assignment_key] << (array_object.is_a?(Hash) ? flexibly_convert_to_hash(array_object, options) : array_object)
+ out[assignment_key] << maybe_convert_to_hash(array_object, options)
end
else
- out[assignment_key] = self[k].is_a?(Hash) || self[k].respond_to?(:to_hash) ? flexibly_convert_to_hash(self[k], options) : self[k]
+ out[assignment_key] = maybe_convert_to_hash(self[k], options)
end
end
out
@@ -44,6 +45,12 @@ module Hashie
private
+ def maybe_convert_to_hash(object, options)
+ return object unless object.is_a?(Hash) || object.respond_to?(:to_hash)
+
+ flexibly_convert_to_hash(object, options)
+ end
+
def flexibly_convert_to_hash(object, options = {})
if object.method(:to_hash).arity.zero?
object.to_hash