Sha256: 723c78b4dcbe20c9083eb27a3539db08c7fa49404ad0a53f4fc3247e70a22afa

Contents?: true

Size: 1.61 KB

Versions: 9

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

module Dry
  module Schema
    module Macros
      # Macro used to prepend `:filled?` predicate
      #
      # @api private
      class Filled < Value
        # @api private
        def call(*predicates, **opts, &block)
          ensure_valid_predicates(predicates)

          append_macro(Macros::Value) do |macro|
            if opts[:type_spec] && !filter_empty_string?
              macro.call(predicates[0], :filled?, *predicates[1..predicates.size - 1], **opts,
                         &block)
            elsif opts[:type_rule]
              macro.call(:filled?).value(*predicates, **opts, &block)
            else
              macro.call(:filled?, *predicates, **opts, &block)
            end
          end
        end

        # @api private
        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

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

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

        # @api private
        def expected_primitives
          primitive_inferrer[schema_type]
        end

        # @api private
        def schema_type
          schema_dsl.types[name]
        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/filled.rb
dry-schema-1.13.2 lib/dry/schema/macros/filled.rb
dry-schema-1.13.1 lib/dry/schema/macros/filled.rb
dry-schema-1.13.0 lib/dry/schema/macros/filled.rb
dry-schema-1.12.0 lib/dry/schema/macros/filled.rb
dry-schema-1.11.3 lib/dry/schema/macros/filled.rb
dry-schema-1.11.2 lib/dry/schema/macros/filled.rb
dry-schema-1.11.1 lib/dry/schema/macros/filled.rb
dry-schema-1.11.0 lib/dry/schema/macros/filled.rb