Sha256: c9fda213756c9aa4cfe7a046cbd131c0e3498112bc9f7334ba065c00b1e86d46

Contents?: true

Size: 680 Bytes

Versions: 1

Compression:

Stored size: 680 Bytes

Contents

# frozen_string_literal: true

require 'deep_merge/rails_compat'
require 'dry/struct'

require 'modern/types'

module Modern
  class Struct < Dry::Struct
    module Copy
      # This implementation is necessary because the "fast" way (hash, merge, recreate)
      # WILL EAT YOUR TYPE DATA. This is the only way I can find to copy-but-change an
      # object that doesn't.
      #
      # Computers are bad.
      def copy(fields = {})
        self.class[self.class.attribute_names.map { |n| [n, self[n]] }.to_h.merge(fields)]
      end
    end

    transform_types do |type|
      type.constructor { |value| value.nil? ? Undefined : value  }
    end

    include Copy
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
modern-0.5.0 lib/modern/struct.rb