Sha256: 989ad26133bed861ba6d1ce3200a3623ecb75b59f93ec5c173871715358a5bcd

Contents?: true

Size: 1.21 KB

Versions: 19

Compression:

Stored size: 1.21 KB

Contents

module Rollbar
  module Util
    module Hash # :nodoc:
      def self.deep_stringify_keys(hash, seen = {})
        return if seen[hash.object_id]

        seen[hash.object_id] = 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

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

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

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
rollbar-3.2.0 lib/rollbar/util/hash.rb
rollbar-3.1.2 lib/rollbar/util/hash.rb
rollbar-3.1.1 lib/rollbar/util/hash.rb
rollbar-3.1.0 lib/rollbar/util/hash.rb
rollbar-3.0.1 lib/rollbar/util/hash.rb
rollbar-3.0.0 lib/rollbar/util/hash.rb
rollbar-2.27.1 lib/rollbar/util/hash.rb
rollbar-2.27.0 lib/rollbar/util/hash.rb
rollbar-2.26.1 lib/rollbar/util/hash.rb
rollbar-2.26.0 lib/rollbar/util/hash.rb
rollbar-2.25.1 lib/rollbar/util/hash.rb
rollbar-2.25.0 lib/rollbar/util/hash.rb
rollbar-2.24.0 lib/rollbar/util/hash.rb
rollbar-2.23.2 lib/rollbar/util/hash.rb
rollbar-2.23.1 lib/rollbar/util/hash.rb
rollbar-2.23.0 lib/rollbar/util/hash.rb
rollbar-2.22.1 lib/rollbar/util/hash.rb
rollbar-2.22.0 lib/rollbar/util/hash.rb
rollbar-2.21.0 lib/rollbar/util/hash.rb