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, type_mapping: nil, container_mapping: nil, language_mapping: nil, reverse_property: false, simple: false) ⇒ 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', '@list') (defaults to: nil)
  • language_mapping (String) (defaults to: nil)

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

  • reverse_property (Boolean) (defaults to: false)
  • simple (Boolean) (defaults to: false)

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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/json/ld/context.rb', line 67

def initialize(term,
              id: nil,
              type_mapping: nil,
              container_mapping: nil,
              language_mapping: nil,
              reverse_property: false,
              simple: false)
  @term               = term
  @id                 = id.to_s           if id
  @type_mapping       = type_mapping.to_s if type_mapping
  @container_mapping  = container_mapping if container_mapping
  @language_mapping   = language_mapping  if language_mapping
  @reverse_property   = reverse_property  if reverse_property
  @simple             = simple            if simple
end

Instance Attribute Details

#container_mapping'@set', '@list'

Returns Container mapping.

Returns:

  • ('@set', '@list')

    Container mapping



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

def container_mapping
  @container_mapping
end

#idRDF::URI

Returns IRI map.

Returns:

  • (RDF::URI)

    IRI map



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

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



44
45
46
# File 'lib/json/ld/context.rb', line 44

def language_mapping
  @language_mapping
end

#reverse_propertyBoolean

Returns Reverse Property.

Returns:

  • (Boolean)

    Reverse Property



47
48
49
# File 'lib/json/ld/context.rb', line 47

def reverse_property
  @reverse_property
end

#simpleBoolean

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

Returns:

  • (Boolean)

    simple



51
52
53
# File 'lib/json/ld/context.rb', line 51

def simple
  @simple
end

#termString

Returns term name.

Returns:

  • (String)

    term name



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

def term
  @term
end

#type_mappingString

Returns Type mapping.

Returns:

  • (String)

    Type mapping



37
38
39
# File 'lib/json/ld/context.rb', line 37

def type_mapping
  @type_mapping
end

Instance Method Details

#inspectObject



134
135
136
137
138
139
140
141
142
143
# File 'lib/json/ld/context.rb', line 134

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

Returns:

  • (Boolean)

    simple



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

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})


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/json/ld/context.rb', line 88

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

#to_rbString

Turn this into a source for a new instantiation

Returns:

  • (String)


124
125
126
127
128
129
130
131
132
# File 'lib/json/ld/context.rb', line 124

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