module JSON::SchemaDsl

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

Constants

DEFAULT_RENDERERS
DEFAULT_TYPES

Attributes

registered_renderers[W]

Public Class Methods

define_schema_dsl!() click to toggle source

Defines the dsl for all registered types.

# 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) click to toggle source

Defines builder methods for the given type. @param [Class] type A class that is a {JSON::SchemaDsl::AstNode}

# 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
proxy() click to toggle source

@return [JSON::SchemaDsl::Proxy] a new proxy to build schemas.

# File lib/json/schema_dsl.rb, line 98
def proxy
  ::JSON::SchemaDsl::Proxy.new
end
register_type(type) click to toggle source

@param [Class] type A new type to be registered. This will define new builder and dsl

methods for that type.

@return [Array<Class>] The registered types.

# File lib/json/schema_dsl.rb, line 63
def register_type(type)
  registered_types.push(type).tap { define_type_methods(type) }
end
registered_renderers() click to toggle source

@return [Array<Class>] The renderer classes that schema_dsl will use in the renderer

# File lib/json/schema_dsl.rb, line 44
def registered_renderers
  @registered_renderers ||= DEFAULT_RENDERERS.dup
end
registered_types() click to toggle source

@return [Array<Class>] The registered types. These are used to add new dsl

and builder methods.
# File lib/json/schema_dsl.rb, line 56
def registered_types
  @registered_types ||= DEFAULT_TYPES.dup
end
reset!() click to toggle source

Reset all settings to default.

# File lib/json/schema_dsl.rb, line 91
def reset!
  reset_registered_renderers!
  reset_type_defaults!
  reset_schema_dsl!
end
reset_registered_renderers!() click to toggle source

Resets the registered_renderers to the default settings @return [Array<Class>] The renderer classes that schema_dsl will use in the renderer

# File lib/json/schema_dsl.rb, line 50
def reset_registered_renderers!
  @registered_renderers = DEFAULT_RENDERERS.dup
end
reset_schema_dsl!() click to toggle source

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

them with the default types.
# 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) click to toggle source

@param [Class] type The class for which a method will be defined. @return [String] the name of the new method.

# File lib/json/schema_dsl.rb, line 109
def type_method_name(type)
  type.type_method_name || 'entity'
end
type_methods() click to toggle source

@return [Array<Symbol>] An array of all type methods

# File lib/json/schema_dsl.rb, line 103
def type_methods
  registered_types.map { |t| type_method_name(t).to_sym } & instance_methods
end