Module: JSI::SchemaModulePossibly

Included in:
NotASchemaModule
Defined in:
lib/jsi/schema_classes.rb

Overview

a JSI Schema module and a JSI::NotASchemaModule are both a SchemaModulePossibly. this module provides a #[] method.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#possibly_schema_nodeObject (readonly)

Returns the value of attribute possibly_schema_node.



205
206
207
# File 'lib/jsi/schema_classes.rb', line 205

def possibly_schema_node
  @possibly_schema_node
end

Instance Method Details

#[](token, **kw) ⇒ Module, ...

subscripting a JSI schema module or a NotASchemaModule will subscript the 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 NotASchemaModule; or if it is another value (a basic type), return that value.

Parameters:

  • token (Object)

Returns:

Raises:

  • (ArgumentError)


237
238
239
240
241
242
243
244
245
246
247
# File 'lib/jsi/schema_classes.rb', line 237

def [](token, **kw)
  raise(ArgumentError) unless kw.empty? # TODO remove eventually (keyword argument compatibility)
  sub = @possibly_schema_node[token]
  if sub.is_a?(JSI::Schema)
    sub.jsi_schema_module
  elsif sub.is_a?(JSI::Base)
    NotASchemaModule.new(sub)
  else
    sub
  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::JSONSchemaOrgDraft07.new_schema_module({'items' => {}}) then the module Foos.items will have a name_from_ancestor of "Foos.items"

Returns:

  • (String, nil)


212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/jsi/schema_classes.rb', line 212

def name_from_ancestor
  named_ancestor_schema, tokens = named_ancestor_schema_tokens
  return nil unless named_ancestor_schema

  name = named_ancestor_schema.jsi_schema_module.name
  ancestor = named_ancestor_schema
  tokens.each do |token|
    if ancestor.jsi_schemas.any? { |s| s.jsi_schema_module.jsi_property_accessors.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
end