Sha256: 1a1829ebe9749f158ef63693ed4c057b1bd6c6b2aee8e0d0448c541fade37097

Contents?: true

Size: 1.05 KB

Versions: 5

Compression:

Stored size: 1.05 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 hash?(&block)
          val = Value[name]
          val.instance_eval(&block)

          rule = create_rule([:val, [:predicate, [:hash?, []]]])
            .and(create_rule([type, [name, [:set, val.rules.map(&:to_ast)]]]))

          add_rule(rule)

          rule
        end

        private

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

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dry-validation-0.7.4 lib/dry/validation/schema/key.rb
dry-validation-0.7.3 lib/dry/validation/schema/key.rb
dry-validation-0.7.2 lib/dry/validation/schema/key.rb
dry-validation-0.7.1 lib/dry/validation/schema/key.rb
dry-validation-0.7.0 lib/dry/validation/schema/key.rb