Class: JSI::Base
- Inherits:
-
Object
- Object
- JSI::Base
- 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:
- the Schema#jsi_schema_module of each schema which describes the instance
- HashNode, ArrayNode, or StringNode if the instance is a hash/object, array, or string
- Modules defining accessor methods for property names described by the schemas
Direct Known Subclasses
Defined Under Namespace
Modules: ArrayNode, HashNode, Immutable, Mutable, StringNode Classes: SimpleNodeChildError
Instance Attribute Summary collapse
-
#jsi_content_to_immutable ⇒ #call?
readonly
Comes from the param
to_immutable
of SchemaSet#new_jsi (or othernew_jsi
/new_schema
/new_schema_module
method). -
#jsi_document ⇒ Object
readonly
document containing the instance of this JSI at our #jsi_ptr.
-
#jsi_indicated_schemas ⇒ JSI::SchemaSet
readonly
the schemas indicated as describing this instance, prior to inplace application.
-
#jsi_ptr ⇒ JSI::Ptr
readonly
Ptr pointing to this JSI's instance within our #jsi_document.
-
#jsi_root_node ⇒ JSI::Base
readonly
the JSI at the root of this JSI's document.
Attributes included from Schema::SchemaAncestorNode
#jsi_schema_base_uri, #jsi_schema_registry, #jsi_schema_resource_ancestors
Class Method Summary collapse
-
.inspect ⇒ String
A string indicating the schema module name and/or schema URI of each schema the class represents.
-
.name ⇒ String
A constant name of this class.
- .to_s ⇒ Object
Instance Method Summary collapse
-
#/(ptr) ⇒ JSI::Base
A shorthand alias for #jsi_descendent_node.
-
#[](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.
-
#[]=(token, value) ⇒ Object
assigns the subscript of the instance identified by the given token to the given value.
-
#as_json(options = {}) ⇒ Object
A structure coerced to JSONifiable types from the instance content.
-
#described_by?(schema) ⇒ Boolean
Is this JSI described by the given schema (or schema module)?.
- #dup ⇒ Object
-
#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
constructor
private
initializes a JSI whose instance is in the given document at the given pointer.
-
#inspect ⇒ String
a string representing this JSI, indicating any named schemas and inspecting its instance.
-
#jmespath_search(expression, **runtime_options) ⇒ Array, ...
queries this JSI using the JMESPath Ruby gem.
-
#jsi_ancestor_nodes ⇒ Array<JSI::Base>
ancestor JSI instances from this node up to the root.
-
#jsi_array? ⇒ Boolean
Is the instance an array?.
-
#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. -
#jsi_child_token_in_range?(token) ⇒ Boolean
Does the given token identify a child of this node?.
-
#jsi_child_use_default_default ⇒ true, 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. -
#jsi_descendent_node(ptr) ⇒ JSI::Base
the descendent node at the given pointer.
-
#jsi_each_child_token {|String, Integer| ... } ⇒ nil, Enumerator
yields each token (array index or hash key) identifying a child node.
-
#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.
- #jsi_fingerprint ⇒ Object private
-
#jsi_hash? ⇒ Boolean
Is the instance a ruby Hash (JSON object)?.
-
#jsi_instance ⇒ Object
The JSON schema instance this JSI represents - the underlying JSON data used to instantiate this JSI.
-
#jsi_is_schema? ⇒ Boolean
Is this a JSI Schema?.
-
#jsi_modified_copy {|Object| ... } ⇒ JSI::Base subclass
yields the content of this JSI's instance.
-
#jsi_mutable? ⇒ Boolean
Is this JSI mutable?.
-
#jsi_node_content ⇒ Object
the content of this node in our #jsi_document at our #jsi_ptr.
-
#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. -
#jsi_parent_node ⇒ JSI::Base?
the immediate parent of this JSI.
-
#jsi_parent_nodes ⇒ Array<JSI::Base>
an array of JSI instances above this one in the document.
-
#jsi_schema_modules ⇒ Set<Module>
the set of JSI schema modules corresponding to the schemas that describe this JSI.
-
#jsi_schemas ⇒ JSI::SchemaSet
The set of schemas that describe this instance.
-
#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.
-
#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.
-
#jsi_valid? ⇒ Boolean
whether this JSI's instance is valid against all of its schemas.
-
#jsi_validate ⇒ JSI::Validation::FullResult
validates this JSI's instance against its schemas.
-
#pretty_print(q)
pretty-prints a representation of this JSI to the given printer.
-
#to_json(options = {}) ⇒ String
A JSON encoded string of the instance content.
- #to_s ⇒ Object
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:
- JSI.new_schema and Schema::MetaSchema#new_schema to instantiate schemas
- Schema#new_jsi to instantiate schema instances
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.
170 171 172 |
# File 'lib/jsi/base.rb', line 170 def jsi_content_to_immutable @jsi_content_to_immutable end |
#jsi_document ⇒ Object (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_schemas ⇒ JSI::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
.
200 201 202 |
# File 'lib/jsi/base.rb', line 200 def jsi_indicated_schemas @jsi_indicated_schemas end |
#jsi_ptr ⇒ JSI::Ptr (readonly)
Ptr pointing to this JSI's instance within our #jsi_document
164 165 166 |
# File 'lib/jsi/base.rb', line 164 def jsi_ptr @jsi_ptr end |
#jsi_root_node ⇒ JSI::Base (readonly)
the JSI at the root of this JSI's document
174 175 176 |
# File 'lib/jsi/base.rb', line 174 def jsi_root_node @jsi_root_node end |
Class Method Details
.inspect ⇒ String
A string indicating the schema module name and/or schema URI of each schema the class represents.
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 |
.name ⇒ String
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.
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_s ⇒ Object
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)
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.
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.
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( = {}) Util.as_json(jsi_instance, **) end |
#described_by?(schema) ⇒ Boolean
Is this JSI described by the given schema (or schema module)?
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 |
#dup ⇒ Object
632 633 634 |
# File 'lib/jsi/base.rb', line 632 def dup jsi_modified_copy(&:dup) end |
#inspect ⇒ String
a string representing this JSI, indicating any named schemas and inspecting its instance
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.
626 627 628 629 630 |
# File 'lib/jsi/base.rb', line 626 def jmespath_search(expression, **) Util.require_jmespath JMESPath.search(expression, self, **) end |
#jsi_ancestor_nodes ⇒ Array<JSI::Base>
ancestor JSI instances from this node up to the root. this node itself is always its own first ancestor.
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.
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.
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.
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_default ⇒ true, 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.
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
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.
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.
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_fingerprint ⇒ Object
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.
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.
591 592 593 594 |
# File 'lib/jsi/base.rb', line 591 def jsi_hash? # note: overridden by Base::HashNode false end |
#jsi_instance ⇒ Object
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?
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.
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?
598 599 600 |
# File 'lib/jsi/base.rb', line 598 def jsi_mutable? # note: overridden by Base::Mutable / Immutable end |
#jsi_node_content ⇒ Object
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.
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_node ⇒ JSI::Base?
the immediate parent of this JSI. nil if there is no parent.
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_nodes ⇒ Array<JSI::Base>
an array of JSI instances above this one in the document.
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_modules ⇒ Set<Module>
the set of JSI schema modules corresponding to the schemas that describe this JSI
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_schemas ⇒ JSI::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.
|
# 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.
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.
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
611 612 613 |
# File 'lib/jsi/base.rb', line 611 def jsi_valid? jsi_indicated_schemas.instance_valid?(self) end |
#jsi_validate ⇒ JSI::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.
695 696 697 |
# File 'lib/jsi/base.rb', line 695 def to_json( = {}) Util.to_json(jsi_instance, **) end |
#to_s ⇒ Object
642 643 644 |
# File 'lib/jsi/base.rb', line 642 def to_s inspect end |