Skip to content

Commit 0e76e76

Browse files
committed
Tweaks to preloaded contexts.
1 parent c23de4c commit 0e76e76

File tree

4 files changed

+28
-13
lines changed

4 files changed

+28
-13
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,15 @@ Then, when performing something like expansion:
242242

243243

244244
## Preloading contexts
245-
In many cases, for small documents, processing time can be dominated by loading and parsing remote contexts. In particular, a small schema.org example may need to download a large context and turn it into an internal representation, before the actual document can be expanded for processing. Using `JSON::LD::Context.add_preloaded`, an implementation can perform this loading up-front, and make it available to the processor.
245+
In many cases, for small documents, processing time can be dominated by loading and parsing remote contexts. In particular, a small schema.org example may need to download a large context and turn it into an internal representation, before the actual document can be expanded for processing. Using {JSON::LD::Context.add_preloaded}, an implementation can perform this loading up-front, and make it available to the processor.
246246

247247
ctx = JSON::LD::Context.new().parse('http://schema.org/')
248248
JSON::LD::Context.add_preloaded('http://schema.org/', ctx)
249249

250+
On lookup, URIs with an `https` prefix are normalized to `http`.
251+
252+
A context may be serialized to Ruby to speed this process using `Context#to_rb`. When loaded, this generated file will add entries to the {JSON::LD::Context::PRELOADED}.
253+
250254
## RDF Reader and Writer
251255
{JSON::LD} also acts as a normal RDF reader and writer, using the standard RDF.rb reader/writer interfaces:
252256

example-files/nicholas-arduino-bench.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env ruby
22
require "bundler/setup"
33
require 'json/ld'
4+
#require File.expand_path('../schema-context', __FILE__)
45
require 'benchmark/ips'
56

67
source = JSON.parse %({

example-files/schema-context.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
# -*- encoding: utf-8 -*-
2+
# frozen_string_literal: true
3+
# This file generated automatically from http://schema.org
14
require 'json/ld'
25
class JSON::LD::Context
3-
PRELOADED["http://schema.org/"] = JSON::LD::Context.new(vocab: "http://schema.org/", term_definitions: {
6+
add_preloaded("http://schema.org/", new(vocab: "http://schema.org/", term_definitions: {
47
"type" => TermDefinition.new("type", id: "@type", simple: true),
58
"id" => TermDefinition.new("id", id: "@id", simple: true),
69
"schema" => TermDefinition.new("schema", id: "http://schema.org/", simple: true),
@@ -2112,5 +2115,5 @@ class JSON::LD::Context
21122115
"worstRating" => TermDefinition.new("worstRating", id: "http://schema.org/worstRating"),
21132116
"yearlyRevenue" => TermDefinition.new("yearlyRevenue", id: "http://schema.org/yearlyRevenue"),
21142117
"yearsInOperation" => TermDefinition.new("yearsInOperation", id: "http://schema.org/yearsInOperation")
2115-
})
2118+
}))
21162119
end

lib/json/ld/context.rb

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ class Context
1313
PRELOADED = {}
1414

1515
class << self
16+
##
17+
# Add preloaded context
18+
# @param [String, RDF::URI] url
19+
# @param [Context] context
1620
def add_preloaded(url, context)
17-
PRELOADED[url.to_sym] = context
21+
PRELOADED[url.to_s.freeze] = context
1822
end
1923
end
2024

@@ -268,14 +272,14 @@ def vocab=(value)
268272
@vocab = case value
269273
when /_:/
270274
value
271-
when String
272-
v = as_resource(value)
273-
raise JsonLdError::InvalidVocabMapping, "@value must be an absolute IRI: #{value.inspect}" if v.uri? && v.relative? && @options[:validate]
275+
when String, RDF::URI
276+
v = as_resource(value.to_s)
277+
raise JsonLdError::InvalidVocabMapping, "@vocab must be an absolute IRI: #{value.inspect}" if v.uri? && v.relative? && @options[:validate]
274278
v
275279
when nil
276280
nil
277281
else
278-
raise JsonLdError::InvalidVocabMapping, "@value must be a string: #{value.inspect}"
282+
raise JsonLdError::InvalidVocabMapping, "@vocab must be an absolute IRI: #{value.inspect}"
279283
end
280284
end
281285

@@ -336,10 +340,10 @@ def parse(local_context, remote_contexts = [])
336340
context_no_base.base = nil
337341
context_no_base.context_base = context.to_s
338342

339-
if PRELOADED[context_canon.to_s.to_sym]
343+
if PRELOADED[context_canon.to_s]
340344
# If we have a cached context, merge it into the current context (result) and use as the new context
341345
log_debug("parse") {"=> cached_context: #{context_canon.to_s.inspect}"}
342-
context = context_no_base.merge!(PRELOADED[context_canon.to_s.to_sym])
346+
context = context_no_base.merge!(PRELOADED[context_canon.to_s])
343347
else
344348

345349
# Load context document, if it is a string
@@ -1196,11 +1200,14 @@ def to_rb
11961200
term_defs = term_definitions.map do |term, td|
11971201
" " + term.inspect + " => " + td.to_rb
11981202
end
1199-
defn << "term_definitions: {\n#{term_defs.join(",\n") }\n}" unless term_defs.empty?
1200-
%(require 'json/ld'
1203+
defn << "term_definitions: {\n#{term_defs.join(",\n") }\n }" unless term_defs.empty?
1204+
%(# -*- encoding: utf-8 -*-
1205+
# frozen_string_literal: true
1206+
# This file generated automatically from #{context_base}
1207+
require 'json/ld'
12011208
class JSON::LD::Context
12021209
).gsub(/^ /, '') +
1203-
" PRELOADED[#{RDF::URI(context_base).canonicalize.to_s.inspect}] = Context.new(" + defn.join(", ") + ")\nend\n"
1210+
" add_preloaded(#{RDF::URI(context_base).canonicalize.to_s.inspect}, Context.new(" + defn.join(", ") + "))\nend\n"
12041211
end
12051212

12061213
def inspect

0 commit comments

Comments
 (0)