Sha256: 8b13698a51064fb8199d9f4bbba05dfc43b0079105a75454353ccc49ed24f91e

Contents?: true

Size: 1.12 KB

Versions: 5

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

require 'dry/initializer'

require 'dry/schema/constants'
require 'dry/schema/config'
require 'dry/schema/result'
require 'dry/schema/messages'
require 'dry/schema/message_compiler'

module Dry
  module Schema
    # Applies rules defined within the DSL
    #
    # @api private
    class RuleApplier
      extend Dry::Initializer

      # @api private
      param :rules

      # @api private
      option :config, default: -> { Config.new }

      # @api private
      option :message_compiler, default: -> { MessageCompiler.new(Messages.setup(config.messages)) }

      # @api private
      def call(input)
        results = EMPTY_ARRAY.dup

        rules.each do |name, rule|
          next if input.error?(name)

          result = rule.(input)
          results << result if result.failure?
        end

        input.concat(results)
      end

      # @api private
      def to_ast
        if config.messages.namespace
          [:namespace, [config.messages.namespace, [:set, rules.values.map(&:to_ast)]]]
        else
          [:set, rules.values.map(&:to_ast)]
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dry-schema-1.0.0 lib/dry/schema/rule_applier.rb
dry-schema-0.6.0 lib/dry/schema/rule_applier.rb
dry-schema-0.5.1 lib/dry/schema/rule_applier.rb
dry-schema-0.5.0 lib/dry/schema/rule_applier.rb
dry-schema-0.4.0 lib/dry/schema/rule_applier.rb