lib/dry/schema/macros/dsl.rb in dry-schema-1.4.3 vs lib/dry/schema/macros/dsl.rb in dry-schema-1.5.0

- old
+ new

@@ -1,12 +1,12 @@ # frozen_string_literal: true -require 'dry/logic/operators' -require 'dry/types/predicate_inferrer' -require 'dry/types/primitive_inferrer' +require "dry/logic/operators" -require 'dry/schema/macros/core' +require "dry/schema/macros/core" +require "dry/schema/predicate_inferrer" +require "dry/schema/primitive_inferrer" module Dry module Schema module Macros # Macro specialization used within the DSL @@ -26,17 +26,17 @@ # @!attribute [r] predicate_inferrer # PredicateInferrer is used to infer predicate type-check from a type spec # @return [PredicateInferrer] # @api private - option :predicate_inferrer, default: proc { ::Dry::Types::PredicateInferrer.new(compiler.predicates) } + option :predicate_inferrer, default: proc { PredicateInferrer.new(compiler.predicates) } # @!attribute [r] primitive_inferrer # PrimitiveInferrer used to get a list of primitive classes from configured type # @return [PrimitiveInferrer] # @api private - option :primitive_inferrer, default: proc { ::Dry::Types::PrimitiveInferrer.new } + option :primitive_inferrer, default: proc { PrimitiveInferrer.new } # @overload value(*predicates, **predicate_opts) # Set predicates without and with arguments # # @param [Array<Symbol>] predicates @@ -175,10 +175,15 @@ def type(spec) schema_dsl.set_type(name, spec) self end + # @api private + def custom_type? + schema_dsl.custom_type?(name) + end + private # @api private def append_macro(macro_type) macro = macro_type.new(schema_dsl: schema_dsl, name: name) @@ -193,33 +198,36 @@ end end # @api private def extract_type_spec(*args, nullable: false, set_type: true) - type_spec = args[0] + type_spec = args[0] unless schema_or_predicate?(args[0]) - is_type_spec = type_spec.is_a?(Dry::Schema::Processor) || - type_spec.is_a?(Symbol) && - type_spec.to_s.end_with?(QUESTION_MARK) - - type_spec = nil if is_type_spec - predicates = Array(type_spec ? args[1..-1] : args) + type_rule = nil if type_spec resolved_type = resolve_type(type_spec, nullable) - type(resolved_type) if set_type + if type_spec.is_a?(::Array) + type_rule = type_spec.map { |ts| new(chain: false).value(ts) }.reduce(:|) + else + type_predicates = predicate_inferrer[resolved_type] - type_predicates = predicate_inferrer[resolved_type] + predicates.replace(type_predicates + predicates) unless type_predicates.empty? - predicates.replace(type_predicates + predicates) unless type_predicates.empty? - - return self if predicates.empty? + return self if predicates.empty? + end end - yield(*predicates, type_spec: type_spec) + type(resolved_type) if set_type && resolved_type + + if type_rule + yield(*predicates, type_spec: nil, type_rule: type_rule) + else + yield(*predicates, type_spec: type_spec, type_rule: nil) + end end # @api private def resolve_type(type_spec, nullable) resolved = schema_dsl.resolve_type(type_spec) @@ -227,9 +235,16 @@ if type_spec.is_a?(::Array) || !nullable || resolved.optional? resolved else schema_dsl.resolve_type([:nil, resolved]) end + end + + # @api private + def schema_or_predicate?(arg) + arg.is_a?(Dry::Schema::Processor) || + arg.is_a?(Symbol) && + arg.to_s.end_with?(QUESTION_MARK) end end end end end