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

Inherits:
Object
  • Object
show all
Defined in:
vendor/bundler/ruby/2.4.0/bundler/gems/json-ld-4e9cdb2fc35e/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, type_mapping: nil, container_mapping: nil, language_mapping: nil, reverse_property: false, nest: nil, simple: false, prefix: nil, context: nil) ⇒ TermDefinition

Create a new Term Mapping with an ID

Parameters:

  • term (String)
  • id (String)
  • type_mapping (String)

    Type mapping

  • container_mapping ('@index', '@language', '@index', '@set', '@type', '@id')
  • language_mapping (String)

    Language mapping of term, false is used if there is explicitly no language mapping for this term

  • reverse_property (Boolean)
  • nest (String)

    term used for nest properties

  • simple (Boolean)

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

  • prefix (Boolean)

    Term may be used as a prefix



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/json-ld-4e9cdb2fc35e/lib/json/ld/context.rb', line 90

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

Instance Attribute Details

#as_setBoolean (readonly)

If container mapping was defined along with @set

Returns:

  • (Boolean)


46
47
48
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/json-ld-4e9cdb2fc35e/lib/json/ld/context.rb', line 46

def as_set
  @as_set
end

#container_mapping'@index', ...

Base container mapping, without @set

Returns:

  • ('@index', '@language', '@index', '@type', '@id')

    Container mapping



42
43
44
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/json-ld-4e9cdb2fc35e/lib/json/ld/context.rb', line 42

def container_mapping
  @container_mapping
end

#contextHash{String => Object}

Term-specific context

Returns:



67
68
69
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/json-ld-4e9cdb2fc35e/lib/json/ld/context.rb', line 67

def context
  @context
end

#idRDF::URI

Returns IRI map

Returns:



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

def id
  @id
end

#language_mappingString

Language mapping of term, false is used if there is explicitly no language mapping for this term.

Returns:

  • (String)

    Language mapping



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

def language_mapping
  @language_mapping
end

#nestString

Returns Term used for nest properties

Returns:

  • (String)

    Term used for nest properties



49
50
51
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/json-ld-4e9cdb2fc35e/lib/json/ld/context.rb', line 49

def nest
  @nest
end

#prefix=(value) ⇒ Object (writeonly)

Indicate that term may be used as a prefix



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

def prefix=(value)
  @prefix = value
end

#reverse_propertyBoolean

Returns Reverse Property

Returns:

  • (Boolean)

    Reverse Property



56
57
58
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/json-ld-4e9cdb2fc35e/lib/json/ld/context.rb', line 56

def reverse_property
  @reverse_property
end

#simpleBoolean

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

Returns:

  • (Boolean)


60
61
62
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/json-ld-4e9cdb2fc35e/lib/json/ld/context.rb', line 60

def simple
  @simple
end

#termString

Returns term name

Returns:



35
36
37
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/json-ld-4e9cdb2fc35e/lib/json/ld/context.rb', line 35

def term
  @term
end

#type_mappingString

Returns Type mapping

Returns:



38
39
40
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/json-ld-4e9cdb2fc35e/lib/json/ld/context.rb', line 38

def type_mapping
  @type_mapping
end

Instance Method Details

#inspectObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/json-ld-4e9cdb2fc35e/lib/json/ld/context.rb', line 179

def inspect
  v = %w([TD)
  v << "id=#{@id}"
  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 << "type=#{type_mapping}" unless type_mapping.nil?
  v << "nest=#{nest.inspect}" unless nest.nil?
  v << "simple=true" if @simple
  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:

  • (Boolean)

    simple



75
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/json-ld-4e9cdb2fc35e/lib/json/ld/context.rb', line 75

def prefix?; @prefix; end

#simple?Boolean

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

Returns:

  • (Boolean)

    simple



71
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/json-ld-4e9cdb2fc35e/lib/json/ld/context.rb', line 71

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:



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/json-ld-4e9cdb2fc35e/lib/json/ld/context.rb', line 127

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 = [container_mapping, ('@set' if as_set)].compact
    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['@context'] = @context if @context
    defn['@nest'] = @nest if @nest
    defn['@prefix'] = @prefix unless @prefix.nil? || (context.processingMode || 'json-ld-1.0') == 'json-ld-1.0'
    defn
  end
end

#to_rbString

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

Returns:



166
167
168
169
170
171
172
173
174
175
176
177
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/json-ld-4e9cdb2fc35e/lib/json/ld/context.rb', line 166

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