module Dry::Types
Main library namespace
@api public
Helper methods for constraint types
@api public
Constants
- ALL_PRIMITIVES
All built-in primitives
- Any
- COERCIBLE
All coercible types
- Definition
Nominal
types define a primitive class and do not apply any constructors or constraintsUse these types for annotations and the base for building more complex types on top of them.
@api public
- Inflector
- KERNEL_COERCIBLE
Primitives with {Kernel} coercion methods
- METHOD_COERCIBLE
Primitives with coercions through by convention `to_*` methods
- METHOD_COERCIBLE_METHODS
By convention methods to coerce {METHOD_COERCIBLE} primitives
- MapError
- NON_COERCIBLE
Primitives that are non-coercible
- NON_NIL
All built-in primitives except {NilClass}
- PRINTER
- Safe
Lax
types rescue from type-related errors when constructors fail@api public
- SchemaKeyError
- TYPE_SPEC_REGEX
- VERSION
Public Class Methods
@param [Hash] options
@return [Dry::Logic::Rule]
@api public
# File lib/dry/types/constraints.rb, line 17 def self.Rule(options) rule_compiler.( options.map { |key, val| Logic::Rule::Predicate.build( Logic::Predicates[:"#{key}?"] ).curry(val).to_ast } ).reduce(:and) end
Get a built-in type by its name
@param [String,Class] name
@return [Type,Class]
@api public
# File lib/dry/types.rb, line 88 def self.[](name) type_map.fetch_or_store(name) do case name when ::String result = name.match(TYPE_SPEC_REGEX) if result type_id, member_id = result[1..2] container[type_id].of(self[member_id]) else container[name] end when ::Class warn(<<~DEPRECATION) Using Dry::Types.[] with a class is deprecated, please use string identifiers: Dry::Types[Integer] -> Dry::Types['integer']. If you're using dry-struct this means changing `attribute :counter, Integer` to `attribute :counter, Dry::Types['integer']` or to `attribute :counter, 'integer'`. DEPRECATION type_name = identifier(name) if container.key?(type_name) self[type_name] else name end end end end
@api private
# File lib/dry/types.rb, line 136 def self.const_missing(const) underscored = Inflector.underscore(const) if container.keys.any? { |key| key.split(".")[0] == underscored } raise ::NameError, "dry-types does not define constants for default types. "\ 'You can access the predefined types with [], e.g. Dry::Types["integer"] '\ "or generate a module with types using Dry.Types()" else super end end
Return container with registered built-in type objects
@return [Container{String => Nominal}]
@api private
# File lib/dry/types.rb, line 55 def self.container @container ||= Container.new end
Add a new type builder method. This is a public API for defining custom type constructors
@example simple custom type constructor
Dry::Types.define_builder(:or_nil) do |type| type.optional.fallback(nil) end Dry::Types["integer"].or_nil.("foo") # => nil
@example fallback alias
Dry::Types.define_builder(:or) do |type, fallback| type.fallback(fallback) end Dry::Types["integer"].or(100).("foo") # => 100
@param [Symbol] method @param [#call] block
@api public
# File lib/dry/types.rb, line 170 def self.define_builder(method, &block) Builder.define_method(method) do |*args| block.(self, *args) end end
Infer a type identifier from the provided class
@param [#to_s] klass
@return [String]
# File lib/dry/types.rb, line 122 def self.identifier(klass) Inflector.underscore(klass).tr("/", ".") end
@api private
# File lib/dry/types.rb, line 46 def self.included(*) raise "Import Dry.Types, not Dry::Types" end
@see Dry
.Types
# File lib/dry/types.rb, line 38 def self.module(*namespaces, default: :nominal, **aliases) ::Module.new(container, *namespaces, default: default, **aliases) end
Register a new built-in type
@param [String] name @param [Type] type @param [#call,nil] block
@return [Container{String => Nominal}]
@api private
# File lib/dry/types.rb, line 77 def self.register(name, type = nil, &block) container.register(name, type || block.call) end
Check if a give type is registered
@return [Boolean]
@api private
# File lib/dry/types.rb, line 64 def self.registered?(class_or_identifier) container.key?(identifier(class_or_identifier)) end
@return [Dry::Logic::RuleCompiler]
@api private
# File lib/dry/types/constraints.rb, line 30 def self.rule_compiler @rule_compiler ||= Logic::RuleCompiler.new(Logic::Predicates) end
Cached type map
@return [Concurrent::Map]
@api private
# File lib/dry/types.rb, line 131 def self.type_map @type_map ||= ::Concurrent::Map.new end