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

Class Method Details

.accessor_module_for_schema(schema, conflicting_modules:, setters: true) ⇒ Module

a module of accessors for described property names of the given schema. getters are always defined. setters are defined by default.

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.

  • setters (Boolean) (defaults to: true)

    whether to define setter methods

Returns:

  • (Module)

152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/jsi/schema_classes.rb', line 152

def accessor_module_for_schema(schema, conflicting_modules: , setters: true)
  Schema.ensure_schema(schema)
  jsi_memoize(:accessor_module_for_schema, schema, conflicting_modules, setters) do |schema, conflicting_modules, setters|
    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.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 |*a|
            self[property_name, *a]
          end
          if setters
            define_method("#{property_name}=") do |value|
              self[property_name] = value
            end
          end
        end
      end
    end
  end
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.

see JSI::Schema#jsi_schema_module

Returns:

  • (Module)

106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/jsi/schema_classes.rb', line 106

def module_for_schema(schema)
  Schema.ensure_schema(schema)
  jsi_memoize(:module_for_schema, schema) do |schema|
    Module.new.tap do |m|
      m.module_eval do
        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::PathedArrayNode, JSI::PathedHashNode] +
            schema.jsi_schema_instance_modules,
        )
        include accessor_module

        define_singleton_method(:jsi_property_accessors) { accessor_module.jsi_property_accessors }

        if schema.describes_schema?
          extend DescribesSchemaModule
        end

        @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