Sha256: e1c6173e801b1d9b02488d07a3af6bedffe979535cf909b32e53356313f33ff2

Contents?: true

Size: 555 Bytes

Versions: 3

Compression:

Stored size: 555 Bytes

Contents

# frozen_string_literal: true

class Hash
  class KeysSorter
    def initialize(hash, recursive: true)
      @hash = hash
      @recursive = recursive
    end

    def sort
      {}.tap do |new_hash|
        sorted_keys.each do |key|
          new_hash[key] = change_value(hash[key])
        end
      end
    end

    private

    def sorted_keys
      hash.keys.sort
    end

    def change_value(value)
      return value unless value.is_a?(Hash) && recursive
      value.sort_keys(recursive: true)
    end

    attr_reader :hash, :recursive
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
darthjee-core_ext-1.6.2 lib/darthjee/core_ext/hash/keys_sorter.rb
darthjee-core_ext-1.6.1 lib/darthjee/core_ext/hash/keys_sorter.rb
darthjee-core_ext-1.6.0 lib/darthjee/core_ext/hash/keys_sorter.rb