Module: JSI::SchemaClasses
- Extended by:
- Util::Memoize
- Defined in:
- lib/jsi/schema_classes.rb
Overview
this module is a namespace for building schema classes and schema modules.
Class Method Summary collapse
-
.accessor_module_for_schema(schema, conflicting_modules:, setters: true) ⇒ Module
private
a module of accessors for described property names of the given schema.
-
.bootstrap_schema_class(modules) ⇒ Class
private
a subclass of MetaschemaNode::BootstrapSchema with the given modules included.
-
.class_for_schemas(schemas, includes:) ⇒ Class subclassing JSI::Base
private
a JSI Schema Class which represents the given schemas.
- .includes_for(instance) ⇒ Set<Module> private
- .module_for_schema(schema) ⇒ Module private
Class Method Details
.accessor_module_for_schema(schema, conflicting_modules:, setters: true) ⇒ Module
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.
a module of accessors for described property names of the given schema. getters are always defined. setters are defined by default.
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/jsi/schema_classes.rb', line 167 def accessor_module_for_schema(schema, conflicting_modules: , setters: true) Schema.ensure_schema(schema) jsi_memoize(:accessor_module_for_schema, schema: schema, conflicting_modules: conflicting_modules, setters: setters) do |schema: , conflicting_modules: , setters: | Module.new do begin define_singleton_method(:inspect) { '(JSI Schema Accessor Module)' } conflicting_instance_methods = conflicting_modules.map do |mod| mod.instance_methods + mod.private_instance_methods end.inject(Set.new, &:|) accessors_to_define = schema.described_object_property_names.select do |name| # must not conflict with any method on a conflicting module Util.ok_ruby_method_name?(name) && !conflicting_instance_methods.any? { |mn| mn.to_s == name } end.to_set.freeze define_singleton_method(:jsi_property_accessors) { accessors_to_define } accessors_to_define.each do |property_name| define_method(property_name) do |**kw| self[property_name, **kw] end if setters define_method("#{property_name}=") do |value| self[property_name] = value end end end end end end end |
.bootstrap_schema_class(modules) ⇒ Class
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.
a subclass of MetaschemaNode::BootstrapSchema with the given modules included
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/jsi/schema_classes.rb', line 107 def bootstrap_schema_class(modules) modules = Util.ensure_module_set(modules) jsi_memoize(__method__, modules: modules) do |modules: | Class.new(MetaschemaNode::BootstrapSchema) do define_singleton_method(:schema_implementation_modules) { modules } define_method(:schema_implementation_modules) { modules } modules.each { |mod| include(mod) } self end end end |
.class_for_schemas(schemas, includes:) ⇒ Class subclassing JSI::Base
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.
a JSI Schema Class which represents the given schemas. an instance of the class is a JSON Schema instance described by all of the given schemas.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/jsi/schema_classes.rb', line 85 def class_for_schemas(schemas, includes: ) schemas = SchemaSet.ensure_schema_set(schemas) includes = Util.ensure_module_set(includes) jsi_memoize(:class_for_schemas, schemas: schemas, includes: includes) do |schemas: , includes: | Class.new(Base).instance_exec(schemas: schemas, includes: includes) do |schemas: , includes: | define_singleton_method(:jsi_class_schemas) { schemas } define_method(:jsi_schemas) { schemas } includes.each { |m| include(m) } schemas.each { |schema| include(schema.jsi_schema_module) } jsi_class = self define_method(:jsi_class) { jsi_class } self end end end |
.includes_for(instance) ⇒ Set<Module>
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.
72 73 74 75 76 77 |
# File 'lib/jsi/schema_classes.rb', line 72 def includes_for(instance) includes = Set[] includes << Base::ArrayNode if instance.respond_to?(:to_ary) includes << Base::HashNode if instance.respond_to?(:to_hash) includes.freeze end |
.module_for_schema(schema) ⇒ Module
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.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/jsi/schema_classes.rb', line 123 def module_for_schema(schema) Schema.ensure_schema(schema) jsi_memoize(:module_for_schema, schema: schema) do |schema: | Module.new do begin define_singleton_method(:schema) { schema } extend SchemaModule schema.jsi_schema_instance_modules.each do |mod| include(mod) end accessor_module = JSI::SchemaClasses.accessor_module_for_schema(schema, conflicting_modules: Set[JSI::Base, JSI::Base::ArrayNode, JSI::Base::HashNode] + schema.jsi_schema_instance_modules, ) include accessor_module define_singleton_method(:jsi_property_accessors) { accessor_module.jsi_property_accessors } @possibly_schema_node = schema extend(SchemaModulePossibly) schema.jsi_schemas.each do |schema_schema| extend JSI::SchemaClasses.accessor_module_for_schema(schema_schema, conflicting_modules: Set[Module, SchemaModule, SchemaModulePossibly], setters: false, ) end end end end end |