Sha256: a32d771b7d969baad2d83105e3c2aa9f54361279f68a8d6d19b6579bb983bdba

Contents?: true

Size: 1.3 KB

Versions: 2

Compression:

Stored size: 1.3 KB

Contents

require 'dry/validation/rule'

module Dry
  module Validation
    class Schema
      class Key < BasicObject
        attr_reader :name, :rules

        def initialize(name, rules, &block)
          @name = name
          @rules = rules
        end

        def optional(&block)
          key_rule = key?

          val_rule = yield(Value.new(name))

          rules <<
            if val_rule.is_a?(::Array)
              Schema::Rule.new(name, [:implication, [key_rule.to_ary, [:set, [name, val_rule.map(&:to_ary)]]]])
            else
              Schema::Rule.new(name, [:implication, [key_rule.to_ary, val_rule.to_ary]])
            end
        end

        private

        def method_missing(meth, *args, &block)
          key_rule = [:key, [name, [:predicate, [meth, args]]]]

          if block
            val_rule = yield(Value.new(name))

            rules <<
              if val_rule.is_a?(::Array)
                Schema::Rule.new(name, [:and, [key_rule, [:set, [name, val_rule.map(&:to_ary)]]]])
              else
                Schema::Rule.new(name, [:and, [key_rule, val_rule.to_ary]])
              end
          else
            Schema::Rule.new(name, key_rule)
          end
        end

        def respond_to_missing?(meth, _include_private = false)
          true
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dry-validation-0.4.1 lib/dry/validation/schema/key.rb
dry-validation-0.4.0 lib/dry/validation/schema/key.rb