Sha256: aef19951a80996ab61d9ea6569c36edbad7030025b5e57ed7bfa30d2eb1a9f5b

Contents?: true

Size: 936 Bytes

Versions: 9

Compression:

Stored size: 936 Bytes

Contents

# frozen_string_literal: true

module Dry
  module Schema
    module Macros
      # Macro used to specify predicates for a value that can be `nil`
      #
      # @api private
      class Maybe < DSL
        # @api private
        def call(*args, **opts, &block)
          if args.include?(:empty?)
            raise ::Dry::Schema::InvalidSchemaError, "Using maybe with empty? predicate is invalid"
          end

          if args.include?(:nil?)
            raise ::Dry::Schema::InvalidSchemaError, "Using maybe with nil? predicate is redundant"
          end

          append_macro(Macros::Value) do |macro|
            macro.call(*args, **opts, &block)
          end

          self
        end

        # @api private
        def to_ast
          [:implication,
           [
             [:not, [:predicate, [:nil?, [[:input, Undefined]]]]],
             trace.to_rule.to_ast
           ]]
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
dry-schema-1.13.3 lib/dry/schema/macros/maybe.rb
dry-schema-1.13.2 lib/dry/schema/macros/maybe.rb
dry-schema-1.13.1 lib/dry/schema/macros/maybe.rb
dry-schema-1.13.0 lib/dry/schema/macros/maybe.rb
dry-schema-1.12.0 lib/dry/schema/macros/maybe.rb
dry-schema-1.11.3 lib/dry/schema/macros/maybe.rb
dry-schema-1.11.2 lib/dry/schema/macros/maybe.rb
dry-schema-1.11.1 lib/dry/schema/macros/maybe.rb
dry-schema-1.11.0 lib/dry/schema/macros/maybe.rb