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?(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 |
# File 'lib/dry/validation/schema/dsl.rb', line 16 def initialize( = {}) @name = [: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
51 52 53 54 |
# File 'lib/dry/validation/schema/dsl.rb', line 51 def add_check(check) checks << check self end |
#add_rule(rule) ⇒ Object
46 47 48 49 |
# File 'lib/dry/validation/schema/dsl.rb', line 46 def add_rule(rule) rules << rule self end |
#inspect ⇒ Object Also known as: to_s
25 26 27 |
# File 'lib/dry/validation/schema/dsl.rb', line 25 def inspect to_ast.inspect end |
#not ⇒ Object
40 41 42 43 44 |
# File 'lib/dry/validation/schema/dsl.rb', line 40 def not negated = create_rule([:not, to_ast]) @rules = [negated] self end |
#optional(name, type_spec = nil, &block) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/dry/validation/schema/dsl.rb', line 30 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
69 70 71 72 |
# File 'lib/dry/validation/schema/dsl.rb', line 69 def path items = [parent && parent.path, name].flatten.compact.uniq items.size == 1 ? items[0] : items end |
#predicate?(meth) ⇒ Boolean
78 79 80 |
# File 'lib/dry/validation/schema/dsl.rb', line 78 def predicate?(meth) registry.key?(meth) end |
#rule_ast ⇒ Object
65 66 67 |
# File 'lib/dry/validation/schema/dsl.rb', line 65 def rule_ast rules.map(&:to_ast) end |
#to_ast ⇒ Object
56 57 58 59 |
# File 'lib/dry/validation/schema/dsl.rb', line 56 def to_ast ast = rules.map(&:to_ast) ast.size > 1 ? [:set, ast] : ast[0] || [] end |
#to_rule ⇒ Object
61 62 63 |
# File 'lib/dry/validation/schema/dsl.rb', line 61 def to_rule create_rule(to_ast) end |
#with(new_options) ⇒ Object
74 75 76 |
# File 'lib/dry/validation/schema/dsl.rb', line 74 def with() self.class.new(.merge()) end |