module Dry::Types
def self.Rule(options)
-
(Dry::Logic::Rule)
-
Parameters:
-
options
(Hash
) --
def self.Rule(options) rule_compiler.( options.map { |key, val| Logic::Rule::Predicate.new(Logic::Predicates[:"#{key}?"]).curry(val).to_ast } ).reduce(:and) end
def self.[](name)
-
(Type, Class)
-
Parameters:
-
name
(String, Class
) --
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].member(self[member_id]) else container[name] end when Class type_name = identifier(name) if container.key?(type_name) self[type_name] else name end end end end
def self.container
-
(Container{String => Definition})
-
def self.container @container ||= Container.new end
def self.define_constants(namespace, identifiers)
-
(
-)
Parameters:
-
identifiers
(
) -- -
namespace
(Module
) --
def self.define_constants(namespace, identifiers) names = identifiers.map do |id| parts = id.split('.') [Inflecto.camelize(parts.pop), parts.map(&Inflecto.method(:camelize))] end names.map do |(klass, parts)| mod = parts.reduce(namespace) do |a, e| a.constants.include?(e.to_sym) ? a.const_get(e) : a.const_set(e, Module.new) end mod.const_set(klass, self[identifier((parts + [klass]).join('::'))]) end end
def self.finalize
- Include {Dry::Types.module} instead
def self.finalize warn 'Dry::Types.finalize and configuring namespace is deprecated. Just'\ ' do `include Dry::Types.module` in places where you want to have access'\ ' to built-in types' define_constants(config.namespace, type_keys) end
def self.identifier(klass)
-
(String)
-
Parameters:
-
klass
(#to_s
) --
def self.identifier(klass) Inflecto.underscore(klass).tr('/', '.') end
def self.module
-
(Module)
-
def self.module namespace = Module.new define_constants(namespace, type_keys) namespace end
def self.register(name, type = nil, &block)
-
(Container{String => Definition})
-
Parameters:
-
block
(#call, nil
) -- -
type
(Type
) -- -
name
(String
) --
def self.register(name, type = nil, &block) container.register(name, type || block.call) end
def self.register_class(klass, meth = :new)
-
(Container{String => Definition})
-
Parameters:
-
meth
(Symbol
) -- -
klass
(Class
) --
def self.register_class(klass, meth = :new) type = Definition.new(klass).constructor(klass.method(meth)) container.register(identifier(klass), type) end
def self.rule_compiler
-
(Dry::Logic::RuleCompiler)
-
def self.rule_compiler @rule_compiler ||= Logic::RuleCompiler.new(Logic::Predicates) end
def self.type_keys
-
(
-)
def self.type_keys container._container.keys end
def self.type_map
-
(Concurrent::Map)
-
def self.type_map @type_map ||= Concurrent::Map.new end