Module: JSON::LD::Utils
- Defined in:
- lib/json/ld/utils.rb
Instance Method Summary collapse
-
#add_value(subject, property, value, options = {}) ⇒ Object
Adds a value to a subject.
-
#as_resource(id, base = nil) ⇒ RDF::Resource
Represent an id as an IRI or Blank Node.
-
#blank_node?(value) ⇒ Boolean
Is value a blank node? Value is a blank node.
-
#compare_values(v1, v2) ⇒ Boolean
Compares two JSON-LD values for equality.
-
#has_property(subject, property) ⇒ Boolean
Returns True if the given subject has the given property.
-
#has_value(subject, property, value) ⇒ Boolean
Determines if the given value is a property of the given subject.
-
#index?(value) ⇒ Boolean
Is value annotated?.
-
#list?(value) ⇒ Boolean
Is value an expaned @list?.
-
#node?(value) ⇒ Boolean
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.
-
#node_reference?(value) ⇒ Boolean
Is value a node reference?.
-
#value?(value) ⇒ Boolean
Is value literal?.
Instance Method Details
#add_value(subject, property, value, options = {}) ⇒ Object
Adds a value to a subject. If the value is an array, all values in the array will be added.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/json/ld/utils.rb', line 122 def add_value(subject, property, value, = {}) = {property_is_array: false, allow_duplicate: true}.merge!() if value.is_a?(Array) subject[property] = [] if value.empty? && [:property_is_array] value.each {|v| add_value(subject, property, v, )} 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 |
#as_resource(id, base = nil) ⇒ RDF::Resource
Represent an id as an IRI or Blank Node
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/json/ld/utils.rb', line 72 def as_resource(id, base = nil) @nodes ||= {} # Re-use BNodes if id[0,2] == '_:' (@nodes[id] ||= RDF::Node.new(namer.get_sym(id))) elsif base base.join(id) else RDF::URI(id) end end |
#blank_node?(value) ⇒ Boolean
Is value a blank node? Value is a blank node
31 32 33 34 35 36 37 38 |
# File 'lib/json/ld/utils.rb', line 31 def blank_node?(value) case value when nil then true when String then value[0,2] == '_:' else (node?(value) || node_reference?(value)) && value.fetch('@id', '_:')[0,2] == '_:' end end |
#compare_values(v1, v2) ⇒ Boolean
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.
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/json/ld/utils.rb', line 96 def compare_values(v1, v2) case when node?(v1) && node?(v2) then v1['@id'] && v1['@id'] == v2['@id'] when 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 |
#has_property(subject, property) ⇒ Boolean
Returns True if the given subject has the given property.
148 149 150 151 |
# File 'lib/json/ld/utils.rb', line 148 def has_property(subject, property) return false unless value = subject[property] !value.is_a?(Array) || !value.empty? end |
#has_value(subject, property, value) ⇒ Boolean
Determines if the given value is a property of the given subject.
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/json/ld/utils.rb', line 160 def has_value(subject, property, value) if has_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 |
#index?(value) ⇒ Boolean
Is value annotated?
54 55 56 |
# File 'lib/json/ld/utils.rb', line 54 def index?(value) value.is_a?(Hash) && value.has_key?('@index') end |
#list?(value) ⇒ Boolean
Is value an expaned @list?
45 46 47 |
# File 'lib/json/ld/utils.rb', line 45 def list?(value) value.is_a?(Hash) && value.has_key?('@list') end |
#node?(value) ⇒ Boolean
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
12 13 14 15 16 |
# File 'lib/json/ld/utils.rb', line 12 def node?(value) value.is_a?(Hash) && !(value.has_key?('@value') || value.has_key?('@list') || value.has_key?('@set')) && (value.length > 1 || !value.has_key?('@id')) end |
#node_reference?(value) ⇒ Boolean
Is value a node reference?
22 23 24 |
# File 'lib/json/ld/utils.rb', line 22 def node_reference?(value) value.is_a?(Hash) && value.keys == %w(@id) end |
#value?(value) ⇒ Boolean
Is value literal?
63 64 65 |
# File 'lib/json/ld/utils.rb', line 63 def value?(value) value.is_a?(Hash) && value.has_key?('@value') end |