Class: JSON::LD::Context::TermDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/json/ld/context.rb

Overview

Term Definitions specify how properties and values have to be interpreted as well as the current vocabulary mapping and the default language

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(term, id: nil, index: nil, type_mapping: nil, container_mapping: nil, language_mapping: nil, direction_mapping: nil, reverse_property: false, nest: nil, protected: nil, simple: false, prefix: nil, context: nil) ⇒ TermDefinition

Create a new Term Mapping with an ID

Parameters:

  • term (String)
  • id (String) (defaults to: nil)
  • type_mapping (String) (defaults to: nil)

    Type mapping

  • container_mapping (Set<'@index', '@language', '@index', '@set', '@type', '@id', '@graph'>) (defaults to: nil)
  • language_mapping (String) (defaults to: nil)

    Language mapping of term, ‘false` is used if there is an explicit language mapping for this term

  • direction_mapping ("ltr", "rtl") (defaults to: nil)

    Direction mapping of term, ‘false` is used if there is an explicit direction mapping for this term

  • reverse_property (Boolean) (defaults to: false)
  • protected (Boolean) (defaults to: nil)

    mark resulting context as protected

  • nest (String) (defaults to: nil)

    term used for nest properties

  • simple (Boolean) (defaults to: false)

    This is a simple term definition, not an expanded term definition

  • prefix (Boolean) (defaults to: nil)

    Term may be used as a prefix



2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
# File 'lib/json/ld/context.rb', line 2178

def initialize(term,
               id: nil,
               index: nil,
               type_mapping: nil,
               container_mapping: nil,
               language_mapping: nil,
               direction_mapping: nil,
               reverse_property: false,
               nest: nil,
               protected: nil,
               simple: false,
               prefix: nil,
               context: nil)
  @term                   = term
  @id                     = id.to_s           unless id.nil?
  @index                  = index.to_s        unless index.nil?
  @type_mapping           = type_mapping.to_s unless type_mapping.nil?
  self.container_mapping  = container_mapping
  @language_mapping       = language_mapping  unless language_mapping.nil?
  @direction_mapping      = direction_mapping unless direction_mapping.nil?
  @reverse_property       = reverse_property
  @protected              = protected
  @nest                   = nest unless nest.nil?
  @simple                 = simple
  @prefix                 = prefix            unless prefix.nil?
  @context                = context           unless context.nil?
end

Instance Attribute Details

#container_mappingArray<'@index', '@language', '@index', '@set', '@type', '@id', '@graph'>

Base container mapping, without @set

Returns:



2115
2116
2117
# File 'lib/json/ld/context.rb', line 2115

def container_mapping
  @container_mapping
end

#contextHash{String => Object}

Term-specific context

Returns:

  • (Hash{String => Object})


2144
2145
2146
# File 'lib/json/ld/context.rb', line 2144

def context
  @context
end

#direction_mapping"ltr", "rtl"

Direction of term, ‘false` is used if there is explicit direction mapping mapping for this term.

Returns:

  • ("ltr", "rtl")

    direction_mapping



2126
2127
2128
# File 'lib/json/ld/context.rb', line 2126

def direction_mapping
  @direction_mapping
end

#idRDF::URI

Returns IRI map.

Returns:



2105
2106
2107
# File 'lib/json/ld/context.rb', line 2105

def id
  @id
end

#indexBoolean

Property used for data indexing; defaults to @index

Returns:

  • (Boolean)


2137
2138
2139
# File 'lib/json/ld/context.rb', line 2137

def index
  @index
end

#language_mappingString

Language mapping of term, ‘false` is used if there is an explicit language mapping for this term.

Returns:

  • (String)

    Language mapping



2122
2123
2124
# File 'lib/json/ld/context.rb', line 2122

def language_mapping
  @language_mapping
end

#nestString

Returns Term used for nest properties.

Returns:

  • (String)

    Term used for nest properties



2118
2119
2120
# File 'lib/json/ld/context.rb', line 2118

def nest
  @nest
end

#prefix=(value) ⇒ Object (writeonly)

Indicate that term may be used as a prefix



2140
2141
2142
# File 'lib/json/ld/context.rb', line 2140

def prefix=(value)
  @prefix = value
end

#protected=(value) ⇒ Boolean (writeonly)

Term is protected.

Returns:

  • (Boolean)


2148
2149
2150
# File 'lib/json/ld/context.rb', line 2148

def protected=(value)
  @protected = value
end

#reverse_propertyBoolean

Returns Reverse Property.

Returns:

  • (Boolean)

    Reverse Property



2129
2130
2131
# File 'lib/json/ld/context.rb', line 2129

def reverse_property
  @reverse_property
end

#simpleBoolean

This is a simple term definition, not an expanded term definition

Returns:

  • (Boolean)


2133
2134
2135
# File 'lib/json/ld/context.rb', line 2133

def simple
  @simple
end

#termString

Returns term name.

Returns:

  • (String)

    term name



2108
2109
2110
# File 'lib/json/ld/context.rb', line 2108

def term
  @term
end

#type_mappingString

Returns Type mapping.

Returns:

  • (String)

    Type mapping



2111
2112
2113
# File 'lib/json/ld/context.rb', line 2111

def type_mapping
  @type_mapping
end

Instance Method Details

#==(other) ⇒ Boolean

Check if term definitions are identical, modulo @protected

Returns:

  • (Boolean)


2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
# File 'lib/json/ld/context.rb', line 2317

