Sha256: 0044ca280fa0523a61849d89c26e44e549b5a59a7453bfd5225afa9997a5c502

Contents?: true

Size: 1.53 KB

Versions: 6

Compression:

Stored size: 1.53 KB

Contents

require 'dry/validation/schema/dsl'

module Dry
  module Validation
    class Schema
      class Key < DSL
        attr_reader :parent

        def self.type
          :key
        end

        def class
          Key
        end

        def type
          self.class.type
        end

        def to_ast
          [type, [name, super]]
        end

        def each(*predicates, &block)
          create_rule([type, [name, value.each(*predicates, &block).to_ast]])
        end

        def schema(other = nil, &block)
          create_rule([type, [name, value.schema(other, &block).to_ast]])
        end

        def hash?(&block)
          predicate = registry[:hash?]

          if block
            val = value.instance_eval(&block)

            rule = create_rule([:val, predicate.to_ast])
              .and(create_rule([type, [name, val.to_ast]]))

            add_rule(rule)
            rule
          else
            add_rule(create_rule([:val, predicate.to_ast]))
          end
        end

        def value
          Value[name, registry: registry]
        end

        private

        def method_missing(meth, *args, &block)
          registry.ensure_valid_predicate(meth, args)
          predicate = registry[meth].curry(*args)

          if block
            val = value.instance_eval(&block)
            add_rule(create_rule([:and, [[:val, predicate.to_ast], val.to_ast]]))
          else
            rule = create_rule([type, [name, predicate.to_ast]])
            add_rule(rule)
            rule
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
dry-validation-0.9.5 lib/dry/validation/schema/key.rb
dry-validation-0.9.4 lib/dry/validation/schema/key.rb
dry-validation-0.9.3 lib/dry/validation/schema/key.rb
dry-validation-0.9.2 lib/dry/validation/schema/key.rb
dry-validation-0.9.1 lib/dry/validation/schema/key.rb
dry-validation-0.9.0 lib/dry/validation/schema/key.rb