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
Public Class Methods
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
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
@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
@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
@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
@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 all settings to default.
# File lib/json/schema_dsl.rb, line 91 def reset! reset_registered_renderers! reset_type_defaults! reset_schema_dsl! end
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
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
@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
@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