Sha256: c31979adb736b7071d39a9357a91eee34fec95bacf53febf6e8e509886be6caa

Contents?: true

Size: 636 Bytes

Versions: 6

Compression:

Stored size: 636 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 { |current| self[current] }
        elsif value.respond_to?(:to_hash)
          value.to_hash.transform_values { |current| self[current] }
        elsif value.respond_to?(:to_ary)
          value.to_ary.map { |item| self[item] }
        else
          value
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
dry-struct-1.6.0 lib/dry/struct/hashify.rb
dry-struct-1.5.2 lib/dry/struct/hashify.rb
dry-struct-1.5.1 lib/dry/struct/hashify.rb
dry-struct-1.5.0 lib/dry/struct/hashify.rb
dry-struct-1.4.0 lib/dry/struct/hashify.rb
dry-struct-1.3.0 lib/dry/struct/hashify.rb