def ==(other)
  other.is_a?(TermDefinition) &&
    id == other.id &&
    term == other.term &&
    type_mapping == other.type_mapping &&
    container_mapping == other.container_mapping &&
    nest == other.nest &&
    language_mapping == other.language_mapping &&
    direction_mapping == other.direction_mapping &&
    reverse_property == other.reverse_property &&
    index == other.index &&
    context == other.context &&
    prefix? == other.prefix? &&
    as_set? == other.as_set?
end

#as_set?Boolean

If container mapping was defined along with @set

Returns:

  • (Boolean)


2311
2312
2313
# File 'lib/json/ld/context.rb', line 2311

def as_set?
  @as_set || false
end

#inspectObject



2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
# File 'lib/json/ld/context.rb', line 2333

def inspect
  v = %w([TD)
  v << "id=#{@id}"
  v << "index=#{index.inspect}" unless index.nil?
  v << "term=#{@term}"
  v << "rev" if reverse_property
  v << "container=#{container_mapping}" if container_mapping
  v << "as_set=#{as_set?.inspect}"
  v << "lang=#{language_mapping.inspect}" unless language_mapping.nil?
  v << "dir=#{direction_mapping.inspect}" unless direction_mapping.nil?
  v << "type=#{type_mapping}" unless type_mapping.nil?
  v << "nest=#{nest.inspect}" unless nest.nil?
  v << "simple=true" if @simple
  v << "protected=true" if @protected
  v << "prefix=#{@prefix.inspect}" unless @prefix.nil?
  v << "has-context" unless context.nil?
  v.join(" ") + "]"
end

#match_compact_iri?(iri) ⇒ Boolean

Returns true if the term matches a compact IRI

Parameters:

  • iri (String)

    the compact IRI

Returns:

  • (Boolean)


2224
2225
2226
# File 'lib/json/ld/context.rb', line 2224

def match_compact_iri?(iri)
  iri.start_with?(prefix_colon)
end

#match_iri?(iri) ⇒ Boolean

Returns true if the term matches a IRI

Parameters:

  • iri (String)

    the IRI

Returns:

  • (Boolean)


2216
2217
2218
# File 'lib/json/ld/context.rb', line 2216

def match_iri?(iri)
  iri.start_with?(id)
end

#prefix?Boolean

This is an appropriate term to use as the prefix of a compact IRI

Returns:

  • (Boolean)

    simple



2158
2159
2160
# File 'lib/json/ld/context.rb', line 2158

def prefix?
  @prefix
end

#protected?Boolean

Term is protected.

Returns:

  • (Boolean)


2208
2209
2210
# File 'lib/json/ld/context.rb', line 2208

def protected?
  !!@protected
end

#simple?Boolean

This is a simple term definition, not an expanded term definition

Returns:

  • (Boolean)

    simple



2152
2153
2154
# File 'lib/json/ld/context.rb', line 2152

def simple?
  simple
end

#to_context_definition(context) ⇒ String, Hash{String => Array[String], String}

Output Hash or String definition for this definition considering @language and @vocab

Parameters:

Returns:

  • (String, Hash{String => Array[String], String})


2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
# File 'lib/json/ld/context.rb', line 2251

def to_context_definition(context)
  cid = if context.vocab && id.start_with?(context.vocab)
    # Nothing to return unless it's the same as the vocab
    id == context.vocab ? context.vocab : id.to_s[context.vocab.length..]
  else
    # Find a term to act as a prefix
    iri, prefix = context.iri_to_term.detect { |i, _p| id.to_s.start_with?(i.to_s) }
    iri && iri != id ? "#{prefix}:#{id.to_s[iri.length..]}" : id
  end

  if simple?
    cid.to_s unless cid == term && context.vocab
  else
    defn = {}
    defn[reverse_property ? '@reverse' : '@id'] = cid.to_s unless cid == term && !reverse_property
    if type_mapping
      defn['@type'] = if KEYWORDS.include?(type_mapping)
        type_mapping
      else
        context.compact_iri(type_mapping, vocab: true)
      end
    end

    cm = Array(container_mapping)
    cm << "@set" if as_set? && !cm.include?("@set")
    cm = cm.first if cm.length == 1
    defn['@container'] = cm unless cm.empty?
    # Language set as false to be output as null
    defn['@language'] = (@language_mapping || nil) unless @language_mapping.nil?
    defn['@direction'] = (@direction_mapping || nil) unless @direction_mapping.nil?
    defn['@context'] = @context if @context
    defn['@nest'] = @nest if @nest
    defn['@index'] = @index if @index
    defn['@prefix'] = @prefix unless @prefix.nil?
    defn
  end
end

#to_rbString

Turn this into a source for a new instantiation FIXME: context serialization

Returns:

  • (String)


2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
# File 'lib/json/ld/context.rb', line 2293

def to_rb
  defn = [%(TermDefinition.new\(#{term.inspect})]
  %w[id index type_mapping container_mapping language_mapping direction_mapping reverse_property nest simple
     prefix context protected].each do |acc|
    v = instance_variable_get("@#{acc}".to_sym)
    v = v.to_s if v.is_a?(RDF::Term)
    if acc == 'container_mapping'
      v = v.to_a
      v << '@set' if as_set?
      v = v.first if v.length <= 1
    end
    defn << "#{acc}: #{v.inspect}" if v
  end
  defn.join(', ') + ")"
end