Sha256: a16f39e599d03d10b8061053dd28f26b6ba787bd234d236efc4d4ab322760287

Contents?: true

Size: 1.69 KB

Versions: 2

Compression:

Stored size: 1.69 KB

Contents

#   Copyright 2011 innoQ Deutschland GmbH
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

module IqRdf
  class Literal

    def initialize(obj, lang = nil, datatype = nil)
      raise "#{datatype.inspect} is not an URI" if datatype && !datatype.is_a?(::URI)
      @obj = obj
      @datatype = datatype
      @lang = lang
    end

    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

    def to_s(parent_lang = nil)
      lang = @lang || parent_lang # Use the Literals lang when given
      quote = @obj.to_s.include?("\n") ? '"""' : '"'
      "#{quote}#{@obj.to_s.gsub("\\", "\\\\\\\\").gsub(/"/, "\\\"")}#{quote}" <<
          ((lang && lang != :none) ? "@#{lang}" : "") <<
          (@datatype ? "^^<#{@datatype.to_s}>" : "")
    end

    def build_xml(xml, &block)
      opts = {}
      opts["rdf:datatype"] = @datatype.to_s if @datatype
      opts["xml:lang"] = @lang if @lang
      block.call(@obj.to_s, opts)
    end

    alias_method :full_uri, :to_s

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
iq_rdf-0.1.4 lib/iq_rdf/literal.rb
iq_rdf-0.1.3 lib/iq_rdf/literal.rb