Class: Redlander::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/redlander/node.rb

Overview

RDF node (usually, a part of an RDF statement)

Instance Method Summary collapse

Constructor Details

#initialize(arg = nil, options = {}) ⇒ Node

Create a RDF node.

Parameters:

  • arg (Any) (defaults to: nil)
    • an instance of URI - to create a RDF “resource”, Note that you cannot create a resource node from an URI string, it must be an instance of URI. Otherwise it is treated as a string literal.

    • nil (or absent) - to create a blank node,

    • any other Ruby object, which can be coerced into a literal.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :blank_id (String)

    optional ID to use for a blank node.

Raises:

  • (RedlandError)

    if it fails to create a node from the given args.



60
61
62
63
64
65
# File 'lib/redlander/node.rb', line 60

def initialize(arg = nil, options = {})
  # If FFI::Pointer is passed, wrap it instantly,
  # because it can be freed outside before it is used here.
  @arg = arg.is_a?(FFI::Pointer) ? wrap(arg) : arg
  @options = options
end

Instance Method Details

#blank?Boolean

Return true if node is a blank node.

Returns:

  • (Boolean)


84
85
86
# File 'lib/redlander/node.rb', line 84

def blank?
  Redland.librdf_node_is_blank(rdf_node) != 0
end

#datatypeObject

Datatype URI for the literal node, or nil



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/redlander/node.rb', line 36

def datatype
  if instance_variable_defined?(:@datatype)
    @datatype
  else
    @datatype = if literal?
                  rdf_uri = Redland.librdf_node_get_literal_value_datatype_uri(rdf_node)
                  rdf_uri.null? ? XmlSchema.datatype_of("") : URI.parse(Redland.librdf_uri_to_string(rdf_uri))
                else
                  nil
                end
  end
end

#eql?(other_node) ⇒ Boolean Also known as: ==

Equivalency. Only works for comparing two Nodes.

Parameters:

  • other_node (Node)

    Node to be compared with.

Returns:

  • (Boolean)


92
93
94
# File 'lib/redlander/node.rb', line 92

def eql?(other_node)
  Redland.librdf_node_equals(rdf_node, other_node.rdf_node) != 0
end

#hashObject



97
98
99
# File 'lib/redlander/node.rb', line 97

def hash
  self.class.hash + to_s.hash
end

#langObject



142
143
144
145
# File 'lib/redlander/node.rb', line 142

def lang
  lng = Redland.librdf_node_get_literal_value_language(rdf_node)
  lng ? lng.to_sym : nil
end

#literal?Boolean

Return true if node is a literal.

Returns:

  • (Boolean)


77
78
79
# File 'lib/redlander/node.rb', line 77

def literal?
  Redland.librdf_node_is_literal(rdf_node) != 0
end

#rdf_nodeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/redlander/node.rb', line 14

def rdf_node
  unless instance_variable_defined?(:@rdf_node)
    @rdf_node = case @arg
                when FFI::Pointer
                  @arg
                when NilClass
                  Redland.librdf_new_node_from_blank_identifier(Redlander.rdf_world, @options[:blank_id])
                when URI
                  Redland.librdf_new_node_from_uri_string(Redlander.rdf_world, @arg.to_s)
                else
                  value = @arg.respond_to?(:xmlschema) ? @arg.xmlschema : @arg.to_s
                  lang = @arg.respond_to?(:lang) ? @arg.lang.to_s : nil
                  dt = lang ? nil : Uri.new(XmlSchema.datatype_of(@arg)).rdf_uri
                  Redland.librdf_new_node_from_typed_literal(Redlander.rdf_world, value, lang, dt)
                end
    raise RedlandError, "Failed to create a new node" if @rdf_node.null?
    ObjectSpace.define_finalizer(self, self.class.send(:finalize_node, @rdf_node))
  end
  @rdf_node
end

#resource?Boolean

Check whether the node is a resource (identified by a URI)

Returns:

  • (Boolean)


70
71
72
# File 'lib/redlander/node.rb', line 70

def resource?
  Redland.librdf_node_is_resource(rdf_node) != 0
end

#to_sString

Convert this node to a string (with a datatype suffix).

Returns:

  • (String)


104
105
106
# File 'lib/redlander/node.rb', line 104

def to_s
  Redland.librdf_node_to_string(rdf_node)
end

#uriURI?

Internal URI of the Node.

Returns the datatype URI for literal nodes, nil for blank nodes.

Returns:

  • (URI, nil)


114
115
116
117
118
119
120
121
122
# File 'lib/redlander/node.rb', line 114

def uri
  if resource?
    URI.parse(to_s[1..-2])
  elsif literal?
    datatype
  else
    nil
  end
end

#valueURI, Any

Value of the literal node as a Ruby object instance.

Returns an instance of URI for resource nodes, “blank identifier” for blank nodes.

Returns:

  • (URI, Any)


130
131
132
133
134
135
136
137
138
139
140
# File 'lib/redlander/node.rb', line 130

def value
  if resource?
    uri
  elsif blank?
    Redland.librdf_node_get_blank_identifier(rdf_node).force_encoding("UTF-8")
  else
    v = Redland.librdf_node_get_literal_value(rdf_node).force_encoding("UTF-8")
    v << "@#{lang}" if lang
    XmlSchema.instantiate(v, datatype)
  end
end