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.



26
27
28
29
# File 'lib/dry/schema/trace.rb', line 26

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.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/dry/schema/trace.rb', line 97

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.



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

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.



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

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.



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

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.



85
86
87
# File 'lib/dry/schema/trace.rb', line 85

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

#evaluate(*predicates, **opts) ⇒ 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.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dry/schema/trace.rb', line 32

def evaluate(*predicates, **opts)
  pred_opts = opts.dup
  pred_opts.delete(:type_spec)

  predicates.each do |predicate|
    if predicate.respond_to?(:call)
      append(predicate)
    elsif predicate.is_a?(::Hash)
      evaluate_hash_predicates(predicate)
    elsif predicate.is_a?(::Array)
      append(predicate.map { |pred| __send__(pred) }.reduce(:|))
    else
      append(__send__(predicate))
    end
  end

  evaluate_hash_predicates(pred_opts)

  self
end

#evaluate_hash_predicates(predicates) ⇒ 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.



54
55
56
57
58
59
# File 'lib/dry/schema/trace.rb', line 54

def evaluate_hash_predicates(predicates)
  predicates.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.



80
81
82
# File 'lib/dry/schema/trace.rb', line 80

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.



69
70
71
72
73
74
75
76
77
# File 'lib/dry/schema/trace.rb', line 69

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

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