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.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/dry/schema/trace.rb', line 92

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.



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

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.



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

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

#evaluate(*args, type_spec: ::Dry::Schema::Undefined, **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
# File 'lib/dry/schema/trace.rb', line 32

def evaluate(*args, type_spec: ::Dry::Schema::Undefined, **opts)
  predicates = opts.empty? ? args : args.push(opts)
  evaluate_predicates(predicates).each do |rule|
    append(rule)
  end

  self
end

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



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/dry/schema/trace.rb', line 42

def evaluate_predicates(predicates)
  predicates.flat_map do |predicate|
    if predicate.respond_to?(:call)
      predicate
    elsif predicate.is_a?(::Array)
      predicate.map { |pred| evaluate_predicates(pred).reduce(:&) }.reduce(:|)
    elsif predicate.is_a?(::Hash)
      predicate.map { |pred, *args| __send__(pred, *args) }
    else
      __send__(predicate)
    end
  end
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.



75
76
77
# File 'lib/dry/schema/trace.rb', line 75

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.



64
65
66
67
68
69
70
71
72
# File 'lib/dry/schema/trace.rb', line 64

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

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