Class: IqRdf::Uri

Inherits:
Node
  • Object
show all
Defined in:
lib/iq_rdf/uri.rb

Direct Known Subclasses

Predicate

Instance Attribute Summary collapse

Attributes inherited from Node

#lang, #nodes

Instance Method Summary collapse

Methods inherited from Node

#<<, #build_full_uri_predicate, #method_missing, #xml_lang

Constructor Details

#initialize(namespace, uri_postfix, rdf_type = nil, lang = nil) ⇒ Uri

Returns a new instance of Uri.



21
22
23
24
25
26
27
# File 'lib/iq_rdf/uri.rb', line 21

def initialize(namespace, uri_postfix, rdf_type = nil, lang = nil)
  raise "rdf_type has to be an IqRdf::Uri" unless rdf_type.nil? || rdf_type.is_a?(IqRdf::Uri)
  @namespace = namespace
  @uri_postfix = uri_postfix
  @rdf_type = rdf_type
  super(lang)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class IqRdf::Node

Instance Attribute Details

#namespaceObject (readonly)

Returns the value of attribute namespace.



18
19
20
# File 'lib/iq_rdf/uri.rb', line 18

def namespace
  @namespace
end

#rdf_typeObject (readonly)

Returns the value of attribute rdf_type.



19
20
21
# File 'lib/iq_rdf/uri.rb', line 19

def rdf_type
  @rdf_type
end

#uri_postfixObject (readonly)

Returns the value of attribute uri_postfix.



18
19
20
# File 'lib/iq_rdf/uri.rb', line 18

def uri_postfix
  @uri_postfix
end

Instance Method Details

#build_xml(xml, &block) ⇒ Object



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

def build_xml(xml, &block)
  if (is_subject?)
    attrs = {"rdf:about" => self.full_uri}
    attrs["xml:lang"] = self.lang if self.lang
    xml.rdf(:Description, attrs) do
      if self.rdf_type
        xml.rdf(:type, "rdf:resource" => self.rdf_type.full_uri)
      end
      self.nodes.each do |predicate|
        predicate.build_xml(xml)
      end
    end
  elsif block
    block.call("rdf:resource" => self.full_uri)
  end
end

#full_uri(parent_lang = nil) ⇒ Object



29
30
31
32
# File 'lib/iq_rdf/uri.rb', line 29

def full_uri(parent_lang = nil)
  # URI::join etc won't work since uri_postfix has not to be a valid URI itself.
  "#{self.namespace.uri_prefix.to_s}#{self.uri_postfix.to_s}"
end

#is_subject?Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
# File 'lib/iq_rdf/uri.rb', line 42

def is_subject?()
  if (nodes.size > 0) # walk through the nodes: a blank node is an object (but has nodes)
    nodes.each do |node|
      return true unless node.is_a?(BlankNode)
    end
  end
  return !rdf_type.nil?
end

#to_s(parent_lang = nil) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/iq_rdf/uri.rb', line 34

def to_s(parent_lang = nil)
  if namespace.token # There is a dummy_empty_namespace without token => postfix is a full uri!
    "#{namespace.turtle_token}:#{self.uri_postfix.to_s}"
  else
    "<#{self.uri_postfix.to_s}>"
  end
end