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:

  • (defaults to: nil)
  • (defaults to: nil)

    Type mapping

  • (defaults to: nil)
  • (defaults to: nil)

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

  • (defaults to: nil)

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

  • (defaults to: false)
  • (defaults to: nil)

    mark resulting context as protected

  • (defaults to: nil)

    term used for nest properties

  • (defaults to: false)

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

  • (defaults to: nil)

    Term may be used as a prefix



2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
# File 'lib/json/ld/context.rb', line 2065

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:

  • Container mapping



2006
2007
2008
# File 'lib/json/ld/context.rb', line 2006

def container_mapping
  @container_mapping
end

#contextHash{String => Object}

Term-specific context

Returns:



2035
2036
2037
# File 'lib/json/ld/context.rb', line 2035

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:

  • direction_mapping



2017
2018
2019
# File 'lib/json/ld/context.rb', line 2017

def direction_mapping
  @direction_mapping
end

#idRDF::URI

Returns IRI map.

Returns:

  • IRI map



1996
1997
1998
# File 'lib/json/ld/context.rb', line 1996

def id
  @id
end

#indexBoolean

Property used for data indexing; defaults to @index

Returns:



2028
2029
2030
# File 'lib/json/ld/context.rb', line 2028

def index
  @index
end

#language_mappingString

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

Returns:

  • Language mapping



2013
2014
2015
# File 'lib/json/ld/context.rb', line 2013

def language_mapping
  @language_mapping
end

#nestString

Returns Term used for nest properties.

Returns:

  • Term used for nest properties



2009
2010
2011
# File 'lib/json/ld/context.rb', line 2009

def nest
  @nest
end

#prefix=(value) ⇒ Object (writeonly)

Indicate that term may be used as a prefix



2031
2032
2033
# File 'lib/json/ld/context.rb', line 2031

def prefix=(value)
  @prefix = value
end

#protected=(value) ⇒ Boolean (writeonly)

Term is protected.

Returns:



2039
2040
2041
# File 'lib/json/ld/context.rb', line 2039

def protected=(value)
  @protected = value
end

#reverse_propertyBoolean

Returns Reverse Property.

Returns:

  • Reverse Property



2020
2021
2022
# File 'lib/json/ld/context.rb', line 2020

def reverse_property
  @reverse_property
end

#simpleBoolean

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

Returns:



2024
2025
2026
# File 'lib/json/ld/context.rb', line 2024

def simple
  @simple
end

#termString

Returns term name.

Returns:

  • term name



1999
2000
2001
# File 'lib/json/ld/context.rb', line 1999

def term
  @term
end

#type_mappingString

Returns Type mapping.

Returns:

  • Type mapping



2002
2003
2004
# File 'lib/json/ld/context.rb', line 2002

def type_mapping
  @type_mapping
end

Instance Method Details

#==(other) ⇒ Boolean

Check if term definitions are identical, modulo @protected

Returns:



2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
# File 'lib/json/ld/context.rb', line 2183

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 &&
  simple == other.simple &&
  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:



2179
# File 'lib/json/ld/context.rb', line 2179

def as_set?; @as_set || false; end

#inspectObject



2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
# File 'lib/json/ld/context.rb', line 2200

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

#prefix?Boolean

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

Returns:

  • simple



2047
# File 'lib/json/ld/context.rb', line 2047

def prefix?; @prefix; end

#protected?Boolean

Term is protected.

Returns:



2095
# File 'lib/json/ld/context.rb', line 2095

def protected?; !!@protected; end

#simple?Boolean

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

Returns:

  • simple



2043
# File 'lib/json/ld/context.rb', line 2043

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:



2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
# File 'lib/json/ld/context.rb', line 2120

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..-1]
  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..-1]}" : 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 ? @language_mapping : nil) unless @language_mapping.nil?
    defn['@direction'] = (@direction_mapping ? @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:



2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
# File 'lib/json/ld/context.rb', line 2162

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