Sha256: ddfc6a0a9d0afc34009204a3a24e15a74a4804d93d29f956c55c598fb8f7a5c0

Contents?: true

Size: 1.48 KB

Versions: 19

Compression:

Stored size: 1.48 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 = predicate(:hash?)

          if block
            val = value.instance_eval(&block)

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

            add_rule(rule)
            rule
          else
            add_rule(create_rule(predicate))
          end
        end

        def value
          Value[name, registry: registry]
        end

        private

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

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

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
dry-validation-0.13.3 lib/dry/validation/schema/key.rb
dry-validation-0.13.2 lib/dry/validation/schema/key.rb
dry-validation-0.13.1 lib/dry/validation/schema/key.rb
dry-validation-0.11.2 lib/dry/validation/schema/key.rb
dry-validation-0.12.3 lib/dry/validation/schema/key.rb
dry-validation-0.13.0 lib/dry/validation/schema/key.rb
dry-validation-0.12.2 lib/dry/validation/schema/key.rb
dry-validation-0.12.1 lib/dry/validation/schema/key.rb
dry-validation-0.12.0 lib/dry/validation/schema/key.rb
dry-validation-0.11.1 lib/dry/validation/schema/key.rb
dry-validation-0.11.0 lib/dry/validation/schema/key.rb
dry-validation-0.10.7 lib/dry/validation/schema/key.rb
dry-validation-0.10.6 lib/dry/validation/schema/key.rb
dry-validation-0.10.5 lib/dry/validation/schema/key.rb
dry-validation-0.10.4 lib/dry/validation/schema/key.rb
dry-validation-0.10.3 lib/dry/validation/schema/key.rb
dry-validation-0.10.2 lib/dry/validation/schema/key.rb
dry-validation-0.10.1 lib/dry/validation/schema/key.rb
dry-validation-0.10.0 lib/dry/validation/schema/key.rb