module JSON::LD::Utils
Constants
- UTIL_GRAPH_KEYS
Public Instance Methods
Adds a value to a subject. If the value is an array, all values in the array will be added.
@param [Hash] subject the hash to add the value to. @param [String] property the property that relates the value to the subject. @param [Object] value the value to add. @param [Boolean] property_is_array (false)
true if the property is always an array, false if not.
@param [Boolean] value_is_array (false)
true if the value to be added should be preserved as an array (lists)
@param [Boolean] allow_duplicate (true)
true to allow duplicates, false not to (uses a simple shallow comparison of subject ID or value).
# File lib/json/ld/utils.rb, line 163 def add_value(subject, property, value, property_is_array: false, value_is_array: false, allow_duplicate: true) if value_is_array subject[property] = value elsif value.is_a?(Array) subject[property] = [] if value.empty? && property_is_array value.each do |v| add_value(subject, property, v, property_is_array: property_is_array, allow_duplicate: allow_duplicate) end elsif subject[property] # check if subject already has value if duplicates not allowed _has_value = !allow_duplicate && has_value?(subject, property, value) # make property an array if value not present or always an array if !subject[property].is_a?(Array) && (!_has_value || property_is_array) subject[property] = [subject[property]] end subject[property] << value unless _has_value else subject[property] = property_is_array ? [value] : value end end
Represent as an array @param [Object] object @return [Array<Object>]
# File lib/json/ld/utils.rb, line 120 def as_array(object) object.is_a?(Array) ? object : [object] end
Represent an id as an IRI or Blank Node @param [String] id @param [RDF::URI] base (nil) @return [RDF::Resource]
# File lib/json/ld/utils.rb, line 105 def as_resource(id, base = nil) @nodes ||= {} # Re-use BNodes if id.start_with?('_:') (@nodes[id] ||= RDF::Node.new(namer.get_sym(id))) elsif base base.join(id) else RDF::URI(id) end end
Is value a blank node? Value is a blank node
@param [Object] value @return [Boolean]
# File lib/json/ld/utils.rb, line 41 def blank_node?(value) case value when nil then true when String then value.start_with?('_:') else (node?(value) || node_reference?(value)) && value.fetch('@id', '_:').start_with?('_:') end end
Compares two JSON-LD values for equality. Two JSON-LD values will be considered equal if:
-
They are both primitives of the same type and value.
-
They are both @values with the same @value, @type, @language,
and @index, OR
-
They both have @ids that are the same.
@param [Object] v1 the first value. @param [Object] v2 the second value.
@return [Boolean] v1 and v2 are considered equal
# File lib/json/ld/utils.rb, line 137 def compare_values(v1, v2) if node_or_ref?(v1) && node_or_ref?(v2) v1['@id'] && v1['@id'] == v2['@id'] elsif value?(v1) && value?(v2) v1['@value'] == v2['@value'] && v1['@type'] == v2['@type'] && v1['@language'] == v2['@language'] && v1['@index'] == v2['@index'] else v1 == v2 end end
Is value an expaned @graph?
Note: A value is a graph if all of these hold true:
-
It is an object.
-
It has an ‘@graph` key.
-
It may have ‘@context’, ‘@id’ or ‘@index’
@param [Object] value @return [Boolean]
# File lib/json/ld/utils.rb, line 60 def graph?(value) value.is_a?(Hash) && (value.keys - UTIL_GRAPH_KEYS) == ['@graph'] end
Determines if the given value is a property of the given subject.
@param [Hash] subject the subject to check. @param [String] property the property to check. @param [Object] value the value to check.
@return [Boolean] true if the value exists, false if not.
# File lib/json/ld/utils.rb, line 206 def has_value?(subject, property, value) if property?(subject, property) val = subject[property] is_list = list?(val) if val.is_a?(Array) || is_list val = val['@list'] if is_list val.any? { |v| compare_values(value, v) } elsif !val.is_a?(Array) compare_values(value, val) else false end else false end end
Is value annotated?
@param [Object] value @return [Boolean]
# File lib/json/ld/utils.rb, line 87 def index?(value) value.is_a?(Hash) && value.key?('@index') end
Is value an expaned @list?
@param [Object] value @return [Boolean]
# File lib/json/ld/utils.rb, line 78 def list?(value) value.is_a?(Hash) && value.key?('@list') end
Is value a node? A value is a node if
-
it is a Hash
-
it is not a @value, @set or @list
-
it has more than 1 key or any key is not @id
@param [Object] value @return [Boolean]
# File lib/json/ld/utils.rb, line 13 def node?(value) value.is_a?(Hash) && !(value.key?('@value') || value.key?('@list') || value.key?('@set')) && (value.length > 1 || !value.key?('@id')) end
Is value a node or a node reference reference? @param [Object] value @return [Boolean]
# File lib/json/ld/utils.rb, line 31 def node_or_ref?(value) value.is_a?(Hash) && !(value.key?('@value') || value.key?('@list') || value.key?('@set')) end
Is value a node reference? @param [Object] value @return [Boolean]
# File lib/json/ld/utils.rb, line 23 def node_reference?(value) value.is_a?(Hash) && value.length == 1 && value.key?('@id') end
Returns True if the given subject has the given property.
@param subject the subject to check. @param property the property to look for.
@return [Boolean] true if the subject has the given property, false if not.
# File lib/json/ld/utils.rb, line 193 def property?(subject, property) return false unless (value = subject[property]) !value.is_a?(Array) || !value.empty? end
Is value a simple graph (lacking @id)?
@param [Object] value @return [Boolean]
# File lib/json/ld/utils.rb, line 69 def simple_graph?(value) graph?(value) && !value.key?('@id') end
Is value literal?
@param [Object] value @return [Boolean]
# File lib/json/ld/utils.rb, line 96 def value?(value) value.is_a?(Hash) && value.key?('@value') end
Private Instance Methods
Merge the last value into an array based for the specified key if hash is not null and value is not already in that array
# File lib/json/ld/utils.rb, line 228 def merge_value(hash, key, value) return unless hash values = hash[key] ||= [] if key == '@list' values << value elsif list?(value) values << value elsif !values.include?(value) values << value end end