Module: Rollbar::Util::Hash

Defined in:
lib/rollbar/util/hash.rb

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.deep_stringify_keys(hash, seen = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/rollbar/util/hash.rb', line 4

def self.deep_stringify_keys(hash, seen = {})
  seen.compare_by_identity
  return if seen[hash]

  seen[hash] = true
  replace_seen_children(hash, seen)

  hash.reduce({}) do |h, (key, value)|
    h[key.to_s] = map_value(value, :deep_stringify_keys, seen)

    h
  end
end

.map_value(thing, meth, seen) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rollbar/util/hash.rb', line 18

def self.map_value(thing, meth, seen)
  case thing
  when ::Hash
    send(meth, thing, seen)
  when Array
    if seen[thing]
      thing
    else
      seen[thing] = true
      replace_seen_children(thing, seen)
      thing.map { |v| map_value(v, meth, seen) }
    end
  else
    thing
  end
end

.replace_seen_children(thing, seen) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rollbar/util/hash.rb', line 35

def self.replace_seen_children(thing, seen)
  case thing
  when ::Hash
    thing.keys.each do |key| # rubocop:disable Style/HashEachMethods
      if seen[thing[key]]
        thing[key] =
          "removed circular reference: #{thing[key]}"
      end
    end
  when Array
    thing.each_with_index do |_, i|
      if seen[thing[i]]
        thing[i] =
          "removed circular reference: #{thing[i]}"
      end
    end
  end
end