Module: JSI::SchemaClasses
- Extended by:
- Memoize
- Defined in:
- lib/jsi/schema_classes.rb
Overview
this module is just a namespace for schema classes.
Class Method Summary collapse
-
.accessor_module_for_schema(schema, conflicting_modules:) ⇒ Module
A module of accessors (setters and getters) for described property names of the given schema.
-
.class_for_schema(schema_object) ⇒ Object
see class_for_schema.
-
.module_for_schema(schema_object) ⇒ Object
a module for the given schema, with accessor methods for any object property names the schema identifies (see JSI::Schema#described_object_property_names).
Methods included from Memoize
Class Method Details
.accessor_module_for_schema(schema, conflicting_modules:) ⇒ Module
Returns a module of accessors (setters and getters) for described property names of the given schema.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/jsi/schema_classes.rb', line 80 def accessor_module_for_schema(schema, conflicting_modules: ) jsi_memoize(:accessor_module_for_schema, schema, conflicting_modules) do |schema, conflicting_modules| Module.new.tap do |m| m.module_eval do conflicting_instance_methods = (conflicting_modules + [m]).map do |mod| mod.instance_methods + mod.private_instance_methods end.inject(Set.new, &:|) accessors_to_define = schema.described_object_property_names.map(&:to_s) - conflicting_instance_methods.map(&:to_s) accessors_to_define.each do |property_name| define_method(property_name) do self[property_name] end define_method("#{property_name}=") do |value| self[property_name] = value end end end end end end |
.class_for_schema(schema_object) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/jsi/schema_classes.rb', line 35 def class_for_schema(schema_object) jsi_memoize(:class_for_schema, JSI::Schema.from_object(schema_object)) do |schema| Class.new(Base).instance_exec(schema) do |schema| define_singleton_method(:schema) { schema } define_method(:schema) { schema } include(schema.jsi_schema_module) jsi_class = self define_method(:jsi_class) { jsi_class } self end end end |
.module_for_schema(schema_object) ⇒ Object
a module for the given schema, with accessor methods for any object property names the schema identifies (see JSI::Schema#described_object_property_names).
defines a singleton method #schema to access the JSI::Schema this module represents, and extends the module with JSI::SchemaModule.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/jsi/schema_classes.rb', line 55 def module_for_schema(schema_object) schema = JSI::Schema.from_object(schema_object) jsi_memoize(:module_for_schema, schema) do |schema| Module.new.tap do |m| m.module_eval do define_singleton_method(:schema) { schema } extend SchemaModule include JSI::SchemaClasses.accessor_module_for_schema(schema, conflicting_modules: [JSI::Base, JSI::BaseArray, JSI::BaseHash]) @possibly_schema_node = schema extend(SchemaModulePossibly) extend(JSI::SchemaClasses.accessor_module_for_schema(schema.schema, conflicting_modules: [Module, SchemaModule, SchemaModulePossibly])) end end end end |