Class: JSI::SchemaModule
- Inherits:
-
Connection
- Object
- Module
- Connection
- JSI::SchemaModule
- Defined in:
- lib/jsi/schema_classes.rb
Overview
A Module associated with a JSI Schema (its #jsi_schema_module).
This module may be opened by the application to define methods for instances described by its schema.
The schema module can also be used in some of the same ways as its schema: JSI instances of the schema can be instantiated using #new_jsi, or instances can be validated with #instance_valid? or #instance_validate. Often the schema module is the more convenient object to work with with than the JSI Schema.
Naming the schema module (assigning it to a constant) can be useful in a few ways.
- When inspected, instances of a schema with a named schema module will show that name.
- Naming the module allows it to be opened with Ruby's
modulesyntax. Any schema module can be opened with Module#module_exec (or from the Schema with jsi_schema_module_exec) but themodulesyntax can be more convenient, especially for assigning or accessing constants.
The schema module makes it straightforward to access the schema modules of the schema's subschemas. It defines readers for schema properties (keywords) on its singleton (that is, called on the module itself, not on instances of it) to access these. The #[] method can also be used.
For example, given a schema with an items subschema, then schema.items.jsi_schema_module
and schema.jsi_schema_module.items both refer to the same module.
Subscripting with #[] can refer to subschemas on properties
that can have any name, e.g. schema.properties['foo'].jsi_schema_module is the same as
schema.jsi_schema_module.properties['foo'].
Schema module property readers and #[] can also take a block, which is passed to module_exec.
Putting the above together, here is example usage with the schema module of the Contact schema used in the README:
Contact = JSI.new_schema_module({
"$schema" => "http://json-schema.org/draft-07/schema",
"type" => "object",
"properties" => {
"name" => {"type" => "string"},
"phone" => {
"type" => "array",
"items" => {
"type" => "object",
"properties" => {
"location" => {"type" => "string"},
"number" => {"type" => "string"}
}
}
}
}
})
module Contact
# name a subschema's schema module
PhoneNumber = properties['phone'].items
# open a subschema's schema module to define methods
properties['phone'] do
def numbers
map(&:number)
end
end
end
bill = Contact.new_jsi({"name" => "bill", "phone" => [{"location" => "home", "number" => "555"}]})
#> #{<JSI (Contact)>
#> "name" => "bill",
#> "phone" => #[<JSI (Contact.properties["phone"])>
#> #{<JSI (Contact::PhoneNumber)> "location" => "home", "number" => "555"}
#> ],
#> "nickname" => "big b"
#> }
Note that when bill is inspected, schema module names Contact, Contact.properties["phone"],
and Contact::PhoneNumber are informatively shown on respective instances.
Defined Under Namespace
Modules: MetaSchemaModule Classes: Connection
Instance Attribute Summary
Attributes inherited from Connection
Instance Method Summary collapse
- #describes_schema!(dialect = nil) ⇒ Object
- #inspect ⇒ String
- #instance_valid?(instance) ⇒ Boolean
- #instance_validate(instance) ⇒ Object
-
#new_jsi(instance, **kw) ⇒ Base
invokes JSI::Schema#new_jsi on this module's schema, passing the given parameters.
-
#schema ⇒ Base + Schema
The schema for which this is the JSI Schema Module.
- #schema_content ⇒ Object
-
#schema_uri ⇒ URI?
a URI which refers to the schema.
- #to_s ⇒ Object
Methods inherited from Connection
#/, #[], #initialize, #name_from_ancestor
Constructor Details
This class inherits a constructor from JSI::SchemaModule::Connection
Instance Method Details
#describes_schema!(dialect = nil) ⇒ Object
146 147 148 |
# File 'lib/jsi/schema_classes.rb', line 146 def describes_schema!(dialect = nil) schema.describes_schema!(dialect) end |
#inspect ⇒ String
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/jsi/schema_classes.rb', line 104 def inspect dam_s = " #{schema.jsi_schema_dynamic_anchor_map.anchor_schemas_identifier}" if !schema.jsi_schema_dynamic_anchor_map.empty? if name_from_ancestor if schema.jsi_resource_uri -"#{name_from_ancestor} <#{schema.jsi_resource_uri}>#{dam_s} (JSI Schema Module)" else -"#{name_from_ancestor}#{dam_s} (JSI Schema Module)" end else -"(JSI Schema Module: #{schema.schema_uri || schema.jsi_ptr.uri}#{dam_s})" end end |
#instance_valid?(instance) ⇒ Boolean
141 142 143 |
# File 'lib/jsi/schema_classes.rb', line 141 def instance_valid?(instance) schema.instance_valid?(instance) end |
#instance_validate(instance) ⇒ Object
136 137 138 |
# File 'lib/jsi/schema_classes.rb', line 136 def instance_validate(instance) schema.instance_validate(instance) end |
#new_jsi(instance, **kw) ⇒ Base
invokes JSI::Schema#new_jsi on this module's schema, passing the given parameters.
125 126 127 128 |
# File 'lib/jsi/schema_classes.rb', line 125 def new_jsi(instance, **kw) raise(BlockGivenError) if block_given? schema.new_jsi(instance, **kw) end |
#schema ⇒ Base + Schema
The schema for which this is the JSI Schema Module
93 94 95 |
# File 'lib/jsi/schema_classes.rb', line 93 def schema @jsi_node end |
#schema_content ⇒ Object
131 132 133 |
# File 'lib/jsi/schema_classes.rb', line 131 def schema_content schema.jsi_node_content end |
#schema_uri ⇒ URI?
a URI which refers to the schema. see JSI::Schema#schema_uri.
99 100 101 |
# File 'lib/jsi/schema_classes.rb', line 99 def schema_uri schema.schema_uri end |
#to_s ⇒ Object
117 118 119 |
# File 'lib/jsi/schema_classes.rb', line 117 def to_s inspect end |