Class: Dry::Schema::Predicate

Inherits:
Object
  • Object
show all
Includes:
Logic::Operators
Defined in:
lib/dry/schema/predicate.rb

Overview

Predicate objects used within the DSL

Defined Under Namespace

Classes: Negation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(compiler, name, args, block) ⇒ Predicate

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Predicate.



56
57
58
59
60
61
62
63
# File 'lib/dry/schema/predicate.rb', line 56

def initialize(compiler, name, args, block)
  @compiler = compiler
  @name = name
  @args = args
  @block = block
  # Cater for optional second argument like in case of `eql?` or `respond_to?`
  @arity = compiler.predicates[name].arity.abs
end

Instance Attribute Details

#argsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



47
48
49
# File 'lib/dry/schema/predicate.rb', line 47

def args
  @args
end

#arityObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



50
51
52
# File 'lib/dry/schema/predicate.rb', line 50

def arity
  @arity
end

#blockObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



53
54
55
# File 'lib/dry/schema/predicate.rb', line 53

def block
  @block
end

#compilerObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



41
42
43
# File 'lib/dry/schema/predicate.rb', line 41

def compiler
  @compiler
end

#nameObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



44
45
46
# File 'lib/dry/schema/predicate.rb', line 44

def name
  @name
end

Instance Method Details

#!Negation

Negate a predicate

Examples:

required(:name).value(:string) { !empty? }

Returns:



73
74
75
# File 'lib/dry/schema/predicate.rb', line 73

def !
  Negation.new(self)
end

#ensure_validObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



78
79
80
81
82
# File 'lib/dry/schema/predicate.rb', line 78

def ensure_valid
  if arity - 1 != args.size
    raise ArgumentError, "#{name} predicate arity is invalid"
  end
end

#to_astArray Also known as: ast

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Dump predicate to an AST

Returns:

  • (Array)


96
97
98
# File 'lib/dry/schema/predicate.rb', line 96

def to_ast(*)
  [:predicate, [name, compiler.predicates.arg_list(name, *args)]]
end

#to_ruleObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Compile predicate to a rule object



87
88
89
# File 'lib/dry/schema/predicate.rb', line 87

def to_rule
  compiler.visit(to_ast)
end