Module: JSON::LD::Utils

Included in:
Compact, Context, Expand, Flatten, Frame, FromRDF, ToRDF, Writer
Defined in:
vendor/bundler/ruby/2.4.0/bundler/gems/json-ld-4e9cdb2fc35e/lib/json/ld/utils.rb

Instance Method Summary collapse

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.

Parameters:

  • subject (Hash)

    the hash to add the value to.

  • property (String)

    the property that relates the value to the subject.

  • value (Object)

    the value to add.

  • options (Hash{Symbol => Object}) (defaults to: {})

Options Hash (options):

  • :property_is_array (Boolean)

    true if the property is always (false) an array, false if not.

  • :allow_duplicate (Boolean) — default: true

    true to allow duplicates, false not to (uses a simple shallow comparison of subject ID or value).



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/json-ld-4e9cdb2fc35e/lib/json/ld/utils.rb', line 131

def add_value(subject, property, value, options = {})
  options = {property_is_array: false, allow_duplicate: true}.merge!(options)

  if value.is_a?(Array)
    subject[property] = [] if value.empty? && options[:property_is_array]
    value.each {|v| add_value(subject, property, v, options)}
  elsif subject[property]
    # check if subject already has value if duplicates not allowed
    _has_value = !options[: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 || options[:property_is_array])
      subject[property] = [subject[property]]
    end
    subject[property] << value unless _has_value
  else
    subject[property] = options[:property_is_array] ? [value] : value
  end
end

#as_resource(id, base = nil) ⇒ RDF::Resource

Represent an id as an IRI or Blank Node

Parameters:

Returns:



81
82
83
84
85
86
87
88
89
90
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/json-ld-4e9cdb2fc35e/lib/json/ld/utils.rb', line 81

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

#blank_node?(value) ⇒ Boolean

Is value a blank node? Value is a blank node

Parameters:

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/json-ld-4e9cdb2fc35e/lib/json/ld/utils.rb', line 40

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

#compare_values(v1, v2) ⇒ Boolean

Compares two JSON-LD values for equality. Two JSON-LD values will be considered equal if:

  1. They are both primitives of the same type and value.
  2. They are both @values with the same @value, @type, @language, and @index, OR
  3. They both have @ids that are the same.

Parameters:

  • v1 (Object)

    the first value.

  • v2 (Object)

    the second value.

Returns:

  • (Boolean)

    v1 and v2 are considered equal



105
106
107
108
109
110
111
112
113
114
115
116
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/json-ld-4e9cdb2fc35e/lib/json/ld/utils.rb', line 105

def compare_values(v1, v2)
  case
  when node_or_ref?(v1) && node_or_ref?(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.

Parameters:

  • subject

    the subject to check.

  • property

    the property to look for.

Returns:

  • (Boolean)

    true if the subject has the given property, false if not.



157
158
159
160
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/json-ld-4e9cdb2fc35e/lib/json/ld/utils.rb', line 157

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.

Parameters:

  • subject (Hash)

    the subject to check.

  • property (String)

    the property to check.

  • value (Object)

    the value to check.

Returns:

  • (Boolean)

    true if the value exists, false if not.



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/json-ld-4e9cdb2fc35e/lib/json/ld/utils.rb', line 169

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?

Parameters:

Returns:

  • (Boolean)


63
64
65
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/json-ld-4e9cdb2fc35e/lib/json/ld/utils.rb', line 63

def index?(value)
  value.is_a?(Hash) && value.has_key?('@index')
end

#list?(value) ⇒ Boolean

Is value an expaned @list?

Parameters:

Returns:

  • (Boolean)


54
55
56
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/json-ld-4e9cdb2fc35e/lib/json/ld/utils.rb', line 54

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

Parameters:

Returns:

  • (Boolean)


12
13
14
15
16
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/json-ld-4e9cdb2fc35e/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_or_ref?(value) ⇒ Boolean

Is value a node or a node reference reference?

Parameters:

Returns:

  • (Boolean)


30
31
32
33
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/json-ld-4e9cdb2fc35e/lib/json/ld/utils.rb', line 30

def node_or_ref?(value)
  value.is_a?(Hash) &&
    !(value.has_key?('@value') || value.has_key?('@list') || value.has_key?('@set'))
end

#node_reference?(value) ⇒ Boolean

Is value a node reference?

Parameters:

Returns:

  • (Boolean)


22
23
24
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/json-ld-4e9cdb2fc35e/lib/json/ld/utils.rb', line 22

def node_reference?(value)
  value.is_a?(Hash) && value.length == 1 && value.key?('@id')
end

#value?(value) ⇒ Boolean

Is value literal?

Parameters:

Returns:

  • (Boolean)


72
73
74
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/json-ld-4e9cdb2fc35e/lib/json/ld/utils.rb', line 72

def value?(value)
  value.is_a?(Hash) && value.has_key?('@value')
end