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.



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

def initialize(compiler, name, args, block)
  @compiler = compiler
  @name = name
  @args = args
  @block = block
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.



39
40
41
# File 'lib/dry/schema/predicate.rb', line 39

def args
  @args
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.



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

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.



33
34
35
# File 'lib/dry/schema/predicate.rb', line 33

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.



36
37
38
# File 'lib/dry/schema/predicate.rb', line 36

def name
  @name
end

Instance Method Details

#!Negation

Negate a predicate

Returns:



57
58
59
# File 'lib/dry/schema/predicate.rb', line 57

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.



62
63
64
65
66
# File 'lib/dry/schema/predicate.rb', line 62

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

#to_astObject 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.



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

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.



69
70
71
# File 'lib/dry/schema/predicate.rb', line 69

def to_rule
  compiler.visit(to_ast)
end