Sha256: acbc26da0ed30b79e16669eae114123f9aee343d9104efdc2ab377ee61676437

Contents?: true

Size: 903 Bytes

Versions: 1

Compression:

Stored size: 903 Bytes

Contents

require 'dry/validation/rule'

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

        def initialize(name, rules, &block)
          @name = name
          @rules = rules
        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)
                Definition::Rule.new([:and, [key_rule, [:set, [name, val_rule.map(&:to_ary)]]]])
              else
                Definition::Rule.new([:and, [key_rule, val_rule.to_ary]])
              end
          else
            Definition::Rule.new(key_rule)
          end
        end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry-validation-0.1.0 lib/dry/validation/schema/key.rb