Class: RDF::Node
- Inherits:
-
Hash
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
#graph ⇒ Object
Returns the value of attribute graph.
28
29
30
|
# File 'lib/lightrdf/node.rb', line 28
def graph
@graph
end
|
#id ⇒ Object
Returns the value of attribute id.
28
29
30
|
# File 'lib/lightrdf/node.rb', line 28
def id
@id
end
|
Class Method Details
.classes ⇒ Object
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
107
108
109
|
# File 'lib/lightrdf/node.rb', line 107
def bnode?
ID.bnode?(id)
end
|
#clone ⇒ Object
103
104
105
|
# File 'lib/lightrdf/node.rb', line 103
def clone
self.graph.clone[id]
end
|
#eql?(node) ⇒ Boolean
53
54
55
|
# File 'lib/lightrdf/node.rb', line 53
def eql? node
self.class == node.class and self.id == node.id
end
|
#hash ⇒ Object
56
57
58
|
# File 'lib/lightrdf/node.rb', line 56
def hash id.hash ^ self.class.hash
end
|
#inspect ⇒ Object
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
|
#predicates ⇒ Object
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_s ⇒ Object
40
|
# File 'lib/lightrdf/node.rb', line 40
def to_s; id.to_s; end
|
#to_sym ⇒ Object
41
|
# File 'lib/lightrdf/node.rb', line 41
def to_sym; id; end
|
#triples ⇒ Object
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
|