Module: JSI::SchemaClasses

Extended by:
Util::Memoize
Defined in:
lib/jsi/schema_classes.rb

Overview

this module is just a namespace for schema classes.

Class Method Summary collapse

Methods included from Util::Memoize

jsi_clear_memo, jsi_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.

Parameters:

  • schema (JSI::Schema)

    a schema for which to define accessors for any described property names

  • conflicting_modules (Enumerable<Module>)

    an array of modules (or classes) which may be used alongside the accessor module. methods defined by any conflicting_module will not be defined as accessors.

Returns:

  • (Module)

    a module of accessors (setters and getters) for described property names of the given schema



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/jsi/schema_classes.rb', line 82

def accessor_module_for_schema(schema, conflicting_modules: )
  unless schema.is_a?(JSI::Schema)
    raise(JSI::Schema::NotASchemaError, "not a schema: #{schema.pretty_inspect.chomp}")
  end
  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_schemas(schema_objects) ⇒ 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_schemas(schema_objects)
  schemas = schema_objects.map { |schema_object| JSI::Schema.from_object(schema_object) }.to_set
  jsi_memoize(:class_for_schemas, schemas) do |schemas|
    Class.new(Base).instance_exec(schemas) do |schemas|
      define_singleton_method(:jsi_class_schemas) { schemas }
      define_method(:jsi_schemas) { schemas }
      schemas.each { |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
73
74
# 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::PathedArrayNode, JSI::PathedHashNode])

        @possibly_schema_node = schema
        extend(SchemaModulePossibly)
        schema.jsi_schemas.each do |schema_schema|
          extend(JSI::SchemaClasses.accessor_module_for_schema(schema_schema, conflicting_modules: [Module, SchemaModule, SchemaModulePossibly]))
        end
      end
    end
  end
end