dry-rb/dry-types

View on GitHub
lib/dry/types/meta.rb

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
# 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