Sha256: 13360f3ab9d611340a47f671b3b7549af64792dce3fbbe8a2fa20fe3f8d5e43c

Contents?: true

Size: 597 Bytes

Versions: 2

Compression:

Stored size: 597 Bytes

Contents

# frozen_string_literal: true

module Dry
  class Struct
    # Helper for {Struct#to_hash} implementation
    module Hashify
      # Converts value to hash recursively
      # @param [#to_hash, #map, Object] value
      # @return [Hash, Array]
      def self.[](value)
        if value.is_a?(Struct)
          value.to_h.transform_values { self[_1] }
        elsif value.respond_to?(:to_hash)
          value.to_hash.transform_values { self[_1] }
        elsif value.respond_to?(:to_ary)
          value.to_ary.map { self[_1] }
        else
          value
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dry-struct-1.7.1 lib/dry/struct/hashify.rb
dry-struct-1.7.0 lib/dry/struct/hashify.rb