Sha256: d28c45636b38591e40f2b7f3af69ba12758158d8d3228fdc15ce53fd1562c060

Contents?: true

Size: 1.01 KB

Versions: 5

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 [Hash] new_options
      #
      # @return [Type]
      #
      # @api public
      def with(options)
        super(meta: @meta, **options)
      end

      # @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 = nil)
        if !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
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dry-types-1.2.0 lib/dry/types/meta.rb
dry-types-1.1.1 lib/dry/types/meta.rb
dry-types-1.1.0 lib/dry/types/meta.rb
dry-types-1.0.1 lib/dry/types/meta.rb
dry-types-1.0.0 lib/dry/types/meta.rb