lib/json/ld/compact.rb in json-ld-0.1.6.1 vs lib/json/ld/compact.rb in json-ld-0.3.0

- old
+ new

@@ -23,11 +23,11 @@ result = depth {element.map {|v| compact(v, property)}} # 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 + if result.length == 1 && @options[:compactArrays] debug("=> extract single element: #{result.first.inspect}") result.first else debug("=> array result: #{result.inspect}") result @@ -42,17 +42,21 @@ k ||= '@id' if element.keys == ['@id'] case k when '@value', '@id' - # If element has an @value property or element is a node reference, return the result of performing - # Value Compaction on element using active property. + # If element has an @value property or element is a node reference, return the result of performing Value Compaction on element using active property. v = context.compact_value(property, element, :depth => @depth) debug("compact") {"value optimization, return as #{v.inspect}"} return v when '@list' - # Otherwise, if the active property has a @container mapping to @list and element has a corresponding @list property, recursively compact that property's value passing a copy of the active context and the active property ensuring that the result is an array and removing null values. + # Otherwise, if the active property has a @container mapping to @list and element has a corresponding @list property, recursively compact that property's value passing a copy of the active context and the active property ensuring that the result is an array with all null values removed. + + # If there already exists a value for active property in element and the full IRI of property is also coerced to @list, return an error. + # FIXME: check for full-iri list coercion + + # Otherwise store the resulting array as value of active property if empty or property otherwise. compacted_key = context.compact_iri(k, :position => :predicate, :depth => @depth) v = depth { compact(element[k], property) } # Return either the result as an array, as an object with a key of @list (or appropriate alias from active context v = [v].compact unless v.is_a?(Array) @@ -64,21 +68,22 @@ # Otherwise, for each property and value in element: element.each do |key, value| debug("compact") {"#{key}: #{value.inspect}"} if %(@id @type).include?(key) + position = key == '@id' ? :subject : :type compacted_key = context.compact_iri(key, :position => :predicate, :depth => @depth) result[compacted_key] = case value when String # If value is a string, the compacted value is the result of performing IRI Compaction on value. debug {" => compacted string for #{key}"} - context.compact_iri(value, :position => :subject, :depth => @depth) + context.compact_iri(value, :position => position, :depth => @depth) when Array # Otherwise, value must be an array. Perform IRI Compaction on every entry of value. If value contains just one entry, value is set to that entry - compacted_value = value.map {|v| context.compact_iri(v, :position => :subject, :depth => @depth)} + compacted_value = value.map {|v| context.compact_iri(v, :position => position, :depth => @depth)} debug {" => compacted value(#{key}): #{compacted_value.inspect}"} - compacted_value = compacted_value.first if compacted_value.length == 1 + compacted_value = compacted_value.first if compacted_value.length == 1 && @options[:compactArrays] compacted_value end else if value.empty? # Make sure that an empty array is preserved