Sha256: 928de1a12b93e32c49d8203a28f1dcf976b7b978f4fe523a906720eb9ac5d4e8

Contents?: true

Size: 637 Bytes

Versions: 3

Compression:

Stored size: 637 Bytes

Contents

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.respond_to?(:to_hash)
          if RUBY_VERSION >= '2.4'
            value.to_hash.transform_values { |v| self[v] }
          else
            value.to_hash.each_with_object({}) { |(k, v), h| h[k] = self[v] }
          end
        elsif value.respond_to?(:to_ary)
          value.to_ary.map { |item| self[item] }
        else
          value
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dry-struct-1.0.0 lib/dry/struct/hashify.rb
dry-struct-0.7.0 lib/dry/struct/hashify.rb
dry-struct-0.6.0 lib/dry/struct/hashify.rb