Class: JSI::SchemaModule::Connection

Inherits:
Module
  • Object
show all
Defined in:
lib/jsi/schema_classes.rb,
lib/jsi/schema_classes.rb

Overview

A JSI Schema Module is a module which represents a schema. A SchemaModule::Connection represents a node in a schema's document which is not a schema, such as the 'properties' object (which contains schemas but is not a schema).

instances of this class act as a stand-in to allow users to subscript or call property accessors on schema modules to refer to their subschemas' schema modules.

A SchemaModule::Connection has readers for property names described by the node's schemas.

This class subclasses Module only so that it can be named, to identify schemas descendent of its node. No object is ever expected to be an instance of a SchemaModule::Connection module.

Direct Known Subclasses

JSI::SchemaModule

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ Connection



389
390
391
392
393
394
395
396
# File 'lib/jsi/schema_classes.rb', line 389

def initialize(node)
  fail(Bug, "node must be JSI::Base: #{node.pretty_inspect.chomp}") unless node.is_a?(JSI::Base)
  @jsi_node = node
  node.jsi_schemas.each do |schema|
    extend(JSI::SchemaClasses.schema_property_reader_module(schema, conflicting_modules: [SchemaModule::Connection]))
  end
  node.jsi_schema_module_connection_created(self)
end

Instance Attribute Details

#jsi_nodeObject (readonly)

Returns the value of attribute jsi_node.



306
307
308
# File 'lib/jsi/schema_classes.rb', line 306

def jsi_node
  @jsi_node
end

Instance Method Details

#/(ptr) ⇒ SchemaModule::Connection

See Base#/ - descendent's Base#jsi_schema_module_connection



335
336
337
# File 'lib/jsi/schema_classes.rb', line 335

def /(ptr)
  (jsi_node / ptr).jsi_schema_module_connection
end

#[](token, **kw) { ... } ⇒ SchemaModule, ...

Subscripting a JSI schema module or a JSI::SchemaModule::Connection will subscript its node, and if the result is a JSI::Schema, return the JSI Schema module of that schema; if it is a JSI::Base, return a SchemaModule::Connection; or if it is another value (a simple type), return that value.

Yields:

  • If the token identifies a schema and a block is given, it is evaluated in the context of the schema's JSI schema module using Module#module_exec.

Raises:

  • (ArgumentError)


348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/jsi/schema_classes.rb', line 348

def [](token, **kw, &block)
  raise(ArgumentError) unless kw.empty? # TODO remove eventually (keyword argument compatibility)
  @jsi_node.jsi_child_ensure_present(token)
  sub = @jsi_node[token]
  if sub.is_a?(JSI::Schema)
    sub.jsi_schema_module_exec(&block) if block
    sub.jsi_schema_module
  elsif block
    raise(BlockGivenError, "block given but token #{token.inspect} does not identify a schema")
  elsif sub.is_a?(JSI::Base)
    sub.jsi_schema_module_connection
  else
    sub
  end
end

#inspectString



399
400
401
402
403
404
405
# File 'lib/jsi/schema_classes.rb', line 399

def inspect
  if name_from_ancestor
    -"#{name_from_ancestor} (#{self.class})"
  else
    -"(#{self.class}: #{@jsi_node.jsi_ptr.uri})"
  end
end

#name_from_ancestorString?

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 name relative to a named schema module of an ancestor schema. for example, if Foos = JSI::JSONSchemaDraft07.new_schema_module({'items' => {}}) then the module Foos.items will have a name_from_ancestor of "Foos.items"



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/jsi/schema_classes.rb', line 313

def name_from_ancestor
  named_ancestor, tokens = named_ancestor_tokens
  return nil unless named_ancestor

  name = named_ancestor.jsi_schema_module_connection.name
  ancestor = named_ancestor
  tokens.each do |token|
    if ancestor.jsi_property_readers.include?(token)
      name += ".#{token}"
    elsif [String, Numeric, TrueClass, FalseClass, NilClass].any? { |m| token.is_a?(m) }
      name += "[#{token.inspect}]"
    else
      return nil
    end
    ancestor = ancestor[token]
  end
  name.freeze
end

#to_sObject



407
408
409
# File 'lib/jsi/schema_classes.rb', line 407

def to_s
  inspect
end