Class: Dry::Schema::Trace Private

Inherits:
BasicObject
Defined in:
lib/dry/schema/trace.rb

Overview

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

Captures predicates defined within the DSL

Constant Summary collapse

INVALID_PREDICATES =

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

%i[key?].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(compiler = Compiler.new) ⇒ Trace

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 Trace.



24
25
26
27
# File 'lib/dry/schema/trace.rb', line 24

def initialize(compiler = Compiler.new)
  @compiler = compiler
  @captures = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object (private)

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.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/dry/schema/trace.rb', line 84

def method_missing(meth, *args, &block)
  if meth.to_s.end_with?(QUESTION_MARK)
    if ::Dry::Schema::Trace::INVALID_PREDICATES.include?(meth)
      ::Kernel.raise InvalidSchemaError, "#{meth} predicate cannot be used in this context"
    end

    unless compiler.supports?(meth)
      ::Kernel.raise ::ArgumentError, "#{meth} predicate is not defined"
    end

    predicate = Predicate.new(compiler, meth, args, block)
    predicate.ensure_valid
    predicate
  else
    super
  end
end

Instance Attribute Details

#capturesObject (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.



21
22
23
# File 'lib/dry/schema/trace.rb', line 21

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



18
19
20
# File 'lib/dry/schema/trace.rb', line 18

def compiler
  @compiler
end

Instance Method Details

#append(op) ⇒ Object Also known as: <<

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.



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

def append(op)
  captures << op
  self
end

#classObject

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.



72
73
74
# File 'lib/dry/schema/trace.rb', line 72

def class
  ::Dry::Schema::Trace
end

#evaluate(*predicates, **opts, &block) ⇒ Object

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.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dry/schema/trace.rb', line 30

def evaluate(*predicates, **opts, &block)
  predicates.each do |predicate|
    if predicate.respond_to?(:call)
      append(predicate)
    elsif predicate.is_a?(::Array)
      append(predicate.map { |pred| __send__(pred) }.reduce(:|))
    else
      append(__send__(predicate))
    end
  end

  opts.each do |predicate, *args|
    append(__send__(predicate, *args))
  end

  self
end

#to_astObject

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.



67
68
69
# File 'lib/dry/schema/trace.rb', line 67

def to_ast
  reduced_rule.to_ast
end

#to_rule(name = nil) ⇒ Object

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.



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

def to_rule(name = nil)
  return if captures.empty?

  if name
    compiler.visit([:key, [name, to_ast]])
  else
    reduced_rule
  end
end