Skip to content

Commit 0bd491e

Browse files
committed
Add as_array utility to cleanup code.
1 parent 3dbd1b2 commit 0bd491e

File tree

6 files changed

+20
-18
lines changed

6 files changed

+20
-18
lines changed

lib/json/ld/api.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,7 @@ def self.flatten(input, context, expanded: false, **options)
293293

294294
if context && !flattened.empty?
295295
# Otherwise, return the result of compacting flattened according the Compaction algorithm passing context ensuring that the compaction result uses the @graph keyword (or its alias) at the top-level, even if the context is empty or if there is only one element to put in the @graph array. This ensures that the returned document has a deterministic structure.
296-
compacted = compact(flattened)
297-
compacted = [compacted] unless compacted.is_a?(Array)
296+
compacted = as_array(compact(flattened))
298297
kwgraph = self.context.compact_iri('@graph', quiet: true)
299298
flattened = self.context.serialize.merge(kwgraph => compacted)
300299
end

lib/json/ld/compact.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def compact(element, property: nil)
172172

173173
# handle @list
174174
if list?(expanded_item)
175-
compacted_item = [compacted_item] unless compacted_item.is_a?(Array)
175+
compacted_item = as_array(compacted_item)
176176
unless container == %w(@list)
177177
al = context.compact_iri('@list', vocab: true, quiet: true)
178178
compacted_item = {al => compacted_item}

lib/json/ld/context.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ def parse(local_context, remote_contexts = [])
385385
result = self.dup
386386
result.provided_context = local_context if self.empty?
387387

388-
local_context = [local_context] unless local_context.is_a?(Array)
388+
local_context = as_array(local_context)
389389

390390
local_context.each do |context|
391391
case context

lib/json/ld/expand.rb

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ def expand_object(input, active_property, context, output_object, ordered:, fram
196196
end
197197

198198
# Use array form if framing
199-
if framing && !e_id.is_a?(Array)
200-
[e_id]
199+
if framing
200+
as_array(e_id)
201201
else
202202
e_id
203203
end
@@ -226,7 +226,7 @@ def expand_object(input, active_property, context, output_object, ordered:, fram
226226
when '@graph'
227227
# If expanded property is @graph, set expanded value to the result of using this algorithm recursively passing active context, @graph for active property, and value for element.
228228
value = expand(value, '@graph', context, ordered: ordered, framing: framing)
229-
value.is_a?(Array) ? value : [value]
229+
as_array(value)
230230
when '@value'
231231
# If expanded property is @value and value is not a scalar or null, an invalid value object value error has been detected and processing is aborted. Otherwise, set expanded value to value. If expanded value is null, set the @value member of result to null and continue with the next key from element. Null values need to be preserved in this case as the meaning of an @type member depends on the existence of an @value member.
232232
# If framing, always use array form, unless null
@@ -281,7 +281,7 @@ def expand_object(input, active_property, context, output_object, ordered:, fram
281281
value = expand(value, active_property, context, ordered: ordered, framing: framing)
282282

283283
# Spec FIXME: need to be sure that result is an array
284-
value = [value] unless value.is_a?(Array)
284+
value = as_array(value)
285285

286286
# If expanded value is a list object, a list of lists error has been detected and processing is aborted.
287287
# Spec FIXME: Also look at each object if result is an array
@@ -399,15 +399,13 @@ def expand_object(input, active_property, context, output_object, ordered:, fram
399399
when %w(@graph @index), %w(@index)
400400
# Indexed graph by graph name
401401
if !graph?(item) && container.include?('@graph')
402-
item = [item] unless expanded_value.is_a?(Array)
403-
item = {'@graph' => item}
402+
item = {'@graph' => as_array(item)}
404403
end
405404
item['@index'] ||= k unless expanded_k == '@none'
406405
when %w(@graph @id), %w(@id)
407406
# Indexed graph by graph name
408407
if !graph?(item) && container.include?('@graph')
409-
item = [item] unless expanded_value.is_a?(Array)
410-
item = {'@graph' => item}
408+
item = {'@graph' => as_array(item)}
411409
end
412410
# Expand k document relative
413411
expanded_k = active_context.expand_iri(k, documentRelative: true, quiet: true).to_s unless expanded_k == '@none'
@@ -436,8 +434,7 @@ def expand_object(input, active_property, context, output_object, ordered:, fram
436434
# If the container mapping associated to key in active context is @list and expanded value is not already a list object, convert expanded value to a list object by first setting it to an array containing only expanded value if it is not already an array, and then by setting it to a JSON object containing the key-value pair @list-expanded value.
437435
if active_context.container(key) == %w(@list) && !list?(expanded_value)
438436
#log_debug(" => ") { "convert #{expanded_value.inspect} to list"}
439-
expanded_value = [expanded_value] unless expanded_value.is_a?(Array)
440-
expanded_value = {'@list' => expanded_value}
437+
expanded_value = {'@list' => as_array(expanded_value)}
441438
end
442439
#log_debug {" => #{expanded_value.inspect}"}
443440

@@ -472,8 +469,7 @@ def expand_object(input, active_property, context, output_object, ordered:, fram
472469

473470
# For each key in nests, recusively expand content
474471
nests.each do |key|
475-
nested_values = input[key]
476-
nested_values = [input[key]] unless input[key].is_a?(Array)
472+
nested_values = as_array(input[key])
477473
nested_values.each do |nv|
478474
raise JsonLdError::InvalidNestValue, nv.inspect unless
479475
nv.is_a?(Hash) && nv.keys.none? {|k| context.expand_iri(k, vocab: true) == '@value'}

lib/json/ld/frame.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,7 @@ def frame(state, subjects, frame, parent: nil, property: nil, **options)
160160
n = frame[prop].first || {}
161161
omit_default_on = get_frame_flag(n, options, :omitDefault)
162162
if !omit_default_on && !output[prop]
163-
preserve = n.fetch('@default', '@null').dup
164-
preserve = [preserve] unless preserve.is_a?(Array)
163+
preserve = as_array(n.fetch('@default', '@null').dup)
165164
output[prop] = [{'@preserve' => preserve}]
166165
end
167166
end

lib/json/ld/utils.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ def as_resource(id, base = nil)
110110
end
111111
end
112112

113+
##
114+
# Represent as an array
115+
# @param [Object] object
116+
# @return [Array<Object>]
117+
def as_array(object)
118+
object.is_a?(Array) ? object : [object]
119+
end
120+
113121
##
114122
# Compares two JSON-LD values for equality. Two JSON-LD values will be
115123
# considered equal if:

0 commit comments

Comments
 (0)