Class: JSI::Base

Inherits:
Object
  • Object
show all
Includes:
Schema::SchemaAncestorNode
Defined in:
lib/jsi/base.rb

Overview

A JSI::Base instance represents a node in a JSON document (its #jsi_document) at a particular location (its #jsi_ptr), described by any number of JSON Schemas (its #jsi_schemas).

JSI::Base is an abstract base class. The subclasses used to instantiate JSIs are dynamically created as needed for a given instance.

These subclasses are generally intended to be ignored by applications using this library - the purpose they serve is to include modules relevant to the instance. The modules these classes include are:

Direct Known Subclasses

MetaSchemaNode

Defined Under Namespace

Modules: ArrayNode, HashNode, Immutable, Mutable, StringNode Classes: SimpleNodeChildError

Instance Attribute Summary collapse

Attributes included from Schema::SchemaAncestorNode

#jsi_schema_base_uri, #jsi_schema_registry, #jsi_schema_resource_ancestors

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Schema::SchemaAncestorNode

#jsi_anchor_subschema, #jsi_anchor_subschemas, #jsi_resource_ancestor_uri

Constructor Details

#initialize(jsi_document, jsi_ptr: , jsi_indicated_schemas:, jsi_schema_base_uri: nil, jsi_schema_resource_ancestors: Util::EMPTY_ARY, jsi_schema_registry:, jsi_content_to_immutable:, jsi_root_node: nil) ⇒ Base

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.

initializes a JSI whose instance is in the given document at the given pointer.

this is a private api - users should look elsewhere to instantiate JSIs, in particular:

Parameters:

  • jsi_document (Object)

    the document containing the instance

  • jsi_ptr (JSI::Ptr) (defaults to: )

    a pointer pointing to the JSI's instance in the document

  • jsi_schema_base_uri (Addressable::URI) (defaults to: nil)

    see SchemaSet#new_jsi param uri

  • jsi_schema_resource_ancestors (Array<JSI::Base + JSI::Schema>) (defaults to: Util::EMPTY_ARY)
  • jsi_root_node (JSI::Base) (defaults to: nil)

    the JSI of the root of the document containing this JSI



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
143
144
145
146
147
148
149
# File 'lib/jsi/base.rb', line 114

def initialize(jsi_document,
    jsi_ptr: Ptr[],
    jsi_indicated_schemas: ,
    jsi_schema_base_uri: nil,
    jsi_schema_resource_ancestors: Util::EMPTY_ARY,
    jsi_schema_registry: ,
    jsi_content_to_immutable: ,
    jsi_root_node: nil
)
  #chkbug raise(Bug, "no #jsi_schemas") unless respond_to?(:jsi_schemas)

  self.jsi_document = jsi_document
  self.jsi_ptr = jsi_ptr
  self.jsi_indicated_schemas = jsi_indicated_schemas
  self.jsi_schema_base_uri = jsi_schema_base_uri
  self.jsi_schema_resource_ancestors = jsi_schema_resource_ancestors
  self.jsi_schema_registry = jsi_schema_registry
  @jsi_content_to_immutable = jsi_content_to_immutable
  if @jsi_ptr.root?
    #chkbug raise(Bug, "jsi_root_node specified for root JSI") if jsi_root_node
    @jsi_root_node = self
  else
    #chkbug raise(Bug, "jsi_root_node is not JSI::Base") if !jsi_root_node.is_a?(JSI::Base)
    #chkbug raise(Bug, "jsi_root_node ptr is not root") if !jsi_root_node.jsi_ptr.root?
    @jsi_root_node = jsi_root_node
  end

  jsi_memomaps_initialize
  jsi_mutability_initialize

  super()

  if jsi_instance.is_a?(JSI::Base)
    raise(TypeError, "a JSI::Base instance must not be another JSI::Base. received: #{jsi_instance.pretty_inspect.chomp}")
  end
end

Instance Attribute Details

#jsi_content_to_immutable#call? (readonly)

Comes from the param to_immutable of SchemaSet#new_jsi (or other new_jsi / new_schema / new_schema_module method). Immutable JSIs use this when instantiating a modified copy so its instance is also immutable.

Returns:

  • (#call, nil)


170
171
172
# File 'lib/jsi/base.rb', line 170

def jsi_content_to_immutable
  @jsi_content_to_immutable
end

#jsi_documentObject (readonly)

document containing the instance of this JSI at our #jsi_ptr



160
161
162
# File 'lib/jsi/base.rb', line 160

def jsi_document
  @jsi_document
end

#jsi_indicated_schemasJSI::SchemaSet

the schemas indicated as describing this instance, prior to inplace application.

this is different from #jsi_schemas, which are the inplace applicator schemas which describe this instance. for most purposes, #jsi_schemas is more relevant.

jsi_indicated_schemas does not include inplace applicator schemas, such as the subschemas of allOf, whereas #jsi_schemas does.

this does include indicated schemas which do not apply themselves, such as $ref schemas (on json schema drafts up to 7) - these are not included on #jsi_schemas.

Returns:



200
201
202
# File 'lib/jsi/base.rb', line 200

def jsi_indicated_schemas
  @jsi_indicated_schemas
end

#jsi_ptrJSI::Ptr (readonly)

Ptr pointing to this JSI's instance within our #jsi_document

Returns:



164
165
166
# File 'lib/jsi/base.rb', line 164

def jsi_ptr
  @jsi_ptr
end

#jsi_root_nodeJSI::Base (readonly)

the JSI at the root of this JSI's document

Returns:



174
175
176
# File 'lib/jsi/base.rb', line 174

def jsi_root_node
  @jsi_root_node
end

Class Method Details

.inspectString

A string indicating the schema module name and/or schema URI of each schema the class represents.

Returns:

  • (String)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/jsi/base.rb', line 35

def inspect
  if !respond_to?(:jsi_class_schemas)
    super
  else
    schema_names = jsi_class_schemas.map do |schema|
      mod_name = schema.jsi_schema_module.name_from_ancestor
      if mod_name && schema.schema_absolute_uri
        "#{mod_name} <#{schema.schema_absolute_uri}>"
      elsif mod_name
        mod_name
      elsif schema.schema_uri
        schema.schema_uri.to_s
      else
        schema.jsi_ptr.uri.to_s
      end
    end

    if schema_names.empty?
      "(JSI Schema Class for 0 schemas#{jsi_class_includes.map { |n| " + #{n}" }})"
    else
      -"(JSI Schema Class: #{(schema_names + jsi_class_includes.map(&:name)).join(' + ')})"
    end
  end
end

.nameString

A constant name of this class. This is generated from any schema module name or URI of each schema this class represents, or random characters.

this generated name is not too pretty but can be more helpful than an anonymous class, especially in error messages.

Returns:

  • (String)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/jsi/base.rb', line 71

def name
  return super if instance_variable_defined?(:@tried_to_name)
  @tried_to_name = true
  return super unless respond_to?(:jsi_class_schemas)
  alnum = proc { |id| (id % 36**4).to_s(36).rjust(4, '0').upcase }
  schema_names = jsi_class_schemas.map do |schema|
    named_ancestor_schema, tokens = schema.jsi_schema_module.send(:named_ancestor_schema_tokens)
    if named_ancestor_schema
      [named_ancestor_schema.jsi_schema_module.name, *tokens].join('_')
    elsif schema.schema_uri
      schema.schema_uri.to_s
    else
      [alnum[schema.jsi_root_node.__id__], *schema.jsi_ptr.tokens].join('_')
    end
  end
  includes_names = jsi_class_includes.map { |m| m.name.sub(/\AJSI::Base::/, '').gsub(Util::RUBY_REJECT_NAME_RE, '_') }
  if schema_names.any?
    parts = schema_names.compact.sort.map { |n| 'X' + n.to_s }
    parts += includes_names
    const_name = Util.const_name_from_parts(parts, join: '__')
    const_name += "__" + alnum[__id__] if SchemaClasses.const_defined?(const_name)
  else
    const_name = (['X' + alnum[__id__]] + includes_names).join('__')
  end
  # collisions are technically possible though vanishingly unlikely
  SchemaClasses.const_set(const_name, self) unless SchemaClasses.const_defined?(const_name)
  super
end

.to_sObject



60
61
62
# File 'lib/jsi/base.rb', line 60

def to_s
  inspect
end

Instance Method Details

#/(ptr) ⇒ JSI::Base

A shorthand alias for #jsi_descendent_node.

Note that, though more convenient to type, using an operator whose meaning may not be intuitive to a reader could impair readability of code.

examples:

my_jsi / ['foo', 'bar']
my_jsi / %w(foo bar)
my_schema / JSI::Ptr['additionalProperties']
my_schema / %w(properties foo items additionalProperties)

Parameters:

Returns:



347
348
349
# File 'lib/jsi/base.rb', line 347

def /(ptr)
  jsi_descendent_node(ptr)
end

#[](token, as_jsi: jsi_child_as_jsi_default, use_default: jsi_child_use_default_default) ⇒ JSI::Base, ...

subscripts to return a child value identified by the given token.

Parameters:

  • token (String, Integer, Object)

    an array index or hash key (JSON object property name) of the instance identifying the child value

  • as_jsi (:auto, true, false) (defaults to: jsi_child_as_jsi_default)

    (default is :auto) Whether to return the child as a JSI. One of:

    • :auto: By default a JSI will be returned when either:

      • the result is a complex value (responds to #to_ary or #to_hash)
      • the result is a schema (including true/false schemas)

    The plain content is returned when it is a simple type.

    • true: the result value will always be returned as a JSI. the #jsi_schemas of the result may be empty if no schemas describe the instance.
    • false: the result value will always be the plain instance.

    note that nil is returned (regardless of as_jsi) when there is no value to return because the token is not a hash key or array index of the instance and no default value applies. (one exception is when this JSI's instance is a Hash with a default or default_proc, which has unspecified behavior.)

  • use_default (true, false) (defaults to: jsi_child_use_default_default)

    (default is false) Whether to return a schema default value when the token refers to a child that is not in the document. If the token is not an array index or hash key of the instance, and one schema for the child instance specifies a default value, that default is returned.

    if the result with the default value is a JSI (per the as_jsi param), that JSI is not a child of this JSI - this JSI is not modified to fill in the default value. the result is a JSI within a new document containing the filled-in default.

    if the child instance's schemas do not indicate a single default value (that is, if zero or multiple defaults are specified across those schemas), nil is returned. (one exception is when this JSI's instance is a Hash with a default or default_proc, which has unspecified behavior.)

Returns:

  • (JSI::Base, Object, nil)

    the child value identified by the subscript token



490
491
492
493
# File 'lib/jsi/base.rb', line 490

def [](token, as_jsi: jsi_child_as_jsi_default, use_default: jsi_child_use_default_default)
  # note: overridden by Base::HashNode, Base::ArrayNode
  jsi_simple_node_child_error(token)
end

#[]=(token, value) ⇒ Object

assigns the subscript of the instance identified by the given token to the given value. if the value is a JSI, its instance is assigned instead of the JSI value itself.

Parameters:

  • token (String, Integer, Object)

    token identifying the subscript to assign

  • value (JSI::Base, Object)

    the value to be assigned



513
514
515
516
517
518
519
520
521
522
# File 'lib/jsi/base.rb', line 513

def []=(token, value)
  unless jsi_array? || jsi_hash?
    jsi_simple_node_child_error(token)
  end
  if value.is_a?(Base)
    self[token] = value.jsi_instance
  else
    jsi_instance[token] = value
  end
end

#as_json(options = {}) ⇒ Object

A structure coerced to JSONifiable types from the instance content. Calls Util#as_json with the instance and any given options.



688
689
690
# File 'lib/jsi/base.rb', line 688

def as_json(options = {})
  Util.as_json(jsi_instance, **options)
end

#described_by?(schema) ⇒ Boolean

Is this JSI described by the given schema (or schema module)?

Parameters:

Returns:

  • (Boolean)


534
535
536
537
538
539
540
541
542
# File 'lib/jsi/base.rb', line 534

def described_by?(schema)
  if schema.is_a?(Schema)
    jsi_schemas.include?(schema)
  elsif schema.is_a?(SchemaModule)
    jsi_schema_modules.include?(schema)
  else
    raise(TypeError, "expected a Schema or Schema Module; got: #{schema.pretty_inspect.chomp}")
  end
end

#dupObject



632
633
634
# File 'lib/jsi/base.rb', line 632

def dup
  jsi_modified_copy(&:dup)
end

#inspectString

a string representing this JSI, indicating any named schemas and inspecting its instance

Returns:

  • (String)


638
639
640
# File 'lib/jsi/base.rb', line 638

def inspect
  -"\#<#{jsi_object_group_text.join(' ')} #{jsi_instance.inspect}>"
end

#jmespath_search(expression, **runtime_options) ⇒ Array, ...

queries this JSI using the JMESPath Ruby gem. see https://jmespath.org/ to learn the JMESPath query language.

the JMESPath gem is not a dependency of JSI, so must be installed / added to your Gemfile to use. e.g. gem 'jmespath', '~> 1.5'. note that versions below 1.5 are not compatible with JSI.

Parameters:

  • expression (String)

    a JMESPath expression

  • runtime_options

    passed to JMESPath.search, though no runtime_options are publicly documented or normally used.

Returns:



626
627
628
629
630
# File 'lib/jsi/base.rb', line 626

def jmespath_search(expression, **runtime_options)
  Util.require_jmespath

  JMESPath.search(expression, self, **runtime_options)
end

#jsi_ancestor_nodesArray<JSI::Base>

ancestor JSI instances from this node up to the root. this node itself is always its own first ancestor.

Returns:



312
313
314
315
316
317
318
319
320
321
322
# File 'lib/jsi/base.rb', line 312

def jsi_ancestor_nodes
  ancestors = []
  ancestor = jsi_root_node
  ancestors << ancestor

  jsi_ptr.tokens.each do |token|
    ancestor = ancestor.jsi_child_node(token)
    ancestors << ancestor
  end
  ancestors.reverse!.freeze
end

#jsi_array?Boolean

Is the instance an array?

An array is typically an instance of the Array class but may be an object that supports implicit conversion with a #to_ary method.

Returns:

  • (Boolean)


579
580
581
582
# File 'lib/jsi/base.rb', line 579

def jsi_array?
  # note: overridden by Base::ArrayNode
  false
end

#jsi_child_as_jsi_default:auto, ...

The default value for the param as_jsi of #[], controlling whether a child is returned as a JSI instance.

Returns:

  • (:auto, true, false)

    a valid value of the as_jsi param of #[]



497
498
499
# File 'lib/jsi/base.rb', line 497

def jsi_child_as_jsi_default
  :auto
end

#jsi_child_token_in_range?(token) ⇒ Boolean

Does the given token identify a child of this node?

In other words, is the given token an array index or hash key of the instance?

Always false if this is not a complex node.

Parameters:

  • token (String, Integer)

Returns:

  • (Boolean)


370
371
372
373
# File 'lib/jsi/base.rb', line 370

def jsi_child_token_in_range?(token)
  # note: overridden by Base::HashNode, Base::ArrayNode
  false
end

#jsi_child_use_default_defaulttrue, false

The default value for the param use_default of #[], controlling whether a schema default value is returned when a token refers to a child that is not in the document.

Returns:

  • (true, false)

    a valid value of the use_default param of #[]



504
505
506
# File 'lib/jsi/base.rb', line 504

def jsi_child_use_default_default
  false
end

#jsi_descendent_node(ptr) ⇒ JSI::Base

the descendent node at the given pointer

Parameters:

Returns:



328
329
330
331
# File 'lib/jsi/base.rb', line 328

def jsi_descendent_node(ptr)
  descendent = Ptr.ary_ptr(ptr).evaluate(self, as_jsi: true)
  descendent
end

#jsi_each_child_token {|String, Integer| ... } ⇒ nil, Enumerator

yields each token (array index or hash key) identifying a child node. yields nothing if this node is not complex or has no children.

Yields:

  • (String, Integer)

    each child token

Returns:

  • (nil, Enumerator)

    an Enumerator if invoked without a block; otherwise nil



356
357
358
359
360
# File 'lib/jsi/base.rb', line 356

def jsi_each_child_token
  # note: overridden by Base::HashNode, Base::ArrayNode
  return to_enum(__method__) { 0 } unless block_given?
  nil
end

#jsi_each_descendent_node(propertyNames: false) {|JSI::Base| ... } ⇒ nil, Enumerator

yields a JSI of each node at or below this one in this JSI's document.

Parameters:

  • propertyNames (Boolean) (defaults to: false)

    Whether to also yield each object property name (Hash key) of any descendent which is a hash/object. These are described by propertyNames subshemas of that object's schemas. They are not actual descendents of this node. See JSI::Base::HashNode#jsi_each_propertyName.

Yields:

  • (JSI::Base)

    each descendent node, starting with self

Returns:

  • (nil, Enumerator)

    an Enumerator if invoked without a block; otherwise nil



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

def jsi_each_descendent_node(propertyNames: false, &block)
  return to_enum(__method__, propertyNames: propertyNames) unless block

  yield self

  if propertyNames && is_a?(HashNode)
    jsi_each_propertyName do |propertyName|
      propertyName.jsi_each_descendent_node(propertyNames: propertyNames, &block)
    end
  end

  jsi_each_child_token do |token|
    jsi_child_node(token).jsi_each_descendent_node(propertyNames: propertyNames, &block)
  end

  nil
end

#jsi_fingerprintObject

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 Util::Private::FingerprintHash



701
702
703
704
705
706
707
708
709
710
711
712
# File 'lib/jsi/base.rb', line 701

def jsi_fingerprint
  {
    class: JSI::Base,
    jsi_schemas: jsi_schemas,
    jsi_document: jsi_document,
    jsi_ptr: jsi_ptr,
    # for instances in documents with schemas:
    jsi_resource_ancestor_uri: jsi_resource_ancestor_uri,
    # different registries mean references may resolve to different resources so must not be equal
    jsi_schema_registry: jsi_schema_registry,
  }.freeze
end

#jsi_hash?Boolean

Is the instance a ruby Hash (JSON object)?

This is typically an instance of the Hash class but may be an object that supports implicit conversion with a #to_hash method.

Returns:

  • (Boolean)


591
592
593
594
# File 'lib/jsi/base.rb', line 591

def jsi_hash?
  # note: overridden by Base::HashNode
  false
end

#jsi_instanceObject

The JSON schema instance this JSI represents - the underlying JSON data used to instantiate this JSI. The same as #jsi_node_content - 'node content' is usually preferable terminology, to avoid ambiguity in the heavily overloaded term 'instance'.



184
185
186
# File 'lib/jsi/base.rb', line 184

def jsi_instance
  jsi_node_content
end

#jsi_is_schema?Boolean

Is this a JSI Schema?

Returns:

  • (Boolean)


546
547
548
# File 'lib/jsi/base.rb', line 546

def jsi_is_schema?
  false
end

#jsi_modified_copy {|Object| ... } ⇒ JSI::Base subclass

yields the content of this JSI's instance. the block must result in a modified copy of the yielded instance (not modified in place, which would alter this JSI as well) which will be used to instantiate and return a new JSI with the modified content.

the result may have different schemas which describe it than this JSI's schemas, if conditional applicator schemas apply differently to the modified instance.

Yields:

  • (Object)

    this JSI's instance. the block should result in a nondestructively modified copy of this.

Returns:

  • (JSI::Base subclass)

    the modified copy of self



560
561
562
563
564
565
566
567
568
569
570
# File 'lib/jsi/base.rb', line 560

def jsi_modified_copy(&block)
    modified_document = @jsi_ptr.modified_document_copy(@jsi_document, &block)
    modified_jsi_root_node = @jsi_root_node.jsi_indicated_schemas.new_jsi(modified_document,
      uri: @jsi_root_node.jsi_schema_base_uri,
      register: false, # default is already false but this is a place to be explicit
      schema_registry: jsi_schema_registry,
      mutable: jsi_mutable?,
      to_immutable: jsi_content_to_immutable,
    )
    modified_jsi_root_node.jsi_descendent_node(@jsi_ptr)
end

#jsi_mutable?Boolean

Is this JSI mutable?

Returns:

  • (Boolean)


598
599
600
# File 'lib/jsi/base.rb', line 598

def jsi_mutable?
  # note: overridden by Base::Mutable / Immutable
end

#jsi_node_contentObject

the content of this node in our #jsi_document at our #jsi_ptr. the same as #jsi_instance.



177
178
179
# File 'lib/jsi/base.rb', line 177

def jsi_node_content
  # stub method for doc, overridden by Mutable/Immutable
end

#jsi_node_content_child(token) ⇒ Object?

The child of the #jsi_node_content identified by the given token, or nil if the token does not identify an existing child.

In other words, the element of the instance array at the given index, or the value of the instance hash/object for the given key.

Returns:

  • (Object, nil)

Raises:



383
384
385
386
# File 'lib/jsi/base.rb', line 383

def jsi_node_content_child(token)
  # note: overridden by Base::HashNode, Base::ArrayNode
  jsi_simple_node_child_error(token)
end

#jsi_parent_nodeJSI::Base?

the immediate parent of this JSI. nil if there is no parent.

Returns:



305
306
307
# File 'lib/jsi/base.rb', line 305

def jsi_parent_node
  jsi_ptr.root? ? nil : jsi_root_node.jsi_descendent_node(jsi_ptr.parent)
end

#jsi_parent_nodesArray<JSI::Base>

an array of JSI instances above this one in the document.

Returns:



292
293
294
295
296
297
298
299
300
# File 'lib/jsi/base.rb', line 292

def jsi_parent_nodes
  parent = jsi_root_node

  jsi_ptr.tokens.map do |token|
    parent.tap do
      parent = parent.jsi_child_node(token)
    end
  end.reverse!.freeze
end

#jsi_schema_modulesSet<Module>

the set of JSI schema modules corresponding to the schemas that describe this JSI

Returns:

  • (Set<Module>)


526
527
528
# File 'lib/jsi/base.rb', line 526

def jsi_schema_modules
  Util.ensure_module_set(jsi_schemas.map(&:jsi_schema_module))
end

#jsi_schemasJSI::SchemaSet

The set of schemas that describe this instance. These are the applicator schemas that apply to this instance, the result of inplace application of our #jsi_indicated_schemas.

Returns:



# File 'lib/jsi/base.rb', line 151

#jsi_select_descendents_leaf_first {|JSI::Base| ... } ⇒ JSI::Base

recursively selects descendent nodes of this JSI, returning a modified copy of self containing only descendent nodes for which the given block had a true-ish result.

this method recursively descends child nodes before yielding each node, so leaf nodes are yielded before their parents.

Yields:

  • (JSI::Base)

    each descendent node below self

Returns:

  • (JSI::Base)

    modified copy of self containing only the selected nodes



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/jsi/base.rb', line 267

def jsi_select_descendents_leaf_first(&block)
  jsi_modified_copy do |instance|
    if jsi_array? || jsi_hash?
      res = instance.class.new
      jsi_each_child_token do |token|
        v = jsi_child_node(token).jsi_select_descendents_leaf_first(&block)
        if yield(v)
          res_v = v.jsi_node_content
          if jsi_array?
            res << res_v
          else
            res[token] = res_v
          end
        end
      end
      res
    else
      instance
    end
  end
end

#jsi_select_descendents_node_first {|JSI::Base| ... } ⇒ JSI::Base

recursively selects descendent nodes of this JSI, returning a modified copy of self containing only descendent nodes for which the given block had a true-ish result.

this method yields a node before recursively descending to its child nodes, so leaf nodes are yielded last, after their parents. if a node is not selected, its descendents are never recursed.

Yields:

  • (JSI::Base)

    each descendent node below self

Returns:

  • (JSI::Base)

    modified copy of self containing only the selected nodes



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/jsi/base.rb', line 237

def jsi_select_descendents_node_first(&block)
  jsi_modified_copy do |instance|
    if jsi_array? || jsi_hash?
      res = instance.class.new
      jsi_each_child_token do |token|
        v = jsi_child_node(token)
        if yield(v)
          res_v = v.jsi_select_descendents_node_first(&block).jsi_node_content
          if jsi_array?
            res << res_v
          else
            res[token] = res_v
          end
        end
      end
      res
    else
      instance
    end
  end
end

#jsi_valid?Boolean

whether this JSI's instance is valid against all of its schemas

Returns:

  • (Boolean)


611
612
613
# File 'lib/jsi/base.rb', line 611

def jsi_valid?
  jsi_indicated_schemas.instance_valid?(self)
end

#jsi_validateJSI::Validation::FullResult

validates this JSI's instance against its schemas



605
606
607
# File 'lib/jsi/base.rb', line 605

def jsi_validate
  jsi_indicated_schemas.instance_validate(self)
end

#pretty_print(q)

This method returns an undefined value.

pretty-prints a representation of this JSI to the given printer



648
649
650
651
652
653
654
655
656
657
# File 'lib/jsi/base.rb', line 648

def pretty_print(q)
  q.text '#<'
  q.text jsi_object_group_text.join(' ')
  q.group(2) {
      q.breakable ' '
      q.pp jsi_instance
  }
  q.breakable ''
  q.text '>'
end

#to_json(options = {}) ⇒ String

A JSON encoded string of the instance content. Calls Util#to_json with the instance and any given options.

Returns:

  • (String)


695
696
697
# File 'lib/jsi/base.rb', line 695

def to_json(options = {})
  Util.to_json(jsi_instance, **options)
end

#to_sObject



642
643
644
# File 'lib/jsi/base.rb', line 642

def to_s
  inspect
end