Class: Schematist::DSL::ConditionalBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/schematist/dsl/conditional_builder.rb

Overview

A branch of a conditional. Backed by a schema class, so anything you can write in a schema you can write in a branch: nested objects, arrays, composition, references, raw fragments. requires and validates stay as shorthands for the two common cases.

Constant Summary collapse

VALIDATES_KEY_MAP =
{
  type: :type,
  const: :const,
  enum: :enum,
  not_value: :not,
  min_length: :minLength,
  max_length: :maxLength,
  pattern: :pattern,
  minimum: :minimum,
  maximum: :maximum
}.freeze

Instance Method Summary collapse

Constructor Details

#initializeConditionalBuilder

Returns a new instance of ConditionalBuilder.



21
22
23
# File 'lib/schematist/dsl/conditional_builder.rb', line 21

def initialize
  @schema_class = Class.new(Schema)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object

Everything else is the ordinary schema DSL, evaluated against the branch's schema class



74
75
76
77
78
# File 'lib/schematist/dsl/conditional_builder.rb', line 74

def method_missing(name, ...)
  return super unless @schema_class.respond_to?(name)

  @schema_class.public_send(name, ...)
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/schematist/dsl/conditional_builder.rb', line 59

def empty?
  to_schema.empty?
end

#required_fieldsObject



63
64
65
# File 'lib/schematist/dsl/conditional_builder.rb', line 63

def required_fields
  branch_required
end

#requires(*fields) ⇒ Object

Requires properties without saying anything else about them



26
27
28
# File 'lib/schematist/dsl/conditional_builder.rb', line 26

def requires(*fields)
  required.concat(fields.map(&:to_s))
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/schematist/dsl/conditional_builder.rb', line 80

def respond_to_missing?(name, include_private = false)
  @schema_class.respond_to?(name) || super
end

#to_schemaObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/schematist/dsl/conditional_builder.rb', line 48

def to_schema
  return @schema_class.self_schema.dup if @schema_class.self_schema

  schema = {}
  schema[:properties] = branch_properties if branch_properties.any?
  schema[:required] = branch_required if branch_required.any?
  schema[:additionalProperties] = @schema_class.additional_properties if additional_properties_set?

  @schema_class.send(:merge_schema_keywords, schema, @schema_class)
end

#validates(field, **options) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/schematist/dsl/conditional_builder.rb', line 30

def validates(field, **options)
  constraints = {}

  options.each do |key, value|
    schema_key = VALIDATES_KEY_MAP[key]
    raise ArgumentError, "unknown validates option: #{key.inspect}" unless schema_key

    case key
    when :type then constraints[:type] = value.to_s
    when :not_value then constraints[:not] = {const: value}
    when :pattern then constraints[:pattern] = value.is_a?(Regexp) ? value.source : value
    else constraints[schema_key] = value
    end
  end

  validations[field.to_s] = constraints
end

#validations_empty?Boolean

A branch that only lists required properties becomes dependentRequired rather than dependentSchemas, which is the smaller thing to say when it is all you mean.

Returns:

  • (Boolean)


69
70
71
# File 'lib/schematist/dsl/conditional_builder.rb', line 69

def validations_empty?
  to_schema.keys == [:required]
end