Sha256: 8724b39905890470fd073d1dfe85749d6b41d3c645e3df58b9f7b9dca1a18993

Contents?: true

Size: 695 Bytes

Versions: 5

Compression:

Stored size: 695 Bytes

Contents

# frozen_string_literal: true

module Darthjee
  module CoreExt
    module 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
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
darthjee-core_ext-1.7.4 lib/darthjee/core_ext/hash/keys_sorter.rb
darthjee-core_ext-1.7.3 lib/darthjee/core_ext/hash/keys_sorter.rb
darthjee-core_ext-1.7.2 lib/darthjee/core_ext/hash/keys_sorter.rb
darthjee-core_ext-1.7.1 lib/darthjee/core_ext/hash/keys_sorter.rb
darthjee-core_ext-1.7.0 lib/darthjee/core_ext/hash/keys_sorter.rb