Class: Redlander::Node
- Inherits:
-
Object
- Object
- Redlander::Node
- Defined in:
- lib/redlander/node.rb
Overview
RDF node (usually, a part of an RDF statement)
Instance Method Summary collapse
-
#blank? ⇒ Boolean
Return true if node is a blank node.
-
#datatype ⇒ Object
Datatype URI for the literal node, or nil.
-
#eql?(other_node) ⇒ Boolean
(also: #==)
Equivalency.
- #hash ⇒ Object
-
#initialize(arg = nil, options = {}) ⇒ Node
constructor
Create a RDF node.
- #lang ⇒ Object
-
#literal? ⇒ Boolean
Return true if node is a literal.
- #rdf_node ⇒ Object private
-
#resource? ⇒ Boolean
Check whether the node is a resource (identified by a URI).
-
#to_s ⇒ String
Convert this node to a string (with a datatype suffix).
-
#uri ⇒ URI?
Internal URI of the Node.
-
#value ⇒ URI, Any
Value of the literal node as a Ruby object instance.
Constructor Details
#initialize(arg = nil, options = {}) ⇒ Node
Create a RDF node.
60 61 62 63 64 65 |
# File 'lib/redlander/node.rb', line 60 def initialize(arg = nil, = {}) # 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 = end |
Instance Method Details
#blank? ⇒ Boolean
Return true if node is a blank node.
84 85 86 |
# File 'lib/redlander/node.rb', line 84 def blank? Redland.librdf_node_is_blank(rdf_node) != 0 end |
#datatype ⇒ Object
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.
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 |
#hash ⇒ Object
97 98 99 |
# File 'lib/redlander/node.rb', line 97 def hash self.class.hash + to_s.hash end |
#lang ⇒ Object
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.
77 78 79 |
# File 'lib/redlander/node.rb', line 77 def literal? Redland.librdf_node_is_literal(rdf_node) != 0 end |
#rdf_node ⇒ Object
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)
70 71 72 |
# File 'lib/redlander/node.rb', line 70 def resource? Redland.librdf_node_is_resource(rdf_node) != 0 end |
#to_s ⇒ String
Convert this node to a string (with a datatype suffix).
104 105 106 |
# File 'lib/redlander/node.rb', line 104 def to_s Redland.librdf_node_to_string(rdf_node) end |
#uri ⇒ URI?
Internal URI of the Node.
Returns the datatype URI for literal nodes, nil for blank nodes.
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 |
#value ⇒ URI, Any
Value of the literal node as a Ruby object instance.
Returns an instance of URI for resource nodes, “blank identifier” for blank nodes.
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 |