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

Defined Under Namespace

Classes: Boolean, Numeric, String, URI

Constant Summary collapse

ESCAPE_MAP =

Turtle accepts the same escapes as N-Triples, so both formats share this

{
  "\\" => "\\\\",
  '"'  => '\\"',
  "\n" => "\\n",
  "\r" => "\\r",
  "\t" => "\\t"
}.freeze
LONG_ESCAPE_MAP =

Inside """...""" line breaks and tabs are legal as they are

{
  "\\" => "\\\\",
  '"'  => '\\"'
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Literal.



33
34
35
36
37
38
# File 'lib/iq_rdf/literal.rb', line 33

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



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

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



89
90
91
92
93
94
95
96
97
98
# File 'lib/iq_rdf/literal.rb', line 89

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



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/iq_rdf/literal.rb', line 76

def to_ntriples(parent_lang = nil)
  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

  "\"#{@obj.to_s.gsub(/[\\"\n\r\t]/, ESCAPE_MAP)}\"#{suffix}"
end

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/iq_rdf/literal.rb', line 52

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

  value = @obj.to_s

  if value.match?(/[\n\r]/)
    # A long string keeps line breaks readable, which is the point of
    # Turtle over N-Triples. Its grammar allows raw line breaks and tabs,
    # so only backslashes and quotes need escaping - escaping every quote
    # also rules out a run of three ending the string prematurely.
    %("""#{value.gsub(/[\\"]/, LONG_ESCAPE_MAP)}"""#{lang}#{datatype})
  else
    %("#{value.gsub(/[\\"\n\r\t]/, ESCAPE_MAP)}"#{lang}#{datatype})
  end
end