Class: Dry::Validation::Rust::Schema::DSL Private

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/validation/rust/schema/dsl.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode:, fields: [], before_hooks: [], after_hooks: []) ⇒ DSL

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.

Returns a new instance of DSL.



11
12
13
14
15
16
# File 'lib/dry/validation/rust/schema/dsl.rb', line 11

def initialize(mode:, fields: [], before_hooks: [], after_hooks: [])
  @mode = mode.to_sym
  @fields = fields
  @before_hooks = before_hooks
  @after_hooks = after_hooks
end

Instance Attribute Details

#after_hooksObject (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.



9
10
11
# File 'lib/dry/validation/rust/schema/dsl.rb', line 9

def after_hooks
  @after_hooks
end

#before_hooksObject (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.



9
10
11
# File 'lib/dry/validation/rust/schema/dsl.rb', line 9

def before_hooks
  @before_hooks
end

#fieldsObject (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.



9
10
11
# File 'lib/dry/validation/rust/schema/dsl.rb', line 9

def fields
  @fields
end

#modeObject (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.



9
10
11
# File 'lib/dry/validation/rust/schema/dsl.rb', line 9

def mode
  @mode
end

Instance Method Details

#after(name, &block) ⇒ Object

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.



48
49
50
51
# File 'lib/dry/validation/rust/schema/dsl.rb', line 48

def after(name, &block)
  ProcessorHooks.register(after_hooks, name, block)
  self
end

#before(name, &block) ⇒ Object

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.



43
44
45
46
# File 'lib/dry/validation/rust/schema/dsl.rb', line 43

def before(name, &block)
  ProcessorHooks.register(before_hooks, name, block)
  self
end

#compile(validate_keys: false, messages: MessageConfig.new) ⇒ Object

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.



53
54
55
56
57
58
# File 'lib/dry/validation/rust/schema/dsl.rb', line 53

def compile(validate_keys: false, messages: MessageConfig.new)
  Schema.new(
    mode: mode, fields: fields, before_hooks: before_hooks, after_hooks: after_hooks,
    validate_keys: validate_keys, messages: messages
  )
end

#import(schema) ⇒ Object

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.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dry/validation/rust/schema/dsl.rb', line 26

def import(schema)
  unless schema.is_a?(Schema)
    raise UnsupportedFeatureError, 'only schemas built by Dry::Validation::Rust can be imported'
  end

  schema.fields.each do |field|
    if fields.any? { |existing| existing.name == field.name }
      raise ArgumentError, "key #{field.name.inspect} is already defined"
    end

    fields << field.deep_dup
  end
  before_hooks.concat(schema.send(:before_hooks))
  after_hooks.concat(schema.send(:after_hooks))
  self
end

#optional(name) ⇒ Object

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.



22
23
24
# File 'lib/dry/validation/rust/schema/dsl.rb', line 22

def optional(name, &)
  add_field(name, required: false, &)
end

#required(name) ⇒ Object

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.



18
19
20
# File 'lib/dry/validation/rust/schema/dsl.rb', line 18

def required(name, &)
  add_field(name, required: true, &)
end