Sha256: 14aa136c3d41e082012f4566ffcc537397c08f73a8f150314cbabca68dded010

Contents?: true

Size: 1.19 KB

Versions: 45

Compression:

Stored size: 1.19 KB

Contents

require 'tins/thread_local'

module Tins
  module HashSymbolizeKeysRecursive
    extend Tins::ThreadLocal

    thread_local :seen

    def symbolize_keys_recursive(circular: nil)
      self.seen = {}
      _symbolize_keys_recursive(self, circular: circular)
    ensure
      self.seen = nil
    end

    def symbolize_keys_recursive!(circular: nil)
      replace symbolize_keys_recursive(circular: circular)
    end

    private

    def _symbolize_keys_recursive(object, circular: nil)
      case
      when seen[object.__id__]
        object = circular
      when Hash === object
        seen[object.__id__] = true
        new_object = object.class.new
        seen[new_object.__id__] = true
        object.each do |k, v|
          new_object[k.to_s.to_sym] = _symbolize_keys_recursive(v, circular: circular)
        end
        object = new_object
      when Array === object
        seen[object.__id__] = true
        new_object = object.class.new(object.size)
        seen[new_object.__id__] = true
        object.each_with_index do |v, i|
          new_object[i] = _symbolize_keys_recursive(v, circular: circular)
        end
        object = new_object
      end
      object
    end
  end
end

require 'tins/alias'

Version data entries

45 entries across 35 versions & 2 rubygems

Version Path
tins-1.24.1 lib/tins/hash_symbolize_keys_recursive.rb
tins-1.24.0 lib/tins/hash_symbolize_keys_recursive.rb
tins-1.23.0 lib/tins/hash_symbolize_keys_recursive.rb
tdiary-5.1.0 vendor/bundle/gems/tins-1.22.2/lib/tins/hash_symbolize_keys_recursive.rb
tins-1.22.2 lib/tins/hash_symbolize_keys_recursive.rb