Class: RDF::Node

Inherits:
Hash
  • Object
show all
Includes:
Parser
Defined in:
lib/lightrdf/node.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Parser

parse, #serialize, #to_ntriples

Constructor Details

#initialize(id = nil, graph = nil) ⇒ Node

Returns a new instance of Node.



30
31
32
33
34
# File 'lib/lightrdf/node.rb', line 30

def initialize id=nil, graph=nil
  @id = ID(id)
  @graph = graph || Graph.new
  @graph[@id] = self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



36
37
38
# File 'lib/lightrdf/node.rb', line 36

def method_missing method, *args
  ID.ns[method] ? NsProxy.new(method, self) : super
end

Instance Attribute Details

#graphObject

Returns the value of attribute graph.



28
29
30
# File 'lib/lightrdf/node.rb', line 28

def graph
  @graph
end

#idObject

Returns the value of attribute id.



28
29
30
# File 'lib/lightrdf/node.rb', line 28

def id
  @id
end

Class Method Details

.classesObject



26
# File 'lib/lightrdf/node.rb', line 26

def self.classes; @classes; end

Instance Method Details

#==(node) ⇒ Object



50
51
52
# File 'lib/lightrdf/node.rb', line 50

def == node
  eql? node
end

#[](name) ⇒ Object



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

def [] name
  ( super(ID(name)) || [] ).clone
end

#[]=(name, values) ⇒ Object



46
47
48
# File 'lib/lightrdf/node.rb', line 46

def []= name, values
  super(ID(name), [values].flatten.map { |node| node.is_a?(Node) ? @graph[node] : node })
end

#all_triples(done = []) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/lightrdf/node.rb', line 69

def all_triples done=[]
  return [] if done.include?(self)
  done << self
  triples = self.triples
  triples.map { |s,p,o| [s,o] }.flatten.uniq.each { |node| triples += graph[node].all_triples(done) if node.is_a?(Symbol) }
  triples
end

#bnode?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/lightrdf/node.rb', line 107

def bnode?
  ID.bnode?(id)
end

#cloneObject



103
104
105
# File 'lib/lightrdf/node.rb', line 103

def clone
  self.graph.clone[id]
end

#eql?(node) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/lightrdf/node.rb', line 53

def eql? node
  self.class == node.class and self.id == node.id
end

#hashObject

Hack for Ruby 1.8.6



56
57
58
# File 'lib/lightrdf/node.rb', line 56

def hash # Hack for Ruby 1.8.6
  id.hash ^ self.class.hash
end

#inspectObject



39
# File 'lib/lightrdf/node.rb', line 39

def inspect; "<#{self.class} #{id} #{super}>"; end

#merge(node) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/lightrdf/node.rb', line 77

def merge node
  new_node = Node(id)
  (self.keys + node.keys).uniq.each do |k|
    new_node[k] = (node[k] + self[k]).uniq
  end
  new_node
end

#merge!(node) ⇒ Object



85
86
87
88
89
90
# File 'lib/lightrdf/node.rb', line 85

def merge! node
  (self.keys + node.keys).uniq.each do |k|
    self[k] = (node[k] + self[k]).uniq
  end
  self
end

#predicatesObject



60
61
62
# File 'lib/lightrdf/node.rb', line 60

def predicates
  keys.map { |p| [ @graph[p], self[p] ] }
end

#proxy(type = nil) ⇒ Object



111
112
113
# File 'lib/lightrdf/node.rb', line 111

def proxy type=nil
  graph.node(self, type)
end

#rename(new_id = nil) ⇒ Object



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

def rename new_id=nil
  clone.rename! new_id
end

#rename!(new_id = nil) ⇒ Object



96
97
98
99
100
101
# File 'lib/lightrdf/node.rb', line 96

def rename! new_id=nil
  self.graph.delete id
  self.id = ID(new_id)
  self.graph[id] = self
  self
end

#to_sObject



40
# File 'lib/lightrdf/node.rb', line 40

def to_s;   id.to_s; end

#to_symObject



41
# File 'lib/lightrdf/node.rb', line 41

def to_sym; id;      end

#triplesObject



64
65
66
67
# File 'lib/lightrdf/node.rb', line 64

def triples
  triples = []; each { |p, v| v.each { |o| triples << [id, p, o.is_a?(Node) ? o.id : o] } }
  triples
end