Class: Dry::Schema::Info::SchemaCompiler Private

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/schema/extensions/info/schema_compiler.rb

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.

Constant Summary collapse

PREDICATE_TO_TYPE =

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.

{
  array?: "array",
  bool?: "bool",
  date?: "date",
  date_time?: "date_time",
  decimal?: "float",
  float?: "float",
  hash?: "hash",
  int?: "integer",
  nil?: "nil",
  str?: "string",
  time?: "time"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSchemaCompiler

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



29
30
31
# File 'lib/dry/schema/extensions/info/schema_compiler.rb', line 29

def initialize
  @keys = EMPTY_HASH.dup
end

Instance Attribute Details

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



26
27
28
# File 'lib/dry/schema/extensions/info/schema_compiler.rb', line 26

def keys
  @keys
end

Instance Method Details

#assign_type(key, type) ⇒ 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.



104
105
106
107
108
109
110
# File 'lib/dry/schema/extensions/info/schema_compiler.rb', line 104

def assign_type(key, type)
  if keys[key][:type]
    keys[key][:member] = type
  else
    keys[key][:type] = type
  end
end

#call(ast) ⇒ 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
# File 'lib/dry/schema/extensions/info/schema_compiler.rb', line 39

def call(ast)
  visit(ast)
end

#to_hObject

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.



34
35
36
# File 'lib/dry/schema/extensions/info/schema_compiler.rb', line 34

def to_h
  {keys: keys}
end

#visit(node, opts = EMPTY_HASH) ⇒ 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.



44
45
46
47
# File 'lib/dry/schema/extensions/info/schema_compiler.rb', line 44

def visit(node, opts = EMPTY_HASH)
  meth, rest = node
  public_send(:"visit_#{meth}", rest, opts)
end

#visit_and(node, opts = EMPTY_HASH) ⇒ 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
# File 'lib/dry/schema/extensions/info/schema_compiler.rb', line 64

def visit_and(node, opts = EMPTY_HASH)
  left, right = node

  visit(left, opts)
  visit(right, opts)
end

#visit_each(node, opts = EMPTY_HASH) ⇒ 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.



79
80
81
# File 'lib/dry/schema/extensions/info/schema_compiler.rb', line 79

def visit_each(node, opts = EMPTY_HASH)
  visit(node, opts.merge(member: true))
end

#visit_implication(node, opts = EMPTY_HASH) ⇒ 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.



72
73
74
75
76
# File 'lib/dry/schema/extensions/info/schema_compiler.rb', line 72

def visit_implication(node, opts = EMPTY_HASH)
  node.each do |el|
    visit(el, opts.merge(required: false))
  end
end

#visit_key(node, opts = EMPTY_HASH) ⇒ 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.



84
85
86
87
# File 'lib/dry/schema/extensions/info/schema_compiler.rb', line 84

def visit_key(node, opts = EMPTY_HASH)
  name, rest = node
  visit(rest, opts.merge(key: name, required: true))
end

#visit_predicate(node, opts = EMPTY_HASH) ⇒ 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.



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/dry/schema/extensions/info/schema_compiler.rb', line 90

def visit_predicate(node, opts = EMPTY_HASH)
  name, rest = node

  key = opts[:key]

  if name.equal?(:key?)
    keys[rest[0][1]] = {required: opts.fetch(:required, true)}
  else
    type = PREDICATE_TO_TYPE[name]
    assign_type(key, type) if type
  end
end

#visit_set(node, opts = EMPTY_HASH) ⇒ 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.



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/dry/schema/extensions/info/schema_compiler.rb', line 50

def visit_set(node, opts = EMPTY_HASH)
  target = (key = opts[:key]) ? self.class.new : self

  node.each { |child| target.visit(child, opts) }

  return unless key

  target_info = opts[:member] ? {member: target.to_h} : target.to_h
  type = opts[:member] ? "array" : "hash"

  keys.update(key => {**keys[key], type: type, **target_info})
end