Skip to content

Commit 36bcf4d

Browse files
committed
Modernize use of Ruby options and keyword parameters.
1 parent 10f0d53 commit 36bcf4d

12 files changed

+64
-70
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/.yardoc/
22
/doc/
3+
/coverage/
34
/*.gem
45
/coverage/
56
/spec.html

Diff for: lib/json/ld/api.rb

+24-28
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,15 @@ class API
8686
# @yield [api]
8787
# @yieldparam [API]
8888
# @raise [JsonLdError]
89-
def initialize(input, context, options = {}, &block)
89+
def initialize(input, context, rename_bnodes: true, unique_bnodes: false, **options, &block)
9090
@options = {
9191
compactArrays: true,
92-
rename_bnodes: true,
9392
documentLoader: self.class.method(:documentLoader)
9493
}.merge(options)
95-
@namer = options[:unique_bnodes] ? BlankNodeUniqer.new : (@options[:rename_bnodes] ? BlankNodeNamer.new("b") : BlankNodeMapper.new)
94+
@namer = unique_bnodes ? BlankNodeUniqer.new : (rename_bnodes ? BlankNodeNamer.new("b") : BlankNodeMapper.new)
9695

9796
# For context via Link header
98-
remote_base, context_ref = nil, nil
97+
_, context_ref = nil, nil
9998

10099
@value = case input
101100
when Array, Hash then input.dup
@@ -115,7 +114,6 @@ def initialize(input, context, options = {}, &block)
115114
when String
116115
remote_doc = @options[:documentLoader].call(input, @options)
117116

118-
remote_base = remote_doc.documentUrl
119117
context_ref = remote_doc.contextUrl
120118
@options = {base: remote_doc.documentUrl}.merge(@options) unless @options[:no_default_base]
121119

@@ -166,12 +164,12 @@ def initialize(input, context, options = {}, &block)
166164
# @return [Object, Array<Hash>]
167165
# If a block is given, the result of evaluating the block is returned, otherwise, the expanded JSON-LD document
168166
# @see http://json-ld.org/spec/latest/json-ld-api/#expansion-algorithm
169-
def self.expand(input, options = {}, &block)
167+
def self.expand(input, ordered: true, framing: false, **options, &block)
170168
result, doc_base = nil
171169
API.new(input, options[:expandContext], options) do
172170
result = self.expand(self.value, nil, self.context,
173-
ordered: options.fetch(:ordered, true),
174-
framing: @options[:processingMode].to_s.include?('expand-frame'))
171+
ordered: ordered,
172+
framing: framing)
175173
doc_base = @options[:base]
176174
end
177175

@@ -215,18 +213,18 @@ def self.expand(input, options = {}, &block)
215213
# If a block is given, the result of evaluating the block is returned, otherwise, the compacted JSON-LD document
216214
# @raise [JsonLdError]
217215
# @see http://json-ld.org/spec/latest/json-ld-api/#compaction-algorithm
218-
def self.compact(input, context, options = {})
216+
def self.compact(input, context, expanded: false, **options)
219217
result = nil
220218
options = {compactToRelative: true}.merge(options)
221219

222220
# 1) Perform the Expansion Algorithm on the JSON-LD input.
223221
# This removes any existing context to allow the given context to be cleanly applied.
224-
expanded_input = options[:expanded] ? input : API.expand(input, options) do |result, base_iri|
222+
expanded_input = expanded ? input : API.expand(input, options) do |res, base_iri|
225223
options[:base] ||= base_iri if options[:compactToRelative]
226-
result
224+
res
227225
end
228226

229-
API.new(expanded_input, context, options.merge(no_default_base: true)) do
227+
API.new(expanded_input, context, no_default_base: true, **options) do
230228
log_debug(".compact") {"expanded input: #{expanded_input.to_json(JSON_STATE) rescue 'malformed json'}"}
231229
result = compact(value)
232230

@@ -260,18 +258,18 @@ def self.compact(input, context, options = {})
260258
# @return [Object, Hash]
261259
# If a block is given, the result of evaluating the block is returned, otherwise, the flattened JSON-LD document
262260
# @see http://json-ld.org/spec/latest/json-ld-api/#framing-algorithm
263-
def self.flatten(input, context, options = {})
261+
def self.flatten(input, context, expanded: false, **options)
264262
flattened = []
265263
options = {compactToRelative: true}.merge(options)
266264

267265
# Expand input to simplify processing
268-
expanded_input = options[:expanded] ? input : API.expand(input, options) do |result, base_iri|
266+
expanded_input = expanded ? input : API.expand(input, options) do |result, base_iri|
269267
options[:base] ||= base_iri if options[:compactToRelative]
270268
result
271269
end
272270

273271
# Initialize input using
274-
API.new(expanded_input, context, options.merge(no_default_base: true)) do
272+
API.new(expanded_input, context, no_default_base: true, **options) do
275273
log_debug(".flatten") {"expanded input: #{value.to_json(JSON_STATE) rescue 'malformed json'}"}
276274

277275
# Initialize node map to a JSON object consisting of a single member whose key is @default and whose value is an empty JSON object.
@@ -333,7 +331,7 @@ def self.flatten(input, context, options = {})
333331
# If a block is given, the result of evaluating the block is returned, otherwise, the framed JSON-LD document
334332
# @raise [InvalidFrame]
335333
# @see http://json-ld.org/spec/latest/json-ld-api/#framing-algorithm
336-
def self.frame(input, frame, options = {})
334+
def self.frame(input, frame, expanded: false, **options)
337335
result = nil
338336
options = {
339337
base: (input if input.is_a?(String)),
@@ -367,16 +365,16 @@ def self.frame(input, frame, options = {})
367365
end
368366

369367
# Expand input to simplify processing
370-
expanded_input = options[:expanded] ? input : API.expand(input, options) do |result, base_iri|
368+
expanded_input = expanded ? input : API.expand(input, options) do |res, base_iri|
371369
options[:base] ||= base_iri if options[:compactToRelative]
372-
result
370+
res
373371
end
374372

375373
# Expand frame to simplify processing
376-
expanded_frame = API.expand(frame, options.merge(processingMode: "json-ld-1.1-expand-frame"))
374+
expanded_frame = API.expand(frame, framing: true, **options)
377375

378376
# Initialize input using frame as context
379-
API.new(expanded_input, nil, options.merge(no_default_base: true)) do
377+
API.new(expanded_input, nil, no_default_base: true, **options) do
380378
log_debug(".frame") {"expanded input: #{expanded_input.to_json(JSON_STATE) rescue 'malformed json'}"}
381379
log_debug(".frame") {"expanded frame: #{expanded_frame.to_json(JSON_STATE) rescue 'malformed json'}"}
382380

@@ -397,7 +395,7 @@ def self.frame(input, frame, options = {})
397395
framing_state[:subjects] = framing_state[:graphMap][framing_state[:graph]]
398396

399397
result = []
400-
frame(framing_state, framing_state[:subjects].keys.sort, (expanded_frame.first || {}), options.merge(parent: result))
398+
frame(framing_state, framing_state[:subjects].keys.sort, (expanded_frame.first || {}), parent: result, **options)
401399

402400
# Count blank node identifiers used in the document, if pruning
403401
bnodes_to_clear = if options[:pruneBlankNodeIdentifiers]
@@ -433,7 +431,7 @@ def self.frame(input, frame, options = {})
433431
# @yield statement
434432
# @yieldparam [RDF::Statement] statement
435433
# @return [RDF::Enumerable] set of statements, unless a block is given.
436-
def self.toRdf(input, options = {}, &block)
434+
def self.toRdf(input, expanded: false, **options, &block)
437435
unless block_given?
438436
results = []
439437
results.extend(RDF::Enumerable)
@@ -444,7 +442,7 @@ def self.toRdf(input, options = {}, &block)
444442
end
445443

446444
# Expand input to simplify processing
447-
expanded_input = options[:expanded] ? input : API.expand(input, options.merge(ordered: false))
445+
expanded_input = expanded ? input : API.expand(input, ordered: false, **options)
448446

449447
API.new(expanded_input, nil, options) do
450448
# 1) Perform the Expansion Algorithm on the JSON-LD input.
@@ -495,9 +493,7 @@ def self.toRdf(input, options = {}, &block)
495493
# @yieldreturn [Object] returned object
496494
# @return [Object, Hash]
497495
# If a block is given, the result of evaluating the block is returned, otherwise, the expanded JSON-LD document
498-
def self.fromRdf(input, options = {}, &block)
499-
useRdfType = options.fetch(:useRdfType, false)
500-
useNativeTypes = options.fetch(:useNativeTypes, false)
496+
def self.fromRdf(input, useRdfType: false, useNativeTypes: false, **options, &block)
501497
result = nil
502498

503499
API.new(nil, nil, options) do |api|
@@ -519,12 +515,12 @@ def self.fromRdf(input, options = {}, &block)
519515
# @return [Object, RemoteDocument]
520516
# If a block is given, the result of evaluating the block is returned, otherwise, the retrieved remote document and context information unless block given
521517
# @raise [JsonLdError]
522-
def self.documentLoader(url, options = {})
518+
def self.documentLoader(url, validate: false, **options)
523519
options = OPEN_OPTS.merge(options)
524520
RDF::Util::File.open_file(url, options) do |remote_doc|
525521
content_type = remote_doc.content_type if remote_doc.respond_to?(:content_type)
526522
# If the passed input is a DOMString representing the IRI of a remote document, dereference it. If the retrieved document's content type is neither application/json, nor application/ld+json, nor any other media type using a +json suffix as defined in [RFC6839], reject the promise passing an loading document failed error.
527-
if content_type && options[:validate]
523+
if content_type && validate
528524
main, sub = content_type.split("/")
529525
raise JSON::LD::JsonLdError::LoadingDocumentFailed, "url: #{url}, content_type: #{content_type}" if
530526
main != 'application' ||

Diff for: lib/json/ld/context.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ def create_term_definition(local_context, term, defined)
735735
#
736736
# @param [Hash{Symbol => Object}] options ({})
737737
# @return [Hash]
738-
def serialize(options = {})
738+
def serialize(**options)
739739
# FIXME: not setting provided_context now
740740
use_context = case provided_context
741741
when String, RDF::URI
@@ -1324,7 +1324,7 @@ def expand_value(property, value, useNativeTypes: false, **options)
13241324
# @raise [JsonLdError] if the iri cannot be expanded
13251325
# @see http://json-ld.org/spec/latest/json-ld-api/#value-compaction
13261326
# FIXME: revisit the specification version of this.
1327-
def compact_value(property, value, options = {})
1327+
def compact_value(property, value, **options)
13281328
#log_debug("compact_value") {"property: #{property.inspect}, value: #{value.inspect}"}
13291329

13301330
num_members = value.length

Diff for: lib/json/ld/format.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ def self.cli_commands
6363
# If files are empty, either use options[:execute]
6464
input = options[:evaluate] ? StringIO.new(options[:evaluate]) : STDIN
6565
input.set_encoding(options.fetch(:encoding, Encoding::UTF_8))
66-
JSON::LD::API.expand(input, options.merge(validate: false)) do |expanded|
66+
JSON::LD::API.expand(input, validate: false, **options) do |expanded|
6767
out.puts expanded.to_json(JSON::LD::JSON_STATE)
6868
end
6969
else
7070
files.each do |file|
71-
JSON::LD::API.expand(file, options.merge(validate: false)) do |expanded|
71+
JSON::LD::API.expand(file, validate: false, **options) do |expanded|
7272
out.puts expanded.to_json(JSON::LD::JSON_STATE)
7373
end
7474
end

Diff for: lib/json/ld/frame.rb

+6-7
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@ module Frame
2020
# @option options [String] :property (nil)
2121
# The parent property.
2222
# @raise [JSON::LD::InvalidFrame]
23-
def frame(state, subjects, frame, **options)
23+
def frame(state, subjects, frame, parent: nil, property: nil, **options)
2424
#log_depth do
2525
#log_debug("frame") {"subjects: #{subjects.inspect}"}
2626
#log_debug("frame") {"frame: #{frame.to_json(JSON_STATE)}"}
27-
#log_debug("frame") {"property: #{options[:property].inspect}"}
27+
#log_debug("frame") {"property: #{property.inspect}"}
2828

29-
parent, property = options[:parent], options[:property]
3029
# Validate the frame
3130
validate_frame(frame)
3231
frame = frame.first if frame.is_a?(Array)
@@ -105,7 +104,7 @@ def frame(state, subjects, frame, **options)
105104
if recurse
106105
state[:graphStack].push(state[:graph])
107106
state[:graph] = id
108-
frame(state, state[:graphMap][id].keys, [subframe], options.merge(parent: output, property: '@graph'))
107+
frame(state, state[:graphMap][id].keys, [subframe], parent: output, property: '@graph', **options)
109108
state[:graph] = state[:graphStack].pop
110109
end
111110
end
@@ -138,14 +137,14 @@ def frame(state, subjects, frame, **options)
138137
src = o['@list']
139138
src.each do |oo|
140139
if node_reference?(oo)
141-
frame(state, [oo['@id']], subframe, options.merge(parent: list, property: '@list'))
140+
frame(state, [oo['@id']], subframe, parent: list, property: '@list', **options)
142141
else
143142
add_frame_output(list, '@list', oo.dup)
144143
end
145144
end
146145
when node_reference?(o)
147146
# recurse into subject reference
148-
frame(state, [o['@id']], subframe, options.merge(parent: output, property: prop))
147+
frame(state, [o['@id']], subframe, parent: output, property: prop, **options)
149148
when value_match?(subframe, o)
150149
# Include values if they match
151150
add_frame_output(output, prop, o.dup)
@@ -174,7 +173,7 @@ def frame(state, subjects, frame, **options)
174173
# Node has property referencing this subject
175174
# recurse into reference
176175
(output['@reverse'] ||= {})[reverse_prop] ||= []
177-
frame(state, [r_id], subframe, options.merge(parent: output['@reverse'][reverse_prop]))
176+
frame(state, [r_id], subframe, parent: output['@reverse'][reverse_prop], property: property, **options)
178177
end
179178
end
180179
end

Diff for: lib/json/ld/resource.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def new?; !!@new; end
7676
# @option options [Boolean] :stub (false)
7777
# This is a stand-in for another resource that has
7878
# not yet been retrieved (or created) from Mongo
79-
def initialize(node_definition, options = {})
79+
def initialize(node_definition, **options)
8080
@context = options[:context]
8181
@clean = options.fetch(:clean, false)
8282
@new = options.fetch(:new, true)

Diff for: lib/json/ld/writer.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def self.options
139139
# @yieldreturn [void]
140140
# @yield [writer]
141141
# @yieldparam [RDF::Writer] writer
142-
def initialize(output = $stdout, options = {}, &block)
142+
def initialize(output = $stdout, **options, &block)
143143
options[:base_uri] ||= options[:base] if options.has_key?(:base)
144144
options[:base] ||= options[:base_uri] if options.has_key?(:base_uri)
145145
super do

Diff for: script/parse

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def run(input, options)
1717
output_dir = File.expand_path("../../doc/profiles/#{File.basename __FILE__, ".rb"}", __FILE__)
1818
FileUtils.mkdir_p(output_dir)
1919
result = RubyProf.profile do
20-
run(input, options.merge(profile: false))
20+
run(input, profile: false, **options)
2121
end
2222
result.eliminate_methods!([/Hash#each/, /JSON::LD::Utils#debug/, /Array#map/, /JSON::LD::Utils#depth/])
2323
# Print a graph profile to text

Diff for: spec/from_rdf_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -398,17 +398,17 @@
398398
end
399399
end
400400

401-
def parse(input, options = {})
401+
def parse(input, **options)
402402
reader = options[:reader] || RDF::TriG::Reader
403403
reader.new(input, options, &:each_statement).to_a.extend(RDF::Enumerable)
404404
end
405405

406406
# Serialize ntstr to a string and compare against regexps
407-
def serialize(ntstr, options = {})
407+
def serialize(ntstr, **options)
408408
logger.info ntstr if ntstr.is_a?(String)
409409
g = ntstr.is_a?(String) ? parse(ntstr, options) : ntstr
410410
logger.info g.dump(:trig)
411411
statements = g.each_statement.to_a
412-
JSON::LD::API.fromRdf(statements, options.merge(logger: logger))
412+
JSON::LD::API.fromRdf(statements, logger: logger, **options)
413413
end
414414
end

Diff for: spec/streaming_writer_spec.rb

+6-7
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@
5050
)
5151
obj = serialize(input)
5252
expect(parse(obj.to_json, format: :jsonld)).to be_equivalent_graph(parse(input), logger: logger)
53-
expect(obj).to contain_exactly *JSON.parse(%{[
53+
expect(obj).to contain_exactly(*JSON.parse(%{[
5454
{"@id": "http://example.com/test-cases/0001", "@type": ["http://www.w3.org/2006/03/test-description#TestCase"]},
5555
{"@id": "http://example.com/test-cases/0002", "@type": ["http://www.w3.org/2006/03/test-description#TestCase"]}
56-
]})
56+
]}))
5757
end
5858
end
5959

@@ -91,7 +91,7 @@
9191
context title do
9292
subject {serialize(input)}
9393
it "matches expected json" do
94-
expect(subject).to contain_exactly *JSON.parse(matches)
94+
expect(subject).to contain_exactly(*JSON.parse(matches))
9595
end
9696
end
9797
end
@@ -125,18 +125,17 @@
125125
end
126126
end unless ENV['CI']
127127

128-
def parse(input, options = {})
129-
format = options.fetch(:format, :trig)
128+
def parse(input, format: :trig, **options)
130129
reader = RDF::Reader.for(format)
131130
RDF::Repository.new << reader.new(input, options)
132131
end
133132

134133
# Serialize ntstr to a string and compare against regexps
135-
def serialize(ntstr, options = {})
134+
def serialize(ntstr, **options)
136135
g = ntstr.is_a?(String) ? parse(ntstr, options) : ntstr
137136
logger = RDF::Spec.logger
138137
logger.info(g.dump(:ttl))
139-
result = JSON::LD::Writer.buffer(options.merge(logger: logger, stream: true)) do |writer|
138+
result = JSON::LD::Writer.buffer(logger: logger, stream: true, **options) do |writer|
140139
writer << g
141140
end
142141
puts result if $verbose

0 commit comments

Comments
 (0)