Module: AngryHash::Conversion::Duplicating::ClassMethods

Defined in:
lib/angry_hash/conversion/duplicating.rb

Instance Method Summary collapse

Instance Method Details

#__convert(hash, cycle_watch = []) ⇒ Object

duplicating convert



12
13
14
15
16
17
18
19
# File 'lib/angry_hash/conversion/duplicating.rb', line 12

def __convert(hash,cycle_watch=[])
  new_hash = hash.inject(AngryHash.new) do |hash,(k,v)|
    hash.regular_writer( __convert_key(k), __convert_value(v,cycle_watch) )
    hash
  end

  new_hash
end

#__convert_value(v, cycle_watch = []) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/angry_hash/conversion/duplicating.rb', line 21

def __convert_value(v,cycle_watch=[])
  id = v.__id__

  return if cycle_watch.include? id

  begin
    cycle_watch << id

    original_v = v
    v = v.to_hash if v.respond_to?(:to_hash)

    case v
    when Hash
      __convert(v,cycle_watch)
    when Array
      v.map {|vv| __convert_value(vv,cycle_watch)}
    when Fixnum,Symbol,NilClass,TrueClass,FalseClass,Float,Bignum,BigDecimal
      v
    else
      v.dup
    end
  ensure
    cycle_watch.pop
  end
end