class Grom::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/node.rb, line 12 def initialize(statements, decorators = nil) @statements = statements populate(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/node.rb, line 70 def blank? @statements.first.subject.anonymous? 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/node.rb, line 43 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/node.rb, line 63 def respond_to_missing?(method, include_all = false) instance_variable_get("@#{method}".to_sym) || super end
Private Instance Methods
# File lib/grom/node.rb, line 81 def populate(decorators) set_graph_id @statements.each do |statement| predicate = Grom::Helper.get_id(statement.predicate).to_sym object = statement.object.to_s 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/node.rb, line 76 def set_graph_id graph_id = Grom::Helper.get_id(@statements.first.subject) instance_variable_set('@graph_id'.to_sym, graph_id) end