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) ⇒ TermDefinition

Create a new Term Mapping with an ID



40
41
42
43
# File 'lib/json/ld/context.rb', line 40

def initialize(term, id = nil)
  @term = term
  @id = id.to_s if id
end

Instance Attribute Details

#container_mappingString



20
21
22
# File 'lib/json/ld/context.rb', line 20

def container_mapping
  @container_mapping
end

#idRDF::URI



11
12
13
# File 'lib/json/ld/context.rb', line 11

def id
  @id
end

#language_mappingString

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



24
25
26
# File 'lib/json/ld/context.rb', line 24

def language_mapping
  @language_mapping
end

#reverse_propertyBoolean



27
28
29
# File 'lib/json/ld/context.rb', line 27

def reverse_property
  @reverse_property
end

#simpleBoolean

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



31
32
33
# File 'lib/json/ld/context.rb', line 31

def simple
  @simple
end

#termString



14
15
16
# File 'lib/json/ld/context.rb', line 14

def term
  @term
end

#type_mappingString



17
18
19
# File 'lib/json/ld/context.rb', line 17

def type_mapping
  @type_mapping
end

Instance Method Details

#inspectObject



83
84
85
86
87
88
89
90
91
92
# File 'lib/json/ld/context.rb', line 83

def inspect
  v = %w([TD)
  v << "id=#{@id}"
  v << "term=#{@term}"
  v << "rev" if reverse_property
  v << "container=#{container_mapping}" if container_mapping
  v << "lang=#{language_mapping.inspect}" unless language_mapping.nil?
  v << "type=#{type_mapping}" unless type_mapping.nil?
  v.join(" ") + "]"
end

#simple?Boolean

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



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

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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/json/ld/context.rb', line 50

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 language_mapping.nil? &&
     container_mapping.nil? &&
     type_mapping.nil? &&
     reverse_property.nil?

     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
    defn['@container'] = container_mapping if container_mapping
    # Language set as false to be output as null
    defn['@language'] = (language_mapping ? language_mapping : nil) unless language_mapping.nil?
    defn
  end
end