Class: JSI::Registry
- Inherits:
-
Object
- Object
- JSI::Registry
- Defined in:
- lib/jsi/registry.rb
Defined Under Namespace
Classes: Collision
Constant Summary collapse
- ResourceNotFound =
Deprecated.
alias after v0.8
an exception raised when a URI we are looking for has not been registered
ResolutionError
Instance Method Summary collapse
- #autoload_dialect_uri(uri) {|registry, uri| ... }
-
#autoload_uri(uri) {|registry, uri| ... }
takes a URI identifying a resource to be loaded by the given block when a reference to the URI is followed.
- #autoload_vocabulary_uri(uri) {|registry, uri| ... }
- #dialect_registered?(uri) ⇒ Boolean
- #dup ⇒ Object
- #find(uri) ⇒ JSI::Base
- #find_dialect(uri) ⇒ Schema::Dialect
- #find_vocabulary(uri) ⇒ Schema::Vocabulary
- #freeze ⇒ Object
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
- #pretty_print(q) ⇒ Object
-
#register(resource)
registers the given resource and/or schema resources it contains in the registry.
- #register_dialect(dialect, uri: dialect.id)
- #register_immediate(node)
- #register_vocabulary(vocabulary, uri: vocabulary.id)
- #registered?(uri) ⇒ Boolean
- #vocabulary_registered?(uri) ⇒ Boolean
Constructor Details
#initialize ⇒ Registry
Returns a new instance of Registry.
19 20 21 22 23 24 25 26 27 |
# File 'lib/jsi/registry.rb', line 19 def initialize @resources = {} @resource_autoloaders = {} @vocabularies = {} @vocabulary_autoloaders = {} @dialects = {} @dialect_autoloaders = {} @mutex = Mutex.new end |
Instance Method Details
#autoload_dialect_uri(uri) {|registry, uri| ... }
This method returns an undefined value.
198 199 200 |
# File 'lib/jsi/registry.rb', line 198 def autoload_dialect_uri(uri, &block) internal_autoload(@dialect_autoloaders, @dialects, uri, block) end |
#autoload_uri(uri) {|registry, uri| ... }
This method returns an undefined value.
takes a URI identifying a resource to be loaded by the given block when a reference to the URI is followed.
for example:
JSI.registry.autoload_uri('http://example.com/schema.json') do
JSI.new_schema({
'$schema' => 'http://json-schema.org/draft-07/schema#',
'$id' => 'http://example.com/schema.json',
'title' => 'my schema',
})
end
the block would normally load JSON from the filesystem or similar.
79 80 81 |
# File 'lib/jsi/registry.rb', line 79 def autoload_uri(uri, &block) internal_autoload(@resource_autoloaders, @resources, uri, block) end |
#autoload_vocabulary_uri(uri) {|registry, uri| ... }
This method returns an undefined value.
167 168 169 |
# File 'lib/jsi/registry.rb', line 167 def autoload_vocabulary_uri(uri, &block) internal_autoload(@vocabulary_autoloaders, @vocabularies, uri, block) end |
#dialect_registered?(uri) ⇒ Boolean
211 212 213 214 |
# File 'lib/jsi/registry.rb', line 211 def dialect_registered?(uri) uri = registration_uri(uri) @dialects.key?(uri) || @dialect_autoloaders.key?(uri) end |
#dup ⇒ Object
245 246 247 248 249 250 251 252 253 254 |
# File 'lib/jsi/registry.rb', line 245 def dup self.class.new.tap do |reg| reg.instance_variable_get(:@resources).update(@resources) reg.instance_variable_get(:@resource_autoloaders).update(@resource_autoloaders) reg.instance_variable_get(:@vocabularies).update(@vocabularies) reg.instance_variable_get(:@vocabulary_autoloaders).update(@vocabulary_autoloaders) reg.instance_variable_get(:@dialects).update(@dialects) reg.instance_variable_get(:@dialect_autoloaders).update(@dialect_autoloaders) end end |
#find(uri) ⇒ JSI::Base
102 103 104 |
# File 'lib/jsi/registry.rb', line 102 def find(uri) internal_find(uri, @resources, @resource_autoloaders, proc { |r| register(r) }, 'resource') end |
#find_dialect(uri) ⇒ Schema::Dialect
205 206 207 |
# File 'lib/jsi/registry.rb', line 205 def find_dialect(uri) internal_find(uri, @dialects, @dialect_autoloaders, proc { |v| register_dialect(v, uri: uri) }, 'dialect') end |
#find_vocabulary(uri) ⇒ Schema::Vocabulary
174 175 176 |
# File 'lib/jsi/registry.rb', line 174 def find_vocabulary(uri) internal_find(uri, @vocabularies, @vocabulary_autoloaders, proc { |v| register_vocabulary(v, uri: uri) }, 'vocabulary') end |
#freeze ⇒ Object
256 257 258 259 260 261 262 263 264 265 |
# File 'lib/jsi/registry.rb', line 256 def freeze @resources.freeze @resource_autoloaders.freeze @vocabularies.freeze @vocabulary_autoloaders.freeze @dialects.freeze @dialect_autoloaders.freeze @mutex = nil super end |
#pretty_print(q) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/jsi/registry.rb', line 216 def pretty_print(q) jsi_pp_object_group(q) do labels_uris = [ ['resources', @resources.keys], ['resources autoload', @resource_autoloaders.keys], ['vocabularies', @vocabularies.keys], ['vocabularies autoload', @vocabulary_autoloaders.keys], ['dialects', @dialects.keys], ['dialects autoload', @dialect_autoloaders.keys], ] q.seplist(labels_uris, q.method(:breakable)) do |label, uris| q.text("#{label} (#{uris.size})") if !uris.empty? q.text(": <") q.group do q.nest(2) do q.breakable('') q.seplist(uris) do |uri| q.text(uri.to_s.inspect) end end q.breakable('') end q.text '>' end end end end |
#register(resource)
This method returns an undefined value.
registers the given resource and/or schema resources it contains in the registry.
each descendent node of the resource (including the resource itself) is registered if it is a schema that has an absolute URI (generally defined by the '$id' keyword).
the given resource itself will be registered, whether or not it is a schema, if it is the root
of its document and was instantiated with the option uri specified.
39 40 41 42 43 44 45 |
# File 'lib/jsi/registry.rb', line 39 def register(resource) register_immediate(resource) if !resource.is_a?(Schema) resource.jsi_each_descendent_schema do |node| register_immediate(node) end end |
#register_dialect(dialect, uri: dialect.id)
This method returns an undefined value.
188 189 190 191 |
# File 'lib/jsi/registry.rb', line 188 def register_dialect(dialect, uri: dialect.id) raise(ArgumentError, "not a #{Schema::Dialect}: #{dialect.inspect}") if !dialect.is_a?(Schema::Dialect) internal_store(@dialects, @dialect_autoloaders, uri, dialect) end |
#register_immediate(node)
This method returns an undefined value.
49 50 51 52 53 54 55 56 57 |
# File 'lib/jsi/registry.rb', line 49 def register_immediate(node) unless node.is_a?(Base) || node.is_a?(Schema) raise(ArgumentError, "resource must be a #{Base}. got: #{node.pretty_inspect.chomp}") end node.jsi_resource_uris.each do |uri| internal_store(@resources, @resource_autoloaders, uri, node) end end |
#register_vocabulary(vocabulary, uri: vocabulary.id)
This method returns an undefined value.
157 158 159 160 |
# File 'lib/jsi/registry.rb', line 157 def register_vocabulary(vocabulary, uri: vocabulary.id) raise(ArgumentError, "not a #{Schema::Vocabulary}: #{vocabulary.inspect}") if !vocabulary.is_a?(Schema::Vocabulary) internal_store(@vocabularies, @vocabulary_autoloaders, uri, vocabulary) end |
#registered?(uri) ⇒ Boolean
149 150 151 152 |
# File 'lib/jsi/registry.rb', line 149 def registered?(uri) uri = registration_uri(uri) @resources.key?(uri) || @resource_autoloaders.key?(uri) end |
#vocabulary_registered?(uri) ⇒ Boolean
180 181 182 183 |
# File 'lib/jsi/registry.rb', line 180 def vocabulary_registered?(uri) uri = registration_uri(uri) @vocabularies.key?(uri) || @vocabulary_autoloaders.key?(uri) end |