Class: Grom::Node

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

Overview

A Ruby object populated with n-triple data.

Since:

  • 0.1.0

Constant Summary collapse

BLANK =

Since:

  • 0.1.0

'blank_node'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(statements, decorators = nil) ⇒ Node

Returns a new instance of Node.

Parameters:

  • statements (Array)

    an array of n-triple statements.

Since:

  • 0.1.0



12
13
14
15
16
# File 'lib/grom/node.rb', line 12

def initialize(statements, decorators = nil)
  @statements = statements

  populate(decorators)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *params, &block) ⇒ Object

Allows the user to access instance variables as methods or raise an error if the variable is not defined.

Examples:

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'

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'

Parameters:

  • method (Symbol)

    name of method.

  • *params (Array)

    extra arguments to pass to super.

  • &block (Block)

    block to pass to super.

Raises:

  • (NoMethodError)

    raises error if the method does not exist.

Since:

  • 0.1.0



43
44
45
# File 'lib/grom/node.rb', line 43

def method_missing(method, *params, &block)
  instance_variable_get("@#{method}".to_sym) || super
end

Instance Attribute Details

#statementsArray (readonly)

an array of n-triple statements.

Returns:

  • (Array)

    the current value of statements

Since:

  • 0.1.0



6
7
8
# File 'lib/grom/node.rb', line 6

def statements
  @statements
end

Instance Method Details

#blank?Boolean

Checks if Grom::Node is a blank node

Returns:

  • (Boolean)

    a boolean depending on whether or not the Grom::Node is a blank node

Since:

  • 0.1.0



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

def blank?
  @statements.first.subject.anonymous?
end

#respond_to_missing?(method, include_all = false) ⇒ Boolean

node = Grom::Node.new(statements)

node.respond_to?(:forename) #=> 'Jane'
node.respond_to?(:foo) #=> false

Returns:

  • (Boolean)

Since:

  • 0.1.0



63
64
65
# File 'lib/grom/node.rb', line 63

def respond_to_missing?(method, include_all = false)
  instance_variable_get("@#{method}".to_sym) || super
end