Class: Dry::Schema::Macros::DSL
- Includes:
- Logic::Operators
- Defined in:
- lib/dry/schema/macros/dsl.rb
Overview
Macro specialization used within the DSL
Instance Attribute Summary collapse
-
#chain ⇒ Boolean
readonly
private
Indicate if the macro should append its rules to the provided trace.
-
#predicate_inferrer ⇒ PredicateInferrer
readonly
private
PredicateInferrer is used to infer predicate type-check from a type spec.
-
#primitive_inferrer ⇒ PrimitiveInferrer
readonly
private
PrimitiveInferrer used to get a list of primitive classes from configured type.
Instance Method Summary collapse
-
#array ⇒ Macros::Core
Like `each` but sets `array?` type-check.
- #custom_type? ⇒ Boolean private
-
#each ⇒ Macros::Core
Specify predicates that should be applied to each element of an array.
-
#filled(*args, **opts, &block) ⇒ Macros::Core
Prepends `:filled?` predicate.
-
#hash ⇒ Object
Specify a nested hash with enforced `hash?` type-check.
-
#maybe(*args, **opts, &block) ⇒ Macros::Key
Set type specification and predicates for a maybe value.
-
#schema ⇒ Macros::Core
Specify a nested hash without enforced `hash?` type-check.
-
#type(spec) ⇒ Macros::Key
Set type spec.
-
#value(*predicates, **predicate_opts) ⇒ Macros::Core
Set predicates without and with arguments.
Methods inherited from Core
#new, #operation, #path, #to_ast, #to_rule
Instance Attribute Details
#chain ⇒ Boolean (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.
Indicate if the macro should append its rules to the provided trace
24 |
# File 'lib/dry/schema/macros/dsl.rb', line 24 option :chain, default: -> { true } |
#predicate_inferrer ⇒ PredicateInferrer (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.
PredicateInferrer is used to infer predicate type-check from a type spec
30 |
# File 'lib/dry/schema/macros/dsl.rb', line 30 option :predicate_inferrer, default: proc { PredicateInferrer.new(compiler.predicates) } |
#primitive_inferrer ⇒ PrimitiveInferrer (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.
PrimitiveInferrer used to get a list of primitive classes from configured type
36 |
# File 'lib/dry/schema/macros/dsl.rb', line 36 option :primitive_inferrer, default: proc { PrimitiveInferrer.new } |
Instance Method Details
#array ⇒ Macros::Core
Like `each` but sets `array?` type-check
177 178 179 180 181 |
# File 'lib/dry/schema/macros/dsl.rb', line 177 def array(...) append_macro(Macros::Array) do |macro| macro.value(...) end end |
#custom_type? ⇒ Boolean
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.
200 201 202 |
# File 'lib/dry/schema/macros/dsl.rb', line 200 def custom_type? schema_dsl.custom_type?(name) end |
#each ⇒ Macros::Core
Specify predicates that should be applied to each element of an array
This is a simpler building block than `array` macro, use it when you want to provide `array?` type-check with other rules manually.
157 158 159 160 161 |
# File 'lib/dry/schema/macros/dsl.rb', line 157 def each(...) append_macro(Macros::Each) do |macro| macro.value(...) end end |
#filled(*args, **opts, &block) ⇒ Macros::Core
Prepends `:filled?` predicate
78 79 80 81 82 83 84 |
# File 'lib/dry/schema/macros/dsl.rb', line 78 def filled(*args, **opts, &block) extract_type_spec(args) do |*predicates, type_spec:, type_rule:| append_macro(Macros::Filled) do |macro| macro.call(*predicates, type_spec: type_spec, type_rule: type_rule, **opts, &block) end end end |
#hash ⇒ Object
Specify a nested hash with enforced `hash?` type-check
133 134 135 136 137 |
# File 'lib/dry/schema/macros/dsl.rb', line 133 def hash(...) append_macro(Macros::Hash) do |macro| macro.call(...) end end |
#maybe(*args, **opts, &block) ⇒ Macros::Key
Set type specification and predicates for a maybe value
96 97 98 99 100 101 102 |
# File 'lib/dry/schema/macros/dsl.rb', line 96 def maybe(*args, **opts, &block) extract_type_spec(args, nullable: true) do |*predicates, type_spec:, type_rule:| append_macro(Macros::Maybe) do |macro| macro.call(*predicates, type_spec: type_spec, type_rule: type_rule, **opts, &block) end end end |
#schema ⇒ Macros::Core
Specify a nested hash without enforced `hash?` type-check
This is a simpler building block than `hash` macro, use it when you want to provide `hash?` type-check with other rules manually.
118 119 120 121 122 |
# File 'lib/dry/schema/macros/dsl.rb', line 118 def schema(...) append_macro(Macros::Schema) do |macro| macro.call(...) end end |
#type(spec) ⇒ Macros::Key
Set type spec
194 195 196 197 |
# File 'lib/dry/schema/macros/dsl.rb', line 194 def type(spec) schema_dsl.set_type(name, spec) self end |
#value(*predicates, **predicate_opts) ⇒ Macros::Core
59 60 61 62 63 64 65 |
# File 'lib/dry/schema/macros/dsl.rb', line 59 def value(*args, **opts, &block) extract_type_spec(args) do |*predicates, type_spec:, type_rule:| append_macro(Macros::Value) do |macro| macro.call(*predicates, type_spec: type_spec, type_rule: type_rule, **opts, &block) end end end |