Class: Dry::Validation::Schema::DSL
- Inherits:
- BasicObject
- Defined in:
- lib/dry/validation/schema/dsl.rb
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.
-
#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
- #attr(name, &block) ⇒ Object
-
#initialize(options = {}) ⇒ DSL
constructor
A new instance of DSL.
- #inspect ⇒ Object (also: #to_s)
- #key(name, &block) ⇒ Object
- #not ⇒ Object
- #optional(name, &block) ⇒ Object
- #path ⇒ Object
- #rule_ast ⇒ Object
- #to_ast ⇒ Object
- #to_rule ⇒ Object
- #with(new_options) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ DSL
Returns a new instance of DSL.
13 14 15 16 17 18 19 |
# File 'lib/dry/validation/schema/dsl.rb', line 13 def initialize( = {}) @name = [:name] @parent = [:parent] @rules = .fetch(:rules, []) @checks = .fetch(:checks, []) @options = end |
Instance Attribute Details
#checks ⇒ Object (readonly)
Returns the value of attribute checks.
7 8 9 |
# File 'lib/dry/validation/schema/dsl.rb', line 7 def checks @checks end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/dry/validation/schema/dsl.rb', line 7 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/dry/validation/schema/dsl.rb', line 7 def @options end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
7 8 9 |
# File 'lib/dry/validation/schema/dsl.rb', line 7 def parent @parent end |
#rules ⇒ Object (readonly)
Returns the value of attribute rules.
7 8 9 |
# File 'lib/dry/validation/schema/dsl.rb', line 7 def rules @rules end |
Class Method Details
.[](name, options = {}) ⇒ Object
9 10 11 |
# File 'lib/dry/validation/schema/dsl.rb', line 9 def self.[](name, = {}) new(.merge(name: name)) end |
Instance Method Details
#add_check(check) ⇒ Object
49 50 51 52 |
# File 'lib/dry/validation/schema/dsl.rb', line 49 def add_check(check) checks << check self end |
#add_rule(rule) ⇒ Object
44 45 46 47 |
# File 'lib/dry/validation/schema/dsl.rb', line 44 def add_rule(rule) rules << rule self end |
#attr(name, &block) ⇒ Object
34 35 36 |
# File 'lib/dry/validation/schema/dsl.rb', line 34 def attr(name, &block) define(name, Attr, &block) end |
#inspect ⇒ Object Also known as: to_s
21 22 23 |
# File 'lib/dry/validation/schema/dsl.rb', line 21 def inspect to_ast.inspect end |
#key(name, &block) ⇒ Object
26 27 28 |
# File 'lib/dry/validation/schema/dsl.rb', line 26 def key(name, &block) define(name, Key, &block) end |
#not ⇒ Object
38 39 40 41 42 |
# File 'lib/dry/validation/schema/dsl.rb', line 38 def not negated = create_rule([:not, to_ast]) @rules = [negated] self end |
#optional(name, &block) ⇒ Object
30 31 32 |
# File 'lib/dry/validation/schema/dsl.rb', line 30 def optional(name, &block) define(name, Key, :then, &block) end |
#path ⇒ Object
67 68 69 70 |
# File 'lib/dry/validation/schema/dsl.rb', line 67 def path items = [parent && parent.path, name].flatten.compact.uniq items.size == 1 ? items[0] : items end |
#rule_ast ⇒ Object
63 64 65 |
# File 'lib/dry/validation/schema/dsl.rb', line 63 def rule_ast rules.map(&:to_ast) end |
#to_ast ⇒ Object
54 55 56 57 |
# File 'lib/dry/validation/schema/dsl.rb', line 54 def to_ast ast = rules.map(&:to_ast) ast.size > 1 ? [:set, ast] : ast[0] || [] end |
#to_rule ⇒ Object
59 60 61 |
# File 'lib/dry/validation/schema/dsl.rb', line 59 def to_rule create_rule(to_ast) end |
#with(new_options) ⇒ Object
72 73 74 |
# File 'lib/dry/validation/schema/dsl.rb', line 72 def with() self.class.new(.merge()) end |