Class: Dry::Validation::Rust::Schema::FieldDefinition Private

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/validation/rust/schema/field_definition.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(name:, required:) ⇒ FieldDefinition

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 FieldDefinition.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/dry/validation/rust/schema/field_definition.rb', line 12

def initialize(name:, required:)
  @name = name&.to_sym
  @required = required
  @nullable = false
  @filled = false
  @type = :any
  @member = nil
  @ruby_type = nil
  @children = []
  @children_by_name = {}
  @predicates = []
end

Instance Attribute Details

#childrenObject

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.



10
11
12
# File 'lib/dry/validation/rust/schema/field_definition.rb', line 10

def children
  @children
end

#filledObject

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/field_definition.rb', line 9

def filled
  @filled
end

#memberObject

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/field_definition.rb', line 9

def member
  @member
end

#nameObject

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/field_definition.rb', line 9

def name
  @name
end

#nullableObject

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/field_definition.rb', line 9

def nullable
  @nullable
end

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



10
11
12
# File 'lib/dry/validation/rust/schema/field_definition.rb', line 10

def predicates
  @predicates
end

#requiredObject

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/field_definition.rb', line 9

def required
  @required
end

#ruby_typeObject

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/field_definition.rb', line 9

def ruby_type
  @ruby_type
end

#typeObject

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/field_definition.rb', line 9

def type
  @type
end

Instance Method Details

#add_predicate(name, argument: true) ⇒ 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.



34
35
36
37
38
39
40
41
42
# File 'lib/dry/validation/rust/schema/field_definition.rb', line 34

def add_predicate(name, argument: true)
  normalized_name = name.to_s.delete_suffix('?').to_sym
  unless (NATIVE_PREDICATES | RUBY_PREDICATES).include?(normalized_name)
    raise UnsupportedFeatureError,
          "predicate #{normalized_name.inspect} is not supported natively; move it to a contract rule"
  end

  predicates << Predicate.new(name: normalized_name, argument: argument)
end

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



30
31
32
# File 'lib/dry/validation/rust/schema/field_definition.rb', line 30

def child_at(name)
  @children_by_name[name.to_sym]
end

#deep_dupObject

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.



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/dry/validation/rust/schema/field_definition.rb', line 65

def deep_dup
  self.class.new(name: name, required: required).tap do |copy|
    copy.nullable = nullable
    copy.filled = filled
    copy.type = type
    copy.ruby_type = ruby_type
    copy.member = member&.deep_dup
    copy.children = children.map(&:deep_dup)
    predicates.each do |predicate|
      copy.predicates << predicate.with(argument: duplicate_value(predicate.argument))
    end
  end
end

#normalized_typeObject

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.



61
62
63
# File 'lib/dry/validation/rust/schema/field_definition.rb', line 61

def normalized_type
  type == :datetime ? :date_time : type
end

#to_native_hObject

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.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/dry/validation/rust/schema/field_definition.rb', line 44

def to_native_h
  {
    name: name&.to_s,
    required: required,
    nullable: nullable,
    filled: filled,
    type: normalized_type.to_s,
    member: member&.to_native_h,
    children: children.map(&:to_native_h),
    predicates: predicates.filter_map do |predicate|
      next unless NATIVE_PREDICATES.include?(predicate.name)

      { name: predicate.name.to_s, argument: predicate.argument }
    end
  }
end