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.37.0 lib/tins/hash_symbolize_keys_recursive.rb
tins-1.36.1 lib/tins/hash_symbolize_keys_recursive.rb
tins-1.36.0 lib/tins/hash_symbolize_keys_recursive.rb
tins-1.35.0 lib/tins/hash_symbolize_keys_recursive.rb
tins-1.34.0 lib/tins/hash_symbolize_keys_recursive.rb
tins-1.33.0 lib/tins/hash_symbolize_keys_recursive.rb
tdiary-5.2.4 vendor/bundle/ruby/3.1.0/gems/tins-1.31.1/lib/tins/hash_symbolize_keys_recursive.rb
tins-1.32.1 lib/tins/hash_symbolize_keys_recursive.rb
tins-1.32.0 lib/tins/hash_symbolize_keys_recursive.rb
tdiary-5.2.3 vendor/bundle/ruby/3.1.0/gems/tins-1.31.1/lib/tins/hash_symbolize_keys_recursive.rb
tdiary-5.2.2 vendor/bundle/ruby/3.1.0/gems/tins-1.31.1/lib/tins/hash_symbolize_keys_recursive.rb
tins-1.31.1 lib/tins/hash_symbolize_keys_recursive.rb
tdiary-5.2.1 vendor/bundle/ruby/3.1.0/gems/tins-1.31.0/lib/tins/hash_symbolize_keys_recursive.rb
tins-1.31.0 lib/tins/hash_symbolize_keys_recursive.rb
tins-1.30.0 lib/tins/hash_symbolize_keys_recursive.rb
tdiary-5.2.0 vendor/bundle/ruby/2.7.0/gems/tins-1.29.1/lib/tins/hash_symbolize_keys_recursive.rb
tdiary-5.2.0 vendor/bundle/ruby/3.0.0/gems/tins-1.29.1/lib/tins/hash_symbolize_keys_recursive.rb
tdiary-5.1.7 vendor/bundle/ruby/2.7.0/gems/tins-1.29.1/lib/tins/hash_symbolize_keys_recursive.rb
tdiary-5.1.7 vendor/bundle/ruby/3.0.0/gems/tins-1.29.1/lib/tins/hash_symbolize_keys_recursive.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tins-1.29.1/lib/tins/hash_symbolize_keys_recursive.rb