File: document_store.rb

package info (click to toggle)
ruby-brandur-json-schema 0.19.1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 376 kB
  • sloc: ruby: 3,764; makefile: 6
file content (30 lines) | stat: -rw-r--r-- 777 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
module JsonSchema
  # The document store helps resolve URI-based JSON pointers by storing IDs
  # that we've seen in the schema.
  #
  # Each URI tuple also contains a pointer map that helps speed up expansions
  # that have already happened and handles cyclic dependencies. Store a
  # reference to the top-level schema before doing anything else.
  class DocumentStore
    include Enumerable

    def initialize
      @schema_map = {}
    end

    def add_schema(schema)
      raise ArgumentError, "can't add nil URI" if schema.uri.nil?
      uri = schema.uri.chomp('#')
      @schema_map[uri] = schema
    end

    def each
      @schema_map.each { |k, v| yield(k, v) }
    end

    def lookup_schema(uri)
      uri = uri.chomp('#')
      @schema_map[uri]
    end
  end
end