class GromNative::Node
A Ruby object populated with n-triple data.
@since 0.1.0 @attr_reader [Array] statements an array of n-triple statements.
Constants
- BLANK
Attributes
Public Class Methods
@param [Array] statements an array of n-triple statements.
# File lib/grom_native/node.rb, line 12 def initialize(statements, decorators = nil) populate(statements, decorators) end
Public Instance Methods
Checks if Grom::Node is a blank node
@return [Boolean] a boolean depending on whether or not the Grom::Node is a blank node
# File lib/grom_native/node.rb, line 68 def blank? @graph_id.match(%r(^_:)) end
Allows the user to access instance variables as methods or raise an error if the variable is not defined.
@param [Symbol] method name of method. @param [Array] *params extra arguments to pass to super. @param [Block] &block block to pass to super. @example Accessing instance variables populated from statements
statements = [ RDF::Statement.new(RDF::URI.new('http://example.com/123'), RDF.type, 'Person'), RDF::Statement.new(RDF::URI.new('http://example.com/123'), RDF::URI.new('http://example.com/forename'), 'Jane'), RDF::Statement.new(RDF::URI.new('http://example.com/123'), RDF::URI.new('http://example.com/surname'), 'Smith') ] node = Grom::Node.new(statements) node.forename #=> 'Jane'
@example Accessing instance variables created on the fly
statements = [RDF::Statement.new(RDF::URI.new('http://example.com/123'), RDF.type, 'Person')] node = Grom::Node.new(statements) node.instance_variable_set('@foo', 'bar') node.foo #=> 'bar'
@raise [NoMethodError] raises error if the method does not exist.
# File lib/grom_native/node.rb, line 41 def method_missing(method, *params, &block) instance_variable_get("@#{method}".to_sym) || super end
node = Grom::Node.new(statements)
node.respond_to?(:forename) #=> 'Jane' node.respond_to?(:foo) #=> false
# File lib/grom_native/node.rb, line 61 def respond_to_missing?(method, include_all = false) instance_variable_get("@#{method}".to_sym) || super end
Private Instance Methods
# File lib/grom_native/node.rb, line 79 def populate(statements, decorators) set_graph_id(statements) statements.each do |statement| predicate = Grom::Helper.get_id(statement['predicate']).to_sym object = RDF::NTriples::Reader.parse_object(statement['object']) object = if object.is_a? RDF::URI object.to_s else object.object end instance_variable = instance_variable_get("@#{predicate}") if instance_variable instance_variable = instance_variable.is_a?(Array) ? instance_variable.flatten : [instance_variable] instance_variable << object instance_variable_set("@#{predicate}", instance_variable) else instance_variable_set("@#{predicate}", object) end decorators&.decorate_with_type(self, object) if statement['predicate'] == RDF.type && decorators end end
# File lib/grom_native/node.rb, line 74 def set_graph_id(statements) graph_id = Grom::Helper.get_id(statements.first['subject']) instance_variable_set('@graph_id'.to_sym, graph_id) end