Sha256: 5ece79e6a4801b8b71744cb1f8ab4a9f25b590ca56d3b977656d80c3b0331c08

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

require 'dry/schema/primitive_inferrer'
require 'dry/schema/macros/value'

module Dry
  module Schema
    module Macros
      # Macro used to prepend `:filled?` predicate
      #
      # @api private
      class Filled < Value
        # @!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 { PrimitiveInferrer.new }

        # @api private
        def call(*predicates, **opts, &block)
          ensure_valid_predicates(predicates)

          if opts[:type_spec]
            if filter?
              value(*predicates, **opts, &block)
            else
              value(predicates[0], :filled?, *predicates[1..predicates.size - 1], **opts, &block)
            end
          else
            value(:filled?, *predicates, **opts, &block)
          end
        end

        # @api private
        # rubocop:disable Style/GuardClause
        def ensure_valid_predicates(predicates)
          if predicates.include?(:empty?)
            raise ::Dry::Schema::InvalidSchemaError, 'Using filled with empty? predicate is invalid'
          end

          if predicates.include?(:filled?)
            raise ::Dry::Schema::InvalidSchemaError, 'Using filled with filled? is redundant'
          end
        end
        # rubocop:enable Style/GuardClause

        # @api private
        def filter?
          !primitives.include?(NilClass) && processor_config.filter_empty_string
        end

        # @api private
        def processor_config
          schema_dsl.processor_type.config
        end

        # @api private
        def primitives
          primitive_inferrer[schema_type]
        end

        # @api private
        def schema_type
          schema_dsl.types[name]
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry-schema-1.1.0 lib/dry/schema/macros/filled.rb