Class: Dry::Validation::Schema::DSL
Instance Attribute Summary collapse
-
#checks ⇒ Object
readonly
Returns the value of attribute checks.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#registry ⇒ Object
readonly
Returns the value of attribute registry.
-
#rules ⇒ Object
readonly
Returns the value of attribute rules.
Class Method Summary collapse
Instance Method Summary collapse
- #add_check(check) ⇒ Object
- #add_rule(rule) ⇒ Object
-
#initialize(options = {}) ⇒ DSL
constructor
A new instance of DSL.
- #inspect ⇒ Object (also: #to_s)
- #not ⇒ Object
- #optional(name, type_spec = nil, &block) ⇒ Object
- #path ⇒ Object
- #predicate(name, args = EMPTY_ARRAY) ⇒ Object
- #predicate?(meth) ⇒ Boolean
- #rule_ast ⇒ Object
- #to_ast ⇒ Object
- #to_rule ⇒ Object
- #with(new_options) ⇒ Object
Methods included from Deprecations
Constructor Details
#initialize(options = {}) ⇒ DSL
Returns a new instance of DSL.
16 17 18 19 20 21 22 23 24 |
# File 'lib/dry/validation/schema/dsl.rb', line 16 def initialize( = {}) @name = [:name] @path = .fetch(:path, name) @parent = [:parent] @registry = .fetch(:registry) @rules = .fetch(:rules, []) @checks = .fetch(:checks, []) @options = end |
Instance Attribute Details
#checks ⇒ Object (readonly)
Returns the value of attribute checks.
10 11 12 |
# File 'lib/dry/validation/schema/dsl.rb', line 10 def checks @checks end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
10 11 12 |
# File 'lib/dry/validation/schema/dsl.rb', line 10 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
10 11 12 |
# File 'lib/dry/validation/schema/dsl.rb', line 10 def @options end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
10 11 12 |
# File 'lib/dry/validation/schema/dsl.rb', line 10 def parent @parent end |
#registry ⇒ Object (readonly)
Returns the value of attribute registry.
10 11 12 |
# File 'lib/dry/validation/schema/dsl.rb', line 10 def registry @registry end |
#rules ⇒ Object (readonly)
Returns the value of attribute rules.
10 11 12 |
# File 'lib/dry/validation/schema/dsl.rb', line 10 def rules @rules end |
Class Method Details
.[](name, options = {}) ⇒ Object
12 13 14 |
# File 'lib/dry/validation/schema/dsl.rb', line 12 def self.[](name, = {}) new(.merge(name: name)) end |
Instance Method Details
#add_check(check) ⇒ Object
52 53 54 55 |
# File 'lib/dry/validation/schema/dsl.rb', line 52 def add_check(check) checks << check.to_rule self end |
#add_rule(rule) ⇒ Object
47 48 49 50 |
# File 'lib/dry/validation/schema/dsl.rb', line 47 def add_rule(rule) rules << rule self end |
#inspect ⇒ Object Also known as: to_s
26 27 28 |
# File 'lib/dry/validation/schema/dsl.rb', line 26 def inspect to_ast.inspect end |
#not ⇒ Object
41 42 43 44 45 |
# File 'lib/dry/validation/schema/dsl.rb', line 41 def not negated = create_rule([:not, to_ast]) @rules = [negated] self end |
#optional(name, type_spec = nil, &block) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/dry/validation/schema/dsl.rb', line 31 def optional(name, type_spec = nil, &block) rule = define(name, Key, :then, &block) if type_spec type_map[name] = type_spec end rule end |
#path ⇒ Object
70 71 72 73 |
# File 'lib/dry/validation/schema/dsl.rb', line 70 def path items = [parent && parent.path, *@path].flatten.compact.uniq items.size == 1 ? items[0] : items end |
#predicate(name, args = EMPTY_ARRAY) ⇒ Object
79 80 81 |
# File 'lib/dry/validation/schema/dsl.rb', line 79 def predicate(name, args = EMPTY_ARRAY) [:predicate, [name, registry.arg_list(name, *args)]] end |
#predicate?(meth) ⇒ Boolean
83 84 85 |
# File 'lib/dry/validation/schema/dsl.rb', line 83 def predicate?(meth) registry.key?(meth) end |
#rule_ast ⇒ Object
66 67 68 |
# File 'lib/dry/validation/schema/dsl.rb', line 66 def rule_ast rules.map(&:to_ast) end |
#to_ast ⇒ Object
57 58 59 60 |
# File 'lib/dry/validation/schema/dsl.rb', line 57 def to_ast ast = rules.map(&:to_ast) ast.size > 1 ? [:set, ast] : ast[0] || [] end |
#to_rule ⇒ Object
62 63 64 |
# File 'lib/dry/validation/schema/dsl.rb', line 62 def to_rule create_rule(to_ast) end |
#with(new_options) ⇒ Object
75 76 77 |
# File 'lib/dry/validation/schema/dsl.rb', line 75 def with() self.class.new(.merge()) end |