Class: IqRdf::Literal

Inherits:
Object
  • Object
show all
Defined in:
lib/iq_rdf/literal.rb,
lib/iq_rdf/literal/uri.rb,
lib/iq_rdf/literal/string.rb,
lib/iq_rdf/literal/boolean.rb,
lib/iq_rdf/literal/numeric.rb

Direct Known Subclasses

Boolean, Numeric, String, URI

Defined Under Namespace

Classes: Boolean, Numeric, String, URI

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj, lang = nil, datatype = nil) ⇒ Literal

Returns a new instance of Literal.



18
19
20
21
22
23
# File 'lib/iq_rdf/literal.rb', line 18

def initialize(obj, lang = nil, datatype = nil)
  raise "#{datatype.inspect} is not an URI" unless datatype.nil? || datatype.is_a?(::URI) || datatype.is_a?(IqRdf::Uri)
  @obj = obj
  @datatype = datatype
  @lang = lang
end

Class Method Details

.build(o) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/iq_rdf/literal.rb', line 25

def self.build(o)
  if o.is_a?(::URI)
    IqRdf::Literal::URI.new(o)
  elsif o === true || o === false
    IqRdf::Literal::Boolean.new(o)
  elsif o.is_a?(::Numeric)
    IqRdf::Literal::Numeric.new(o)
  else
    IqRdf::Literal::String.new(o)
  end
end

Instance Method Details

#build_xml(xml, &block) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/iq_rdf/literal.rb', line 66

def build_xml(xml, &block)
  opts = {}
  if @datatype.is_a?(::URI)
    opts["rdf:datatype"] = @datatype.to_s
  elsif @datatype.is_a?(IqRdf::Uri)
    opts["rdf:datatype"] = @datatype.full_uri
  end
  opts["xml:lang"] = @lang if @lang
  block.call(@obj.to_s, opts)
end

#to_ntriples(parent_lang = nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/iq_rdf/literal.rb', line 52

def to_ntriples(parent_lang = nil)
  quote = @obj.to_s.include?("\n") ? '"""' : '"'
  suffix = if @datatype.is_a?(::URI)
    "^^<#{@datatype.to_s}>"
  elsif @datatype.is_a?(IqRdf::Uri)
    "^^<#{@datatype.full_uri}>"
  else
    lang = @lang || parent_lang # Use the Literals lang when given
    (lang && lang != :none) ? "@#{lang}" : ""
  end

  "#{quote}#{@obj.to_s.gsub("\\", "\\\\\\\\").gsub(/"/, "\\\"")}#{quote}#{suffix}"
end

#to_s(options = {}) ⇒ Object Also known as: full_uri



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/iq_rdf/literal.rb', line 37

def to_s(options = {})
  lang = @lang || options[:lang] # Use the Literals lang when given
  lang = (lang && lang != :none) ? "@#{lang}" : ""
  quote = @obj.to_s.include?("\n") ? '"""' : '"'
  datatype = if @datatype.is_a?(::URI)
    "^^<#{@datatype.to_s}>"
  elsif @datatype.is_a?(IqRdf::Uri)
    "^^#{@datatype.to_s}"
  else
    ""
  end

  "#{quote}#{@obj.to_s.gsub("\\", "\\\\\\\\").gsub(/"/, "\\\"")}#{quote}#{lang}#{datatype}"
end