Sha256: fd4b4fecb9a07ca7ed5b45baa338dc29d9c785e85d5a0c8790ec29f54d4ff1d7

Contents?: true

Size: 1.01 KB

Versions: 3

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

module Dry
  module Types
    # Storage for meta-data
    #
    # @api public
    module Meta
      def initialize(*args, meta: EMPTY_HASH, **options)
        super(*args, **options)
        @meta = meta.freeze
      end

      # @param options [Hash] new_options
      #
      # @return [Type]
      #
      # @api public
      def with(**options) = super(meta: @meta, **options)

      # @overload meta
      #   @return [Hash] metadata associated with type
      #
      # @overload meta(data)
      #   @param [Hash] new metadata to merge into existing metadata
      #   @return [Type] new type with added metadata
      #
      # @api public
      def meta(data = Undefined)
        if Undefined.equal?(data)
          @meta
        elsif data.empty?
          self
        else
          with(meta: @meta.merge(data))
        end
      end

      # Resets meta
      #
      # @return [Dry::Types::Type]
      #
      # @api public
      def pristine = with(meta: EMPTY_HASH)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dry-types-1.8.2 lib/dry/types/meta.rb
dry-types-1.8.1 lib/dry/types/meta.rb
dry-types-1.8.0 lib/dry/types/meta.rb