Class: Dry::Types::Module

Inherits:
Module
  • Object
show all
Defined in:
lib/dry/types/module.rb

Overview

Export types registered in a container as module constants.

Examples:

module Types
  include Dry.Types(:strict, :coercible, :nominal, default: :strict)
end

Types.constants
# => [:Class, :Strict, :Symbol, :Integer, :Float, :String, :Array, :Hash,
#     :Decimal, :Nil, :True, :False, :Bool, :Date, :Nominal, :DateTime, :Range,
#     :Coercible, :Time]

API:

  • public

Instance Method Summary collapse

Constructor Details

#initialize(registry, *args, **kwargs) ⇒ Module

Returns a new instance of Module.

API:

  • public



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dry/types/module.rb', line 20

def initialize(registry, *args, **kwargs)
  @registry = registry
  check_parameters(*args, **kwargs)
  constants = type_constants(*args, **kwargs)
  define_constants(constants)
  extend(BuilderMethods)

  if constants.key?(:Nominal)
    singleton_class.define_method(:included) do |base|
      super(base)
      base.instance_exec(const_get(:Nominal, false)) do |nominal|
        extend Dry::Core::Deprecations[:"dry-types"]
        const_set(:Definition, nominal)
        deprecate_constant(:Definition, message: "Nominal")
      end
    end
  end
end

Instance Method Details

#registry_treeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/dry/types/module.rb', line 74

def registry_tree
  @registry_tree ||= @registry.keys.each_with_object({}) { |key, tree|
    type = @registry[key]
    *modules, const_name = key.split(".").map { |part|
      Types::Inflector.camelize(part).to_sym
    }
    next if modules.empty?

    modules.reduce(tree) { |br, name| br[name] ||= {} }[const_name] = type
  }.freeze
end

#type_constants(*namespaces, default: Undefined, **aliases) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/PerceivedComplexity

API:

  • private



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dry/types/module.rb', line 42

def type_constants(*namespaces, default: Undefined, **aliases)
  if namespaces.empty? && aliases.empty? && Undefined.equal?(default)
    default_ns = :Strict
  elsif Undefined.equal?(default)
    default_ns = Undefined
  else
    default_ns = Types::Inflector.camelize(default).to_sym
  end

  tree = registry_tree

  if namespaces.empty? && aliases.empty?
    modules = tree.select { _2.is_a?(::Hash) }.map(&:first)
  else
    modules = (namespaces + aliases.keys).map { |n|
      Types::Inflector.camelize(n).to_sym
    }
  end

  tree.each_with_object({}) do |(key, value), constants|
    if modules.include?(key)
      name = aliases.fetch(Inflector.underscore(key).to_sym, key)
      constants[name] = value
    end

    constants.update(value) if key == default_ns
  end
end