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.



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

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.



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

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.support?(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.



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

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.



16
17
18
# File 'lib/dry/schema/trace.rb', line 16

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.



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

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.



77
78
79
# File 'lib/dry/schema/trace.rb', line 77

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

#evaluate(*args, **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.



28
29
30
31
32
33
34
35
36
# File 'lib/dry/schema/trace.rb', line 28

def evaluate(*args, **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.



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dry/schema/trace.rb', line 39

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.



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

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.



61
62
63
64
65
66
67
68
69
# File 'lib/dry/schema/trace.rb', line 61

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

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