Class: Dry::Schema::Info::SchemaCompiler Private
- Inherits:
-
Object
- Object
- Dry::Schema::Info::SchemaCompiler
- 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
- #keys ⇒ Object readonly private
Instance Method Summary collapse
- #assign_type(key, type, nullable) ⇒ Object private
- #call(ast) ⇒ Object private
-
#initialize ⇒ SchemaCompiler
constructor
private
A new instance of SchemaCompiler.
- #to_h ⇒ Object private
- #visit(node, opts = EMPTY_HASH) ⇒ Object private
- #visit_and(node, opts = EMPTY_HASH) ⇒ Object private
- #visit_each(node, opts = EMPTY_HASH) ⇒ Object private
- #visit_implication(node, opts = EMPTY_HASH) ⇒ Object private
- #visit_key(node, opts = EMPTY_HASH) ⇒ Object private
- #visit_predicate(node, opts = EMPTY_HASH) ⇒ Object private
- #visit_set(node, opts = EMPTY_HASH) ⇒ Object private
Constructor Details
#initialize ⇒ SchemaCompiler
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
#keys ⇒ Object (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, nullable) ⇒ 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.
112 113 114 115 116 117 118 119 |
# File 'lib/dry/schema/extensions/info/schema_compiler.rb', line 112 def assign_type(key, type, nullable) if keys[key][:type] keys[key][:member] = type else keys[key][:type] = type keys[key][:nullable] = nullable 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_h ⇒ 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.
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.
84 85 86 |
# File 'lib/dry/schema/extensions/info/schema_compiler.rb', line 84 def visit_each(node, opts = EMPTY_HASH) visit(node, {**opts, 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 77 78 79 80 81 |
# File 'lib/dry/schema/extensions/info/schema_compiler.rb', line 72 def visit_implication(node, opts = EMPTY_HASH) case node in [:not, [:predicate, [:nil?, _]]], el visit(el, {**opts, nullable: true}) else node.each do |el| visit(el, {**opts, required: false}) end 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.
89 90 91 92 |
# File 'lib/dry/schema/extensions/info/schema_compiler.rb', line 89 def visit_key(node, opts = EMPTY_HASH) name, rest = node visit(rest, {**opts, 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.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/dry/schema/extensions/info/schema_compiler.rb', line 95 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] nullable = opts.fetch(:nullable, false) assign_type(key, type, nullable) 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 |