Class: JSONSkooma::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/json_skooma/registry.rb

Constant Summary collapse

DEFAULT_NAME =
"registry"

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: DEFAULT_NAME) ⇒ Registry

Returns a new instance of Registry.



23
24
25
26
27
28
29
30
# File 'lib/json_skooma/registry.rb', line 23

def initialize(name: DEFAULT_NAME)
  @uri_sources = {}
  @vocabularies = {}
  @schema_cache = {}
  @enabled_formats = Set.new

  self.class.registries[name] = self
end

Class Attribute Details

.registriesObject

Returns the value of attribute registries.



8
9
10
# File 'lib/json_skooma/registry.rb', line 8

def registries
  @registries
end

Class Method Details

.[](key) ⇒ Object

Raises:



10
11
12
13
14
15
16
# File 'lib/json_skooma/registry.rb', line 10

def [](key)
  return key if key.is_a?(Registry)

  raise RegistryError, "Registry `#{key}` not found" unless registries.key?(key)

  registries[key]
end

Instance Method Details

#add_format(key, validator = nil) ⇒ Object

Raises:



32
33
34
35
36
37
# File 'lib/json_skooma/registry.rb', line 32

def add_format(key, validator = nil)
  JSONSkooma::Validators.register(key, validator) if validator
  raise RegistryError, "Format validator `#{key}` not found" unless Validators.validators.key?(key)

  @enabled_formats << key
end

#add_metaschema(uri, default_core_vocabulary_uri = nil, *default_vocabulary_uris) ⇒ Object

Raises:



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/json_skooma/registry.rb', line 85

def add_metaschema(uri, default_core_vocabulary_uri = nil, *default_vocabulary_uris)
  metaschema_doc = load_json(uri)
  default_core_vocabulary = vocabulary(default_core_vocabulary_uri) if default_core_vocabulary_uri
  default_vocabularies = default_vocabulary_uris.map { |vocabulary_uri| vocabulary(vocabulary_uri) }

  metaschema = Metaschema.new(
    metaschema_doc,
    default_core_vocabulary,
    *default_vocabularies,
    registry: self,
    uri: uri
  )

  return metaschema if metaschema.validate.valid?

  raise RegistryError, "The metaschema is invalid against its own metaschema #{metaschema_doc["$schema"]}"
end

#add_schema(uri, schema, cache_id: "default") ⇒ Object



51
52
53
54
# File 'lib/json_skooma/registry.rb', line 51

def add_schema(uri, schema, cache_id: "default")
  @schema_cache[cache_id] ||= {}
  @schema_cache[cache_id][uri.to_s] = schema
end

#add_source(uri, source) ⇒ Object

Raises:



110
111
112
113
114
# File 'lib/json_skooma/registry.rb', line 110

def add_source(uri, source)
  raise RegistryError, "uri must end with '/'" unless uri.end_with?("/")

  @uri_sources[uri] = source
end

#add_vocabulary(uri, *keywords) ⇒ Object



43
44
45
# File 'lib/json_skooma/registry.rb', line 43

def add_vocabulary(uri, *keywords)
  @vocabularies[uri.to_s] = Vocabulary.new(uri, *keywords)
end

#delete_schema(uri, cache_id: "default") ⇒ Object



56
57
58
# File 'lib/json_skooma/registry.rb', line 56

def delete_schema(uri, cache_id: "default")
  @schema_cache[cache_id].delete(uri.to_s)
end

#format_enabled?(key) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/json_skooma/registry.rb', line 39

def format_enabled?(key)
  @enabled_formats.include?(key)
end

#metaschema(uri) ⇒ Object

Raises:



103
104
105
106
107
108
# File 'lib/json_skooma/registry.rb', line 103

def metaschema(uri)
  schema = @schema_cache.dig("__meta__", uri.to_s) || add_metaschema(uri.to_s)
  return schema if schema

  raise RegistryError, "The schema referenced by #{uri} is not a metaschema"
end

#schema(uri, metaschema_uri: nil, cache_id: "default", schema_class: JSONSchema, expected_class: JSONSchema) ⇒ Object

Raises:



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/json_skooma/registry.rb', line 60

def schema(uri, metaschema_uri: nil, cache_id: "default", schema_class: JSONSchema, expected_class: JSONSchema)
  schema = @schema_cache.dig(cache_id, uri.to_s)
  return schema if schema

  base_uri = uri.dup.tap { |u| u.fragment = nil }
  schema = @schema_cache.dig(cache_id, base_uri.to_s) if uri.fragment

  if schema.nil?
    doc = load_json(base_uri)

    schema = schema_class.new(
      doc,
      registry: self,
      cache_id: cache_id,
      uri: base_uri,
      metaschema_uri: metaschema_uri
    )
    return @schema_cache.dig(cache_id, uri.to_s) if @schema_cache.dig(cache_id, uri.to_s)
  end
  schema = JSONPointer.new(uri.fragment).eval(schema) if uri.fragment
  return schema if schema.is_a?(expected_class)

  raise RegistryError, "The object referenced by #{uri} is not #{expected_class}"
end

#vocabulary(uri) ⇒ Object



47
48
49
# File 'lib/json_skooma/registry.rb', line 47

def vocabulary(uri)
  @vocabularies[uri.to_s] or raise RegistryError, "vocabulary #{uri} not found"
end