File: meta.rb

package info (click to toggle)
ruby-dry-types 1.2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 504 kB
  • sloc: ruby: 3,059; makefile: 4
file content (51 lines) | stat: -rw-r--r-- 1,033 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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