Class: Schemurai::SchemaRegistry

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

Instance Method Summary collapse

Constructor Details

#initialize(schemas: {}) ⇒ SchemaRegistry

Returns a new instance of SchemaRegistry.



34
35
36
37
# File 'lib/schemurai.rb', line 34

def initialize(schemas: {})
  @graph = Internal::SchemaGraph.new(schemas: schemas)
  @shareable = false
end

Instance Method Details

#compile(schema, base_uri: nil, content: false, format: false) ⇒ Object

Raises:



39
40
41
42
43
44
# File 'lib/schemurai.rb', line 39

def compile(schema, base_uri: nil, content: false, format: false)
  raise Error, "cannot compile schemas after the registry is made shareable" if @shareable

  root = @graph.compile(schema, base_uri: base_uri)
  Validator.new(@graph, root, content: content, format: format)
end

#make_shareableObject



46
47
48
49
50
51
52
# File 'lib/schemurai.rb', line 46

def make_shareable
  return self if @shareable

  @graph.make_shareable
  @shareable = true
  Ractor.make_shareable(self)
end

#shareable?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/schemurai.rb', line 54

def shareable?
  @shareable
end

#validator_for(uri, content: false, format: false) ⇒ Object

Raises:



58
59
60
61
62
63
64
65
# File 'lib/schemurai.rb', line 58

def validator_for(uri, content: false, format: false)
  raise Error, "make_shareable must be called before retrieving validators by URI" unless @shareable

  root = @graph.node_at(uri)
  raise ResolutionError, "unregistered schema URI #{uri.inspect}" unless root

  Validator.new(@graph, root, content: content, format: format)
end