Module: JSON::SchemaDsl

Included in:
Proxy
Defined in:
lib/json/schema_dsl.rb,
lib/json/schema_dsl/null.rb,
lib/json/schema_dsl/array.rb,
lib/json/schema_dsl/proxy.rb,
lib/json/schema_dsl/types.rb,
lib/json/schema_dsl/entity.rb,
lib/json/schema_dsl/object.rb,
lib/json/schema_dsl/string.rb,
lib/json/schema_dsl/boolean.rb,
lib/json/schema_dsl/builder.rb,
lib/json/schema_dsl/integer.rb,
lib/json/schema_dsl/numeric.rb,
lib/json/schema_dsl/ast_node.rb,
lib/json/schema_dsl/renderer.rb,
lib/json/schema_dsl/configuration.rb,
lib/json/schema_dsl/renderers/base.rb,
lib/json/schema_dsl/renderers/alias.rb,
lib/json/schema_dsl/renderers/filter.rb,
lib/json/schema_dsl/renderers/desugar.rb,
lib/json/schema_dsl/renderers/multiplexer.rb

Overview

This module provides the base that it includes with the methods to build new json-schemas.

Defined Under Namespace

Modules: AstNode, Renderers Classes: Array, Boolean, Builder, Configuration, Entity, Error, Integer, Null, Number, Numeric, Object, Proxy, Renderer, String, Types

Constant Summary collapse

DEFAULT_TYPES =
JSON::SchemaDsl::Entity
.descendants.dup.push(JSON::SchemaDsl::Entity).freeze
DEFAULT_RENDERERS =
[Renderers::Desugar,
Renderers::Multiplexer,
Renderers::Alias,
Renderers::Filter].freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.registered_renderersArray<Class>

Returns The renderer classes that schema_dsl will use in the renderer.

Returns:

  • (Array<Class>)

    The renderer classes that schema_dsl will use in the renderer



44
45
46
# File 'lib/json/schema_dsl.rb', line 44

def registered_renderers
  @registered_renderers ||= DEFAULT_RENDERERS.dup
end

Class Method Details

.define_schema_dsl!Object

Defines the dsl for all registered types.



76
77
78
# File 'lib/json/schema_dsl.rb', line 76

def define_schema_dsl!
  registered_types.map { |t| define_type_methods(t) }
end

.define_type_methods(type) ⇒ Object

Defines builder methods for the given type.

Parameters:

  • type (Class)

    A class that is a AstNode



82
83
84
85
86
87
88
# File 'lib/json/schema_dsl.rb', line 82

def define_type_methods(type)
  JSON::SchemaDsl::Builder.define_builder_method(type)
  builder = JSON::SchemaDsl::Builder[type]
  define_method(type_method_name(type)) do |name = nil, **attributes, &block|
    builder.build(name, **attributes, scope: self, &block)
  end
end

.proxyJSON::SchemaDsl::Proxy

Returns a new proxy to build schemas.

Returns:



98
99
100
# File 'lib/json/schema_dsl.rb', line 98

def proxy
  ::JSON::SchemaDsl::Proxy.new
end

.register_type(type) ⇒ Array<Class>

Returns The registered types.

Parameters:

  • type (Class)

    A new type to be registered. This will define new builder and dsl methods for that type.

Returns:

  • (Array<Class>)

    The registered types.



63
64
65
# File 'lib/json/schema_dsl.rb', line 63

def register_type(type)
  registered_types.push(type).tap { define_type_methods(type) }
end

.registered_typesArray<Class>

Returns The registered types. These are used to add new dsl and builder methods.

Returns:

  • (Array<Class>)

    The registered types. These are used to add new dsl and builder methods.



56
57
58
# File 'lib/json/schema_dsl.rb', line 56

def registered_types
  @registered_types ||= DEFAULT_TYPES.dup
end

.reset!Object

Reset all settings to default.



91
92
93
94
95
# File 'lib/json/schema_dsl.rb', line 91

def reset!
  reset_registered_renderers!
  reset_type_defaults!
  reset_schema_dsl!
end

.reset_registered_renderers!Array<Class>

Resets the registered_renderers to the default settings

Returns:

  • (Array<Class>)

    The renderer classes that schema_dsl will use in the renderer



50
51
52
# File 'lib/json/schema_dsl.rb', line 50

def reset_registered_renderers!
  @registered_renderers = DEFAULT_RENDERERS.dup
end

.reset_schema_dsl!Object

Resets schema_dsl back to default. Removes all dsl methods and redefines

them with the default types.


69
70
71
72
73
# File 'lib/json/schema_dsl.rb', line 69

def reset_schema_dsl!
  type_methods.each { |tm| remove_method tm }
  @registered_types = DEFAULT_TYPES.dup
  define_schema_dsl!
end

.type_method_name(type) ⇒ String

Returns the name of the new method.

Parameters:

  • type (Class)

    The class for which a method will be defined.

Returns:

  • (String)

    the name of the new method.



109
110
111
# File 'lib/json/schema_dsl.rb', line 109

def type_method_name(type)
  type.type_method_name || 'entity'
end

.type_methodsArray<Symbol>

Returns An array of all type methods.

Returns:

  • (Array<Symbol>)

    An array of all type methods



103
104
105
# File 'lib/json/schema_dsl.rb', line 103

def type_methods
  registered_types.map { |t| type_method_name(t).to_sym } & instance_methods
end