File: fn_container.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 (39 lines) | stat: -rw-r--r-- 897 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
# frozen_string_literal: true

require 'dry/types/container'

module Dry
  module Types
    # Internal container for constructor functions used by the built-in types
    #
    # @api private
    class FnContainer
      # @api private
      def self.container
        @container ||= Container.new
      end

      # @api private
      def self.register(function = Dry::Core::Constants::Undefined, &block)
        fn = Dry::Core::Constants::Undefined.default(function, block)
        fn_name = register_name(fn)
        container.register(fn_name, fn) unless container.key?(fn_name)
        fn_name
      end

      # @api private
      def self.[](fn_name)
        if container.key?(fn_name)
          container[fn_name]
        else
          fn_name
        end
      end

      # @api private
      def self.register_name(function)
        "fn_#{function.object_id}"
      end
    end
  end
end