Class: Lutaml::JsonLd::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/jsonld/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeContext

Returns a new instance of Context.



8
9
10
11
12
13
14
# File 'lib/lutaml/jsonld/context.rb', line 8

def initialize
  @prefixes = {}
  @terms = {}
  @vocab = nil
  @language = nil
  @base = nil
end

Instance Attribute Details

#prefixesObject (readonly)

Returns the value of attribute prefixes.



6
7
8
# File 'lib/lutaml/jsonld/context.rb', line 6

def prefixes
  @prefixes
end

#termsObject (readonly)

Returns the value of attribute terms.



6
7
8
# File 'lib/lutaml/jsonld/context.rb', line 6

def terms
  @terms
end

Instance Method Details

#base(uri = nil) ⇒ Object



30
31
32
33
# File 'lib/lutaml/jsonld/context.rb', line 30

def base(uri = nil)
  @base = uri if uri
  @base
end

#language(lang = nil) ⇒ Object



25
26
27
28
# File 'lib/lutaml/jsonld/context.rb', line 25

def language(lang = nil)
  @language = lang if lang
  @language
end

#prefix(namespace_class) ⇒ Object



16
17
18
# File 'lib/lutaml/jsonld/context.rb', line 16

def prefix(namespace_class)
  @prefixes[namespace_class.prefix] = namespace_class.uri
end

#resolve(term_name) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/lutaml/jsonld/context.rb', line 57

def resolve(term_name)
  if term_name.include?(":")
    pfx, local = term_name.split(":", 2)
    "#{@prefixes[pfx]}#{local}" if @prefixes.key?(pfx)
  elsif @terms.key?(term_name)
    @terms[term_name].id
  elsif @vocab
    "#{@vocab}#{term_name}"
  end
end

#term(name, id: nil, type: nil, container: nil, language: nil, reverse: false) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/lutaml/jsonld/context.rb', line 35

def term(name, id: nil, type: nil, container: nil, language: nil,
reverse: false)
  @terms[name] = TermDefinition.new(
    name: name,
    id: id,
    type: type,
    container: container,
    language: language,
    reverse: reverse,
  )
end

#to_hashObject



47
48
49
50
51
52
53
54
55
# File 'lib/lutaml/jsonld/context.rb', line 47

def to_hash
  ctx = {}
  ctx["@vocab"] = @vocab if @vocab
  ctx["@language"] = @language if @language
  ctx["@base"] = @base if @base
  @prefixes.each { |pfx, uri| ctx[pfx] = uri }
  @terms.each_value { |td| ctx.merge!(td.to_context_hash) }
  ctx
end

#vocab(uri = nil) ⇒ Object



20
21
22
23
# File 'lib/lutaml/jsonld/context.rb', line 20

def vocab(uri = nil)
  @vocab = uri if uri
  @vocab
end