Class: Lutaml::Rdf::Literal

Inherits:
Object
  • Object
show all
Includes:
LanguageTagged
Defined in:
lib/lutaml/rdf/literal.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LanguageTagged

#language_tag

Constructor Details

#initialize(value, datatype: nil, language: nil) ⇒ Literal

Returns a new instance of Literal.



10
11
12
13
14
# File 'lib/lutaml/rdf/literal.rb', line 10

def initialize(value, datatype: nil, language: nil)
  @value = value
  @datatype = datatype
  @language = language
end

Instance Attribute Details

#datatypeObject (readonly)

Returns the value of attribute datatype.



8
9
10
# File 'lib/lutaml/rdf/literal.rb', line 8

def datatype
  @datatype
end

#languageObject (readonly)

Returns the value of attribute language.



8
9
10
# File 'lib/lutaml/rdf/literal.rb', line 8

def language
  @language
end

#valueObject (readonly)

Returns the value of attribute value.



8
9
10
# File 'lib/lutaml/rdf/literal.rb', line 8

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



37
38
39
40
41
42
# File 'lib/lutaml/rdf/literal.rb', line 37

def ==(other)
  other.is_a?(self.class) &&
    value == other.value &&
    datatype == other.datatype &&
    language == other.language
end

#hashObject



45
46
47
# File 'lib/lutaml/rdf/literal.rb', line 45

def hash
  [value, datatype, language].hash
end

#to_jsonld_termObject



27
28
29
30
31
32
33
34
35
# File 'lib/lutaml/rdf/literal.rb', line 27

def to_jsonld_term
  if language
    { "@value" => value, "@language" => language }
  elsif datatype
    { "@value" => value, "@type" => datatype.to_s }
  else
    value
  end
end

#to_turtleObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/lutaml/rdf/literal.rb', line 16

def to_turtle
  escaped = escape_turtle(value.to_s)
  if language
    "#{escaped}@#{language}"
  elsif datatype
    "#{escaped}^^<#{datatype}>"
  else
    escaped
  end
end