lib/rh.rb in lorj-1.0.3 vs lib/rh.rb in lorj-1.0.4

- old
+ new

@@ -16,10 +16,16 @@ # limitations under the License. require 'rubygems' require 'yaml' +# Adding rh_clone at object level. This be able to use a generic rh_clone +# redefined per object Hash and Array. +class Object + alias_method :rh_clone, :clone +end + # Recursive Hash added to the Hash class class Hash # Recursive Hash deep level found counter # This function will returns the count of deep level of recursive hash. # * *Args* : @@ -123,10 +129,12 @@ # yVal.rh_exist?(:test, :test5 ) => False # # # it is like searching for nothing... # yVal.rh_exist? => nil def rh_exist?(*p) + p = p.flatten + return nil if p.length == 0 count = p.length (rh_lexist?(*p) == count) end @@ -281,12 +289,12 @@ # examples: # With hdata = { :test => { :test2 => { :test5 => :test, # 'text' => 'blabla' }, # 'test5' => 'test' }} # - # rh_key_to_symbol(1) return no diff - # rh_key_to_symbol(2) return "test5" is replaced by :test5 + # hdata.rh_key_to_symbol(1) return no diff + # hdata.rh_key_to_symbol(2) return "test5" is replaced by :test5 # # hdata = { :test => { :test2 => { :test5 => :test, # # 'text' => 'blabla' }, # # :test5 => 'test' }} # rh_key_to_symbol(3) return "test5" replaced by :test5, and "text" to :text # # hdata = { :test => { :test2 => { :test5 => :test, @@ -320,14 +328,14 @@ # examples: # With hdata = { :test => { :test2 => { :test5 => :test, # 'text' => 'blabla' }, # 'test5' => 'test' }} # - # rh_key_to_symbol?(1) return false - # rh_key_to_symbol?(2) return true - # rh_key_to_symbol?(3) return true - # rh_key_to_symbol?(4) return true + # hdata.rh_key_to_symbol?(1) return false + # hdata.rh_key_to_symbol?(2) return true + # hdata.rh_key_to_symbol?(3) return true + # hdata.rh_key_to_symbol?(4) return true def rh_key_to_symbol?(levels = 1) each do |key, value| return true if key.is_a?(String) res = false @@ -335,7 +343,84 @@ res = value.rh_key_to_symbol?(levels - 1) end return true if res end false + end + + # return an exact clone of the recursive Array and Hash contents. + # + # * *Args* : + # + # * *Returns* : + # - Recursive Array/Hash cloned. Other kind of objects are kept referenced. + # * *Raises* : + # Nothing + # + # examples: + # hdata = { :test => { :test2 => { :test5 => :test, + # 'text' => 'blabla' }, + # 'test5' => 'test' }, + # :array => [{ :test => :value1 }, 2, { :test => :value3 }]} + # + # hclone = hdata.rh_clone + # hclone[:test] = "test" + # hdata[:test] == { :test2 => { :test5 => :test,'text' => 'blabla' } + # # => true + # hclone[:array].pop + # hdata[:array].length != hclone[:array].length + # # => true + # hclone[:array][0][:test] = "value2" + # hdata[:array][0][:test] != hclone[:array][0][:test] + # # => true + def rh_clone + result = {} + each do |key, value| + if [Array, Hash].include?(value.class) + result[key] = value.rh_clone + else + result[key] = value + end + end + result + end +end + +# Defines rh_clone for Array +class Array + # return an exact clone of the recursive Array and Hash contents. + # + # * *Args* : + # + # * *Returns* : + # - Recursive Array/Hash cloned. + # * *Raises* : + # Nothing + # + # examples: + # hdata = { :test => { :test2 => { :test5 => :test, + # 'text' => 'blabla' }, + # 'test5' => 'test' }, + # :array => [{ :test => :value1 }, 2, { :test => :value3 }]} + # + # hclone = hdata.rh_clone + # hclone[:test] = "test" + # hdata[:test] == { :test2 => { :test5 => :test,'text' => 'blabla' } + # # => true + # hclone[:array].pop + # hdata[:array].length != hclone[:array].length + # # => true + # hclone[:array][0][:test] = "value2" + # hdata[:array][0][:test] != hclone[:array][0][:test] + # # => true + def rh_clone + result = [] + each do |value| + begin + result << value.rh_clone + rescue + result << value + end + end + result end end