Sha256: 5334dac7f09cdcc0f4ab5829d3e6558977fbe6bce17b160b7a7257a3d82d221e

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

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: proc { Config.new }

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

      # @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.namespace
          [:namespace, [config.namespace, [:set, rules.values.map(&:to_ast)]]]
        else
          [:set, rules.values.map(&:to_ast)]
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry-schema-0.2.0 lib/dry/schema/rule_applier.rb