Sha256: 630308f8c201ee7bb89e25cc4ef18e727a6666faf919bf9eae3263f58d80bdbb

Contents?: true

Size: 1.64 KB

Versions: 7

Compression:

Stored size: 1.64 KB

Contents

module Dry
  module Validation
    class ProcessInput
      attr_reader :processor

      def initialize(processor)
        @processor = processor
      end

      def call(input, *)
        processor.(input)
      end
    end

    class ApplyInputRule
      attr_reader :rule

      def initialize(rule)
        @rule = rule
      end

      def call(input, result)
        rule_res = rule.(input)
        result.update(nil => rule_res) unless rule_res.success?
        input
      end
    end

    class ApplyRules
      attr_reader :rules

      def initialize(rules)
        @rules = rules
      end

      def call(input, result)
        rules.each_with_object(result) do |(name, rule), hash|
          hash[name] = rule.(input)
        end
        input
      end
    end

    class ApplyChecks < ApplyRules
      def call(input, result)
        rules.each_with_object(result) do |(name, check), hash|
          check_res = check.is_a?(Guard) ? check.(input, result) : check.(input)
          hash[name] = check_res if check_res
        end
        input
      end
    end

    class BuildErrors
      def call(result)
        result.values.select(&:failure?)
      end
    end

    class Executor
      attr_reader :steps, :final

      def self.new(&block)
        super(BuildErrors.new).tap { |executor| yield(executor.steps) }.freeze
      end

      def initialize(final)
        @steps = []
        @final = final
      end

      def call(input, result = {})
        output = steps.reduce(input) do |a, e|
          return [a, final.(result)] if result.key?(nil)
          e.call(a, result)
        end
        [output, final.(result)]
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
dry-validation-0.10.6 lib/dry/validation/executor.rb
dry-validation-0.10.5 lib/dry/validation/executor.rb
dry-validation-0.10.4 lib/dry/validation/executor.rb
dry-validation-0.10.3 lib/dry/validation/executor.rb
dry-validation-0.10.2 lib/dry/validation/executor.rb
dry-validation-0.10.1 lib/dry/validation/executor.rb
dry-validation-0.10.0 lib/dry/validation/executor.rb