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.
- #key(name, &block) ⇒ Object
- #not ⇒ Object
- #optional(name, &block) ⇒ Object
- #path ⇒ 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
44 45 46 47 |
# File 'lib/dry/validation/schema/dsl.rb', line 44 def add_check(check) checks << check self end |
#add_rule(rule) ⇒ Object
39 40 41 42 |
# File 'lib/dry/validation/schema/dsl.rb', line 39 def add_rule(rule) rules << rule self end |
#attr(name, &block) ⇒ Object
29 30 31 |
# File 'lib/dry/validation/schema/dsl.rb', line 29 def attr(name, &block) define(name, Attr, &block) end |
#key(name, &block) ⇒ Object
21 22 23 |
# File 'lib/dry/validation/schema/dsl.rb', line 21 def key(name, &block) define(name, Key, &block) end |
#not ⇒ Object
33 34 35 36 37 |
# File 'lib/dry/validation/schema/dsl.rb', line 33 def not negated = create_rule([:not, to_ast]) @rules = [negated] self end |
#optional(name, &block) ⇒ Object
25 26 27 |
# File 'lib/dry/validation/schema/dsl.rb', line 25 def optional(name, &block) define(name, Key, :then, &block) end |
#path ⇒ Object
58 59 60 61 |
# File 'lib/dry/validation/schema/dsl.rb', line 58 def path items = [parent && parent.path, name].flatten.compact.uniq items.size == 1 ? items[0] : items end |
#to_ast ⇒ Object
49 50 51 52 |
# File 'lib/dry/validation/schema/dsl.rb', line 49 def to_ast ast = rules.map(&:to_ast) ast.size > 1 ? [:set, ast] : ast[0] || [] end |
#to_rule ⇒ Object
54 55 56 |
# File 'lib/dry/validation/schema/dsl.rb', line 54 def to_rule create_rule(to_ast) end |
#with(new_options) ⇒ Object
63 64 65 |
# File 'lib/dry/validation/schema/dsl.rb', line 63 def with() self.class.new(.merge()) end |