lib/lotus/utils/hash.rb in lotus-utils-0.5.2 vs lib/lotus/utils/hash.rb in lotus-utils-0.6.0
- old
+ new
@@ -1,10 +1,26 @@
+require 'lotus/utils/duplicable'
+
module Lotus
module Utils
# Hash on steroids
# @since 0.1.0
class Hash
+ # @since 0.6.0
+ # @api private
+ #
+ # @see Lotus::Utils::Hash#deep_dup
+ # @see Lotus::Utils::Duplicable
+ DUPLICATE_LOGIC = Proc.new do |value|
+ case value
+ when Hash
+ value.deep_dup
+ when ::Hash
+ Hash.new(value).deep_dup.to_h
+ end
+ end.freeze
+
# Initialize the hash
#
# @param hash [#to_h] the value we want to use to initialize this instance
# @param blk [Proc] define the default value
#
@@ -143,11 +159,11 @@
#
# # it deeply duplicates Lotus::Utils::Hash, by preserving the class
# duped['u_hash'].class # => Lotus::Utils::Hash
def deep_dup
Hash.new.tap do |result|
- @hash.each {|k, v| result[k] = duplicate(v) }
+ @hash.each {|k, v| result[k] = Duplicable.dup(v, &DUPLICATE_LOGIC) }
end
end
# Returns a new array populated with the keys from this hash
#
@@ -275,25 +291,9 @@
#
# @api private
# @since 0.3.0
def respond_to_missing?(m, include_private=false)
@hash.respond_to?(m, include_private)
- end
-
- private
- # @api private
- # @since 0.3.1
- def duplicate(value)
- case value
- when NilClass, FalseClass, TrueClass, Symbol, Numeric
- value
- when Hash
- value.deep_dup
- when ::Hash
- Hash.new(value).deep_dup.to_h
- else
- value.dup
- end
end
end
end
end