lib/json/ld/compact.rb in json-ld-3.1.3 vs lib/json/ld/compact.rb in json-ld-3.1.4
- old
+ new
@@ -10,15 +10,18 @@
##
# This algorithm compacts a JSON-LD document, such that the given context is applied. This must result in shortening any applicable IRIs to terms or compact IRIs, any applicable keywords to keyword aliases, and any applicable JSON-LD values expressed in expanded form to simple values such as strings or numbers.
#
# @param [Array, Hash] element
- # @param [String] property (nil)
- # @param [Boolean] ordered (true)
+ # @param [String, RDF::URI] base (nil)
# Ensure output objects have keys ordered properly
+ # @param [String] property (nil)
+ # Extra validatation
# @return [Array, Hash]
- def compact(element, property: nil, ordered: false)
+ def compact(element,
+ base: nil,
+ property: nil)
#if property.nil?
# log_debug("compact") {"element: #{element.inspect}, ec: #{context.inspect}"}
#else
# log_debug("compact") {"property: #{property.inspect}"}
#end
@@ -27,11 +30,13 @@
input_context = self.context
case element
when Array
#log_debug("") {"Array #{element.inspect}"}
- result = element.map {|item| compact(item, property: property, ordered: ordered)}.compact
+ result = element.map do |item|
+ compact(item, base: base, property: property)
+ end.compact
# If element has a single member and the active property has no
# @container mapping to @list or @set, the compacted value is that
# member; otherwise the compacted value is element
if result.length == 1 &&
@@ -53,23 +58,27 @@
self.context = context.previous_context
end
# Look up term definintions from property using the original type-scoped context, if it exists, but apply them to the now current previous context
td = input_context.term_definitions[property] if property
- self.context = context.parse(td.context, override_protected: true) if td && td.context
+ if td && !td.context.nil?
+ self.context = context.parse(td.context,
+ override_protected: true)
+ end
if element.key?('@id') || element.key?('@value')
- result = context.compact_value(property, element, log_depth: @options[:log_depth])
+ result = context.compact_value(property, element, base: @options[:base])
if !result.is_a?(Hash) || context.coerce(property) == '@json'
#log_debug("") {"=> scalar result: #{result.inspect}"}
return result
end
end
# If expanded property is @list and we're contained within a list container, recursively compact this item to an array
if list?(element) && context.container(property).include?('@list')
- return compact(element['@list'], property: property, ordered: ordered)
+ return compact(element['@list'], base: base,
+ property: property)
end
inside_reverse = property == '@reverse'
result, nest_result = {}, nil
@@ -78,29 +87,33 @@
Array(element['@type']).
map {|expanded_type| context.compact_iri(expanded_type, vocab: true)}.
sort.
each do |term|
term_context = input_context.term_definitions[term].context if input_context.term_definitions[term]
- self.context = context.parse(term_context, propagate: false) if term_context
+ self.context = context.parse(term_context, propagate: false) unless term_context.nil?
end
- element.keys.opt_sort(ordered: ordered).each do |expanded_property|
+ element.keys.opt_sort(ordered: @options[:ordered]).each do |expanded_property|
expanded_value = element[expanded_property]
#log_debug("") {"#{expanded_property}: #{expanded_value.inspect}"}
if expanded_property == '@id'
- compacted_value = Array(expanded_value).map {|expanded_id| context.compact_iri(expanded_id)}
+ compacted_value = Array(expanded_value).map do |expanded_id|
+ context.compact_iri(expanded_id, base: @options[:base])
+ end
kw_alias = context.compact_iri('@id', vocab: true)
as_array = compacted_value.length > 1
compacted_value = compacted_value.first unless as_array
result[kw_alias] = compacted_value
next
end
if expanded_property == '@type'
- compacted_value = Array(expanded_value).map {|expanded_type| input_context.compact_iri(expanded_type, vocab: true)}
+ compacted_value = Array(expanded_value).map do |expanded_type|
+ input_context.compact_iri(expanded_type, vocab: true)
+ end
kw_alias = context.compact_iri('@type', vocab: true)
as_array = compacted_value.length > 1 ||
(context.as_array?(kw_alias) &&
!value?(element) &&
@@ -108,11 +121,12 @@
add_value(result, kw_alias, compacted_value, property_is_array: as_array)
next
end
if expanded_property == '@reverse'
- compacted_value = compact(expanded_value, property: '@reverse', ordered: ordered)
+ compacted_value = compact(expanded_value, base: base,
+ property: '@reverse')
#log_debug("@reverse") {"compacted_value: #{compacted_value.inspect}"}
# handle double-reversed properties
compacted_value.each do |prop, value|
if context.reverse?(prop)
add_value(result, prop, value,
@@ -129,11 +143,12 @@
next
end
if expanded_property == '@preserve'
# Compact using `property`
- compacted_value = compact(expanded_value, property: property, ordered: ordered)
+ compacted_value = compact(expanded_value, base: base,
+ property: property)
#log_debug("@preserve") {"compacted_value: #{compacted_value.inspect}"}
unless compacted_value.is_a?(Array) && compacted_value.empty?
result['@preserve'] = compacted_value
end
@@ -156,12 +171,11 @@
if expanded_value.empty?
item_active_property =
context.compact_iri(expanded_property,
value: expanded_value,
vocab: true,
- reverse: inside_reverse,
- log_depth: @options[:log_depth])
+ reverse: inside_reverse)
if nest_prop = context.nest(item_active_property)
result[nest_prop] ||= {}
add_value(result[nest_prop], item_active_property, [],
property_is_array: true)
@@ -175,12 +189,11 @@
expanded_value.each do |expanded_item|
item_active_property =
context.compact_iri(expanded_property,
value: expanded_item,
vocab: true,
- reverse: inside_reverse,
- log_depth: @options[:log_depth])
+ reverse: inside_reverse)
nest_result = if nest_prop = context.nest(item_active_property)
# FIXME??: It's possible that nest_prop will be used both for nesting, and for values of @nest
result[nest_prop] ||= {}
@@ -195,11 +208,12 @@
when list?(expanded_item) then expanded_item['@list']
when graph?(expanded_item) then expanded_item['@graph']
else expanded_item
end
- compacted_item = compact(value, property: item_active_property, ordered: ordered)
+ compacted_item = compact(value, base: base,
+ property: item_active_property)
#log_debug("") {" => compacted key: #{item_active_property.inspect} for #{compacted_item.inspect}"}
# handle @list
if list?(expanded_item)
compacted_item = as_array(compacted_item)
@@ -223,13 +237,13 @@
(container.include?('@id') || container.include?('@index') && simple_graph?(expanded_item))
# container includes @graph and @id
map_object = nest_result[item_active_property] ||= {}
# If there is no @id, create a blank node identifier to use as an index
map_key = if container.include?('@id') && expanded_item['@id']
- context.compact_iri(expanded_item['@id'])
+ context.compact_iri(expanded_item['@id'], base: @options[:base])
elsif container.include?('@index') && expanded_item['@index']
- context.compact_iri(expanded_item['@index'])
+ context.compact_iri(expanded_item['@index'], vocab: true)
else
context.compact_iri('@none', vocab: true)
end
add_value(map_object, map_key, compacted_item,
property_is_array: as_array)
@@ -297,10 +311,12 @@
else compacted_item[container_key] = types
end
# if compacted_item contains a single entry who's key maps to @id, then recompact the item without @type
if compacted_item.keys.length == 1 && expanded_item.keys.include?('@id')
- compacted_item = compact({'@id' => expanded_item['@id']}, property: item_active_property)
+ compacted_item = compact({'@id' => expanded_item['@id']},
+ base: base,
+ property: item_active_property)
end
compacted_item
end
map_key ||= context.compact_iri('@none', vocab: true)
add_value(map_object, map_key, compacted_item,