Class: Dry::Types::Module
- Inherits:
-
Module
- Object
- Module
- Dry::Types::Module
- Defined in:
- lib/dry/types/module.rb
Overview
Export types registered in a container as module constants.
Instance Method Summary collapse
-
#initialize(registry, *args, **kwargs) ⇒ Module
constructor
A new instance of Module.
- #registry_tree ⇒ Object private
- #type_constants(*namespaces, default: Undefined, **aliases) ⇒ Object private
Constructor Details
#initialize(registry, *args, **kwargs) ⇒ Module
Returns a new instance of Module.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/dry/types/module.rb', line 21 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.send(: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_tree ⇒ 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.
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/dry/types/module.rb', line 69 def registry_tree @registry_tree ||= @registry.keys.each_with_object({}) { |key, tree| type = @registry[key] *modules, const_name = key.split('.').map { |part| 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.
41 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 |
# File 'lib/dry/types/module.rb', line 41 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 = Inflector.camelize(default).to_sym end tree = registry_tree if namespaces.empty? && aliases.empty? modules = tree.select { |_, v| v.is_a?(::Hash) }.map(&:first) else modules = (namespaces + aliases.keys).map { |n| 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 